Skip to content

Instantly share code, notes, and snippets.

@jlinoff
jlinoff / aes_example.py
Created December 11, 2015 17:43
Python example that shows how to use the pycrypto (Crypto) module to do AES encryption and decryption.
#!/usr/bin/env python
'''
Simple example of how to use the crypto library for AES encryption and
decryption.
Make sure that the Crypto (pycrypto) module is installed:
$ pip-2.7 install pycrypto
$ pip-3.5 install pycrypto
@jlinoff
jlinoff / copy_large_file.py
Last active February 1, 2022 23:47
Python example that shows how to copy a large file showing progress.
#!/usr/bin/env python
'''
Copy a large file showing progress.
MIT License
Copyright (c) 2015 Joe Linoff
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
@jlinoff
jlinoff / download-repo-rpms.sh
Last active March 9, 2021 14:47
Download all RPM files from base and epel repositories.
#!/bin/bash
# Download RPM files from the base and epel repositories.
for Repo in base epel ; do
OutDir=/opt/shared/repo/$Repo
[ ! -d $OutDir ] && mkdir -p $OutDir
for RPM in $(repoquery -qa --repoid=$Repo) ; do
yum install $RPM -y --downloadonly --downloaddir=$OutDir
done
done
@jlinoff
jlinoff / runcmd.py
Last active June 20, 2023 06:18
Python example that shows how to run non-interactive shell commands using two different subprocess implementations.
#!/usr/bin/env python
'''
Python example that shows how to run non-interactive shell commands using
two different subprocess implementations.
Works in Python 2.7 and Python 3.x.
LICENSE (MIT Open Source)
Copyright (c) 2016 Joe Linoff
@jlinoff
jlinoff / debug.py
Last active March 9, 2021 14:47
Simple debug function that uses introspection to display the file name, function name and line number of the caller.
def debug(msg):
'''
Display a debug message with the file name, function
name and line number.
'''
import inspect
parent_frame = inspect.currentframe().f_back
lineno = parent_frame.f_lineno
fctname = parent_frame.f_code.co_name
filename = parent_frame.f_code.co_filename
@jlinoff
jlinoff / os.path.sh
Last active January 24, 2018 19:06
Bash path functions modeled after python: os.path.normpath, os.path.abspath, os.path.relpath.
#!/bin/bash
#
# Some useful path functions. Modelled after Python.
#
# License: MIT Open Source
# Copyright (c) 2016 Joe Linoff
#
# Normalize a path.
# Similar to Python's os.path.normpath()
@jlinoff
jlinoff / progress.sh
Created January 27, 2016 17:42
Bash example to report the progress of dealing with n items that also has an estimate of time remaining.
#!/bin/bash
#
# Function to measure and report progress.
# A bit like PV but for general things like copying N files.
# Use it as a starting point to created your own.
#
# License: MIT Open Source
# Copyright (c) 2016 Joe Linoff
# Show the progress of N items with an estimate of the time remaining.
@jlinoff
jlinoff / mysshpass.py
Last active September 14, 2022 14:22
Simple python script to emulate sshpass using only pthon standard libraries, tested on python-2.7 and python-3.4.
#!/usr/bin/env python
# License: MIT Open Source
# Copyright (c) 2016 Joe Linoff
'''
The tool runs ssh or scp as a child process with a connected psuedo
terminal (pty).
It will automatically answer yes to the "Are you sure you want to
continue connecting (yes/no)?" if it comes up.
@jlinoff
jlinoff / find_open_files.py
Last active March 9, 2021 14:47
Python script that shows how to find all of the open files on a linux system by process /proc/pid/fd.
#!/usr/bin/env python
# License: MIT Open Source
# Copyright (c) 2016 Joe Linoff
'''
This example shows how to find and report all open files on a linux
system by reading /proc/<pid>/fd.
It is similar to the basic functionality provided by the command line
tool lsof but lsof is much faster and provides a lot more
capabilities.
@jlinoff
jlinoff / fix-img-parts.sh
Created March 18, 2016 21:19
Bash script to automatically find and fix the partitions in an image file on linux using losetup, fdisk, mke2fs and fsck.
#!/bin/bash
#
# This image will fix all of the partitions in a disk image.
#
# Usage:
# fix-image.sh test.img
#
# License: MIT Open Source
# Copyright (c) 2016 by Joe Linoff