Skip to content

Instantly share code, notes, and snippets.

@putnamhill
putnamhill / jq-snippets.sh
Last active January 13, 2018 16:21
collection of miscellaneous jq snippets
# print value pairs from an array in lower case
jq --raw-output '.[] | "\(.key_1 | ascii_downcase) \(.key_2 | ascii_downcase)"'
# convert `key=value` lines into `{"key":"value"}` json
jq --raw-input 'split("=") | {(.[0]):.[1]}' some.conf | jq --slurp 'add'
# validate json (bad)
echo '{hello: "there"}' | jq . >/dev/null 2>&1 && echo good json || echo bad json
bad json
@putnamhill
putnamhill / private-ip.pl
Last active December 14, 2017 15:02
Reads ip addresses from the command line or stdin and prints them if they belong to any of the three RFC1918 private address blocks
#!/usr/bin/perl -w
use Net::CIDR;
use Getopt::Long;
# Net::CIDR one liner:
# perl -mNet::CIDR -ne 'print if Net::CIDR::cidrlookup($_, ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"))'
#
# TODO: add ip address validation
# Use awk to to redirect docker build output to stderr for visual feedback
# while capturing the image id on stdout when the build is sucessfull
ID=$(docker build . | awk '{print > "/dev/stderr"} END {if (/^Successfully built /) print $NF; else exit 1}')
# given docker volume path, return local storage path
doc2loc(){ docker inspect --format "{{range .Mounts}}{{if eq .Destination \"$1\"}}{{.Source}}{{end}}{{end}}" $2;}
@putnamhill
putnamhill / docker-rmi-interactive.sh
Last active January 4, 2024 13:40
A script to help clean up docker images interactively
#!/bin/bash
agree() {
local question="$1"
while true; do
read -p "$question " answer
case $answer in
[Yy]|[Yy][Ee][Ss]) return 0 ;;
[Nn]|[Nn][Oo]|'') return 1 ;; # empty string is default
@putnamhill
putnamhill / ontouchdo
Last active April 12, 2017 16:25
Run a command each time a file is modified. For example: validate an html file automatically each time it's saved.
#!/bin/sh
# sample usage:
# ontouchdo malformed.html 'clear; xmllint --noout --valid malformed.html 2>&1 | head -n20'
#
# Now fix some things in malformed.html and save. See the remaining errors in real time. Ctrl-c to quit.
[ ! -r "$1" ] && echo 'Nothing to watch.' && exit 1
[ ! -n "$2" ] && echo 'Nothing to do.' && exit 1
@putnamhill
putnamhill / opendkim.md
Last active August 29, 2015 13:57
Setting up opendkim on ubuntu running postfix.

configuring opendkim with postfix on ubuntu

Replace example.com with your domain. All occurrences of the string 'mail' are know as the selector and can be any alpha numeric string.

Install opendkim:

$ sudo apt-get install opendkim opendkim-tools
@putnamhill
putnamhill / group-dupes.pl
Last active June 19, 2021 14:16
Print groups of files that are duplicates.
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Digest::MD5;
#use diagnostics;
my $minimum = 0;
my $header = '';
@putnamhill
putnamhill / stop-watch.sh
Created July 31, 2013 20:04
A simple bash stop watch.
#!/bin/bash
START=$(date +%s)
read -s -n1 -p $'Press a key when done.\n'
ELAPSED_SECONDS=$(bc <<< "scale=10; ($(date +%s) - $START)")
if [ $ELAPSED_SECONDS -gt 60 ]; then
ELAPSED_MINUTES=$((ELAPSED_SECONDS / 60))
ELAPSED_SECONDS=$((ELAPSED_SECONDS - ELAPSED_MINUTES * 60))
echo -n "$ELAPSED_MINUTES minutes "
fi
@putnamhill
putnamhill / mysql-notes
Last active September 29, 2016 11:45
Creating a new user with the mysql client and set some privileges for all the tables of a database.
CREATE USER 'someuser'@'localhost' IDENTIFIED BY 'cleartext-password';
GRANT SELECT,INSERT,UPDATE,DELETE ON `database`.* TO 'someuser'@'localhost';
@putnamhill
putnamhill / smtp-test
Last active December 18, 2015 12:19
smtp commands to test email between two servers via telnet
telnet smtp.example.com 25
Trying 203.0.113.1...
Connected to smtp.example.com.
Escape character is '^]'.
220 example.com running My Bitchin Mail Server 1.0.0
HELO example.org
250 example.com hello example.org (198.51.100.1)
MAIL FROM:<postmaster@example.org>
250 2.1.5 sender OK
RCPT TO:<someone@example.com>