Skip to content

Instantly share code, notes, and snippets.

@jlinoff
jlinoff / heartbeat.sh
Last active March 9, 2021 14:44
Bash shell script that runs a simple command (like ping) periodically and tests the results.
#!/bin/bash
#
# Run a simple command every 15 seconds and report whether it passes
# or fails. It can be used to check for disconnected hosts (a
# heartbeat test).
#
# The minimum valid interval depends on the time it takes to execute
# the command.
#
# Usage:
@jlinoff
jlinoff / rsa-example.py
Last active January 27, 2019 18:31
Simple RSA implementation in python3 that demonstrates key generation, encryption, padding and decryption.
#!/usr/bin/env python3.7
'''
Demonstrate the RSA algorithm key generation, encryption and
decryption with a simple padding scheme.
It is useful for understanding how RSA works in an ecosystem of
padding and translation to encrypt and decrypt text information.
It was written because many of examples of RSA do a great job
of describing the algorithm but they do not show an end to end
@jlinoff
jlinoff / semver.sh
Created February 16, 2018 17:28
Bash function to parse major, minor and patch versions from a semantic versioning string
#!/bin/bash
#
# Function that parses semantic versioning striings of
# the form:
# MAJOR.MINOR.PATCH([+-].*)?
#
# Parse the major, minor and patch versions
# out.
# You use it like this:
@jlinoff
jlinoff / trace.sh
Last active April 6, 2022 02:51
Bash script that shows each executing statement with file, function and lineno information. It also executes set +x silently.
#!/bin/bash
#
# Show how to make statements verbose with custom information.
#
function fct() {
echo "$*"
}
##PS4='+ LINE:${BASH_SOURCE[0]}:${BASH_FUNCNAME[0]}:${LINENO}: '
@jlinoff
jlinoff / state-machine-test.sh
Created February 8, 2018 17:59
Use bash introspection to enable simple state handling.
#!/bin/bash
#
# Tests the state machine handler.
# Define 5 functions, auto-generate a state machine control mechanism to
# determine which functions are executed.
#
# License: MIT Open Source
# Copyright (c) 2018 by Joe Linoff
source state-machine.sh
@jlinoff
jlinoff / state-machine-test.sh
Created February 8, 2018 17:59
Use bash introspection to enable simple state handling.
#!/bin/bash
#
# Tests the state machine handler.
# Define 5 functions, auto-generate a state machine control mechanism to
# determine which functions are executed.
#
# License: MIT Open Source
# Copyright (c) 2018 by Joe Linoff
source state-machine.sh
@jlinoff
jlinoff / fixipv4s.py
Last active January 12, 2018 20:27
Find and replace IPv4 addresses in python.
#!/usr/bin/env python
'''
Find all IPv4 addresses on a line and replace the interesting
ones.
Interesting IP addresses are those that are not in the exclude
list.
'''
# License: MIT Open Source
# Copyright (c) 2018 by Joe Linoff
@jlinoff
jlinoff / argparse.sh
Last active March 9, 2021 14:45
Bash argument parser idiom that accepts short form, long form and long form = options.
#!/bin/bash
#
# Copyright (c) 2017 by Joe Linoff
# MIT Open Source License.
#
# This script shows how to implement an argument parser with
# 4 options. Two of the options are simply flags, one of
# of them has a single argument and the other has 2 arguments.
#
# It is meant to show bash can support reasonably complex
@jlinoff
jlinoff / purge.sh
Created December 6, 2017 13:23
Bash script to purge all but the last N files that match a pattern.
#/bin/bash
#
# Purge all but the last N files in the input list.
#
if (( $# > 1 )) ; then
readonly NUM=$1
shift
readonly LIST=($*)
readonly LEN=$(( ${#LIST[@]} - $NUM ))
if (( LEN )) ; then
@jlinoff
jlinoff / fix-python-header.sh
Last active March 9, 2021 14:45
fix-python-headers - bash tool to fix python file header references
#!/bin/bash
#
# Fix the first line of python a file to point to the
# new python interpreter.
#
# This mess required to make sure that script works on
# the Mac. Normally we would just use readlink.
set -e
RootDir=$(cd $(dirname $0) && pwd)
source $RootDir/libutils.sh