Skip to content

Instantly share code, notes, and snippets.

View rosenhouse's full-sized avatar

Gabe Rosenhouse rosenhouse

View GitHub Profile
echo "Connecting to bastion via SSH..."
ssh -nNf bastion
echo "Connecting to MySQL..."
mysql -ugabe -pPaS$w0rD -h127.0.0.1 my_database
echo "MySQL client terminated, closing SSH connection..."
ssh -O exit bastion
echo "Done."
@rosenhouse
rosenhouse / ve.sh
Last active December 27, 2015 16:49
python virtualenv for the lazy
#!/bin/bash -e
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
echo "source this!"
exit 1
fi
if [ ! "$1" ]; then
echo "Name an environment!"
return 2
@rosenhouse
rosenhouse / extract-aws-creds.py
Created December 10, 2013 04:45
Extract temporary AWS credentials that an EC2 instance has assumed.
import sys
import json
import urllib
def print_creds(iam_role_name):
'''
Print out the current AWS credentials that this EC2 instance
has been assigned, for a IAM role it has assumed.
Note that credentials are rotated frequently
@rosenhouse
rosenhouse / s3_delete_recurisve.py
Last active December 12, 2018 19:27
Recursive delete on S3
import boto
def delete_recursive(bucketname, keyprefix):
'''
Recusively delete all keys with given prefix from the named bucket
Stolen from http://stackoverflow.com/a/10055320/141084
'''
s3 = boto.connect_s3()
bucket = s3.get_bucket(bucketname, validate=False)
bucketListResultSet = bucket.list(prefix=keyprefix)
@rosenhouse
rosenhouse / s3_rr_sync.sh
Last active January 1, 2016 09:39
Reduced redundancy sync to S3
#!/bin/bash -e
SRC="$1"
DEST="$2"
echo "Starting reduced-redundancy sync of $SRC --> $DEST"
# would prefer to use this tool, but we get errors: https://github.com/aws/aws-cli/issues/401
# aws --region us-west-2 s3 sync --storage-class REDUCED_REDUNDANCY $SRC $DEST
@rosenhouse
rosenhouse / time-ago.py
Last active June 20, 2024 03:07
Python time-ago
def time_ago(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
Modified from: http://stackoverflow.com/a/1551394/141084
"""
now = datetime.utcnow()
if type(time) is int:
@rosenhouse
rosenhouse / stream_hash.py
Created May 14, 2014 04:17
Python stream hasher
def local_file_hash(filepath):
'''
Return a hash of the file's contents, if file exists
Otherwise return None
'''
hasher = hashlib.sha256()
blocksize=(1 << 16)
try:
with open(filepath, "rb") as afile:
buf = afile.read(blocksize)
@rosenhouse
rosenhouse / dd.py
Created May 14, 2014 06:47 — forked from hikoz/dd.py
#!/usr/bin/env python
import sys
import time
import signal
from subprocess import Popen, PIPE
dd = Popen(['dd'] + sys.argv[1:], stderr=PIPE)
while dd.poll() is None:
time.sleep(.3)
dd.send_signal(signal.SIGUSR1)
@rosenhouse
rosenhouse / sd-backup.sh
Created May 19, 2014 23:57
MacOS SD card backup and restore
#!/bin/bash -e
# simple script to automate backup of SD cards to a file on disk
# only works on Mac OS at this time.
if [ -z "$1" ]; then
echo "Specify an img name to save to"
exit
fi
@rosenhouse
rosenhouse / qr_label.py
Created June 12, 2014 05:34
Put a QR code in a PDF sized for our label maker
#!/usr/bin/env python2.7
import tempfile
from reportlab.graphics import renderPDF
from reportlab.pdfgen import canvas
import qrcode
import qrcode.image.svg
from svglib.svglib import svg2rlg