Skip to content

Instantly share code, notes, and snippets.

View rsperl's full-sized avatar

Richard rsperl

  • North Carolina, United States
View GitHub Profile
@rsperl
rsperl / process_watcher.sh
Last active July 25, 2022 15:04
script to watch a process and report resource usage #snippet
#!/bin/bash
procname="$1"
if [[ -z "$procname" ]]; then
echo "Usage: $0 <procname> [sleeptime] [timeout]"
exit 1
fi
sleeptime="${2:-1}"
if [[ "$sleeptime" -lt 1 ]] || [[ "$sleeptime" -gt 60 ]]; then
echo "sleeptime must be an integer between 1-60"
@rsperl
rsperl / chainmap_demo.py
Last active July 25, 2022 15:07
chainmaps #snippet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
from collections import ChainMap
except ImportError:
from chainmap import ChainMap
uservars = {"layer": 1, "layer1": True}
defaults = {"layer": 2, "layer2": True}
@rsperl
rsperl / Makefile #snippet
Last active April 3, 2025 14:04
self-documenting makefile with colors
SHELL=/bin/bash
# to see all colors, run
# bash -c 'for c in {0..255}; do tput setaf $c; tput setaf $c | cat -v; echo =$c; done'
# the first 15 entries are the 8-bit colors
# define standard colors
ifneq (,$(findstring xterm,${TERM}))
BLACK := $(shell tput -Txterm setaf 0)
RED := $(shell tput -Txterm setaf 1)
@rsperl
rsperl / filetime2epoch.py
Last active July 25, 2022 15:12
convert ldap windows filetime to epoch and timestamp #snippet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from datetime import datetime
filetime = int(sys.argv[1])
seconds_till_epoch = 11644473600
epoch = filetime / 10000000 - seconds_till_epoch
@rsperl
rsperl / printf_padded_header.sh
Last active July 25, 2022 15:06
printf a padded header in bash #snippet
#!/bin/bash
function header() {
local name="$1"
echo ""
printf '= %-.100s %.s\n' "$name $(printf '%.1s' ={1..100})"
}
header "my header"
@rsperl
rsperl / docker_commands.sh #snippet
Last active July 25, 2022 15:38
various docker commands that use the underlying json to do cool things
#!/bin/bash
# sort by created
jq -s -c 'sort_by(.CreatedAt)[]' <(docker images --format '{{json .}}') | jq .
# returns json array of image repositories
function list_image_repos() {
jq -c --slurp --raw-input 'split("\n")[:-1]' <(docker images --format '{{.Repository}}' | sort | uniq)
}
@rsperl
rsperl / find_command.sh
Last active July 25, 2022 15:18
find command #snippet
# Find files that have been modified on your system in the past 60 minutes
find / -mmin 60 -type f
# Find all files larger than 20M
find / -type f -size +20M
# Find duplicate files (based on MD5 hash)
find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33
# Change permission only for files
@rsperl
rsperl / strace.sh
Last active July 25, 2022 15:13
strace #snippet
# Track child process
strace -f -p $(pidof glusterfsd)
# Track process after 30 seconds
timeout 30 strace $(< /var/run/zabbix/zabbix_agentd.pid)
# Track child process and redirect output to a file
ps auxw | grep 'sbin/[a]pache' | awk '{print " -p " $2}' | xargs strace -o /tmp/strace-apache-proc.out
# Track the open request of a network port
@rsperl
rsperl / diff.sh
Last active July 25, 2022 15:14
diff #snippet
# Compare two directory trees
diff <(cd directory1 && find | sort) <(cd directory2 && find | sort)
@rsperl
rsperl / openssl.sh
Last active July 25, 2022 13:09
openssl #snippet
# Testing connection to remote host
echo | openssl s_client -connect google.com:443 -showcerts
# Testing connection to remote host (with SNI support)
echo | openssl s_client -showcerts -servername google.com -connect google.com:443
# Testing connection to remote host with specific ssl version
openssl s_client -tls1_2 -connect google.com:443
# Testing connection to remote host with specific ssl cipher