Skip to content

Instantly share code, notes, and snippets.

View princebot's full-sized avatar

princebot princebot

View GitHub Profile
#!/usr/bin/env bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Actions: Hop through the [redacted] NAT box to SSH into [redacted] servers.
# Globals: None.
# Options: --key, -k, -i PRIVATE_KEY
# Use this key for authentication (default: standard SSH key
# locations [see man page ssh (1)]
# --debug, -d
# Run in debugger mode: set shell options -x and -v, and run
# ssh in verbose mode with -vvv
# Bash shell function that runs "docker inspect" and pipes the output through jq.
# Requires jq [https://stedolan.github.io/jq/]
#
# Usage: di CONTAINER|IMAGE
di() (
error=1
__obj=$1
if [[ ! ${__obj} ]]; then
echo "${FUNCNAME}(): error: missing docker object argument" >&2
# Bash function for updating all packages installed with either `pip` or
# `conda` in the current Python virtualenv or conda environment.
pyupdate() {
# If we're in a conda environment, update all packages conda installed.
which conda 2>/dev/null && conda update -y --all
# After we're done with conda, if we have pip, update pip then update any
# packages installed with pip.
if which pip 2>/dev/null; then
pip install -U pip
@princebot
princebot / cat.js
Last active November 20, 2015 02:55
#!/usr/bin/env node
/* GNU utility `cat` as a JavaScript executable.
*
* This works mostly the same as `cat`, with one difference: an explicit
* argument of '-' (specifying stdin) is processed _after_ all filename
* arguments.
*
* USAGE: cat.js [<FILE> <FILE...> [-]]
*/
#!/usr/bin/env python
"""
Extract and print network addresses from a file.
This was written for Python 3.x because Python 2.x is a decade old, and every
time you use it, Baby Guido cries.
If you have an older python, download the Miniconda Python installer at
http://conda.pydata.org/miniconda.html, run it, and load a new shell.
#!/usr/bin/env python
"""
Open several browser tabs displaying information about an IP passed as a CLI arg.
(extremely quick, perversely dirty)
"""
from string import Template
import subprocess
import sys
@princebot
princebot / getpass.go
Created October 9, 2016 08:14
Read passwords from stdin with tty echo disabled.
// Package getpass reads passwords without echoing output.
//
// The current implementation is compatible with modern Windows and *nix systems
// that use cmd.exe, PowerShell, Bash, or Bourne shell environments.
package getpass
import (
"fmt"
"os"
@princebot
princebot / random.bash
Created October 24, 2016 02:11
Generate a random string of characters with Bash.
# This reads a stream of random bytes from the system and drops input that isn’t
# a valid encoding for a letter or a number. It stops after 8 characters.
#
# Setting LC_CTYPE=C is important: Modern systems will (hopefully) be set to
# UTF-8, but that makes tr throw errors for illegal byte sequences. Usually, we
# want that kind of sane behavior — but here, we don’t.
LC_CTYPE=C tr -d -c '[:alnum:]' </dev/urandom | head -c 8
# This saves a 15-character alphanumeric string in a variable:
random=$(LC_CTYPE=C tr -d -c '[:alnum:]' </dev/urandom | head -c 15)
@princebot
princebot / pre-commit
Created October 28, 2016 18:00
(bash) A Git pre-commit hook that rejects commits to Ansible projects that contain unencrypted Ansible Vault files.
#!/usr/bin/env bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Name: pre-commit
# Author: prince@princebot.com
# Synopsis: Reject commits containing unencrypted Ansible Vault files.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set -e
vault_files=($(IFS=$'\n' find . -type f -name 'vault'))
@princebot
princebot / pre-commit
Last active October 28, 2016 18:32
(python) A Git pre-commit hook that rejects commits to Ansible projects that contain unencrypted Ansible Vault files.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Reject commits to Ansible projects that contain unencrypted vault files.
I realized pretty quick that a simple shell script could so the same thing (see
https://gist.github.com/princebot/2bd84b3b344168db22ce1259241f4a88) — but I
finished this anyway for funsies.
Also for funsies, I wanted to see what static classes in Python might look like.