Skip to content

Instantly share code, notes, and snippets.

@wamsachel
wamsachel / backup_to_s3.sh
Created June 16, 2022 20:30
Simple bash for creating a logball and then uploading to S3; uses awscli where `aws configure` has already been run
#!/usr/bin/env bash
set -e
# example: ./backup_to_s3 [/var/log] [s3-bucket[/sub-dir]]
# log directory
log_dir=${1:-"/var/log"}
# filename for the tarball of all the logs
logball_fname=`date +%FT%H%M`.tgz
# absolute filepath + filename of the tarball
@wamsachel
wamsachel / my_tmux_se.sh
Created June 16, 2022 20:19
Simple bash for attaching to, or starting new, tmux session
#!/bin/bash
# This script will check for tmux session,
# attach to it or create a new one
# For debugging uncomment below
# set -o verbose
USERNAME=${USERNAME:-wamsachel}
TMUX_SESSION="${USERNAME}s_session"
@wamsachel
wamsachel / web_app.py
Last active November 21, 2016 19:15
Simple web_app skeleton with arg/conf parsing
#!/usr/bin/env python3
"""
web_app.py
Author: wamsachel
Date: today
web_app is a simple bottle-drive web app which examines how to better handle internet vitriol,
rather than waiting for lawmakers to do so
Usage: $python3 web_app.py
def to_bytes3(bytes_or_str):
"""Python3 helper return bytes type of bytes_or_str """
if isinstance(bytes_or_str, str):
ret_val = bytes_or_str.encode('utf-8')
else:
ret_val = bytes_or_str
return ret_val
def to_str3(bytes_or_str):
@wamsachel
wamsachel / gen_byte_str
Created October 21, 2016 20:45
Converts a decimal value into a hex byte string
def gen_bytestr(dec_val, byte_str_len=4):
""" takes dec_val, converts to hex and returns byte_str that is byte_str_len long
e.g. gen_bytestr(255, 4) -> '\x00\x00\x00\xff'
"""
# first check if dec_val can even be represented by a byte string of byte_str_len length
byte_count = (int.bit_length(dec_val) / 4)
if byte_count > byte_str_len:
raise Exception('[!] {val} too large for {max} byte length. (requires {req} bytes)'.format(
val=dec_val, max=byte_str_len, req=byte_count))
hex_val = hex(dec_val)
@wamsachel
wamsachel / gist:41712decbcf7542ff7c9a98dea1cd663
Created April 1, 2016 23:18
configure() will step through the options in config.ini and allow the user to change them before the config file is updated
import configparser
CONFIGURATION_FILE = 'CONFIG.ini'
def configure():
""" Step the user through the configuration options """
try:
config = configparser.ConfigParser()
config.read(CONFIGURATION_FILE)
print ('[*] {} read successfully'.format(CONFIGURATION_FILE))
@wamsachel
wamsachel / rm_r.py
Created November 19, 2015 00:44
I wanted a simple way to recursively move through directory tree and remove filenames containing a certain string
#!/usr/bin/env python
import argparse
import os
# Handle command line args
try:
arg_parser = argparse.ArgumentParser(description = 'Will recursively traverse a directory tree,\
and remove all files and directories that match any passed in regular expressions')
arg_parser.add_argument('dir', help = 'dir to start tree traversal process')
@wamsachel
wamsachel / gist:92561357ff5f9f75b33a
Created August 4, 2015 20:04
Hasty python enum (Enum type included in python versions after 3.4)
#Taken from http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python
def enum(**enums):
return type('Enum', (), enums)
Numbers = enum(ONE = 1, TWO = 2, THREE = 'three')
Numbers.ONE
>> 1
@wamsachel
wamsachel / whereami
Created May 5, 2015 17:23
whereami - simply prints out ipv4 addresses from ifconfig commands
For OS X:
alias whereami='ifconfig | grep "inet " | cut -d " " -f 2'
For Linux:
alias whereami='ifconfig | grep "inet addr:" | cut -d : -f 2 | cut -d " " -f 1'