Skip to content

Instantly share code, notes, and snippets.

@alexcreek
alexcreek / getopts.sh
Last active December 17, 2021 23:40
How to use getopts in bash
#!/bin/bash
usage() {
echo "Usage: $(basename "$0") [-v] [-f filename] [-g group] positional-param"
exit 1
}
if [[ "$#" -lt 1 ]]; then
usage
fi
#!/bin/bash
temp_ruleset=$(mktemp)
nginx_ruleset="# Generated by iptables-save v1.4.21 on Fri Jan 9 18:21:29 2015
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -p icmp -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
@alexcreek
alexcreek / argparse.py
Last active December 18, 2021 02:49
python argparse example
#!/usr/bin/env python3
# https://docs.python.org/3.9/library/argparse.html
import argparse
def parse_arguments():
parser = argparse.ArgumentParser(description='Argument Parser Template')
parser.add_argument('-b', '--basic', help='basic arg consuming option')
parser.add_argument('-v', '--verbose', help='increase output verbosity, flag', action='store_true')
parser.add_argument('-g', '--greedy', help='greedy narg=* arg', nargs='*')
parser.add_argument('-c', '--choices', help='choices demonstration', choices=['list', 'of', 'choices'])
@alexcreek
alexcreek / git.md
Last active December 18, 2021 03:08
git cheat sheet

Show me all commits since the previous tag option 1 - hard mode

git --no-pager log --oneline --no-merges --decorate=no $(git describe --tags --abbrev=0)..

Show me all commits since the previous tag option 2 - ez mode

# with git-extras installed https://github.com/tj/git-extras
# update the changlog format first 
git config changelog.format "%h %s"
@alexcreek
alexcreek / shell-parameter-reference.md
Last active December 18, 2021 03:09
Shell parameter expansion examples

replace()

${parameter/old/new} - replace one occurrence

${parameter//old/new} - replace all occurrences

~ ✘  f=asdf.txt
~ ✘  echo ${f/txt/md}
asdf.md
@alexcreek
alexcreek / datetime.md
Last active April 1, 2022 05:24
python datetime examples

https://docs.python.org/3/library/datetime.html

from datetime import datetime as dt

In [119]: dt.now()
Out[119]: datetime.datetime(2021, 12, 17, 23, 29, 32, 946086)

In [180]: dt.now(timezone.utc)
Out[180]: datetime.datetime(2021, 12, 18, 4, 51, 10, 734244, tzinfo=datetime.timezone.utc)
@alexcreek
alexcreek / renew-token.py
Last active June 23, 2022 01:14
Use your existing tdameritrade refresh_token to create a new refresh_token valid for another 90 days
#!/usr/bin/env python3
# https://developer.tdameritrade.com/content/authentication-faq
from requests import post
import os
import sys
try:
refresh_token = os.environ['REFRESH_TOKEN']
client_id = os.environ['CLIENT_ID']
@alexcreek
alexcreek / openssl.md
Last active August 19, 2022 06:46
OpenSSL cheat sheet

Creating

Create a csr and key

openssl req -new -out fqdn.csr -newkey rsa:2048 -nodes -keyout fqdn.key -subj /C=US/ST=State/L=City/O=Company/CN=fqdn

Create a self signed cert and key

openssl req -x509 -days 365 -out fqdn.crt -newkey rsa:2048 -nodes -keyout fqdn.key -subj /C=US/ST=State/L=City/O=Company/CN=fqdn
@alexcreek
alexcreek / get-exit-nodes.sh
Last active March 5, 2023 00:29
Download a list of tor exit nodes from torproject.org
#!/bin/bash
curl -s https://check.torproject.org/exit-addresses | awk '/ExitAddress/ {print $2}' | sort -n
@alexcreek
alexcreek / backup.sh
Last active May 18, 2023 13:59
simple backup script using rsync + tar
#!/bin/bash
TODAY=$(date +%Y%m%d)
TARGETS=( '/etc' '/home' '/root' '/var/www' )
BACKUP_ROOT='/backups'
BACKUP_DIR="${BACKUP_ROOT}/${TODAY}"
echo "$(date +%D" "%r): Beginning backup"
mkdir -p $BACKUP_DIR
for i in ${TARGETS[@]}; do