Skip to content

Instantly share code, notes, and snippets.

View angrychimp's full-sized avatar

Randall Kahler angrychimp

View GitHub Profile
# Dumps all files into the same folder
folders=($(ls targets/))
find source/ -type f -print0 | \
xargs -r0 -P 10 -I{} \
mv --target-directory=targets/${folders[$(expr $RANDOM % ${#folders[@]})]}/
@angrychimp
angrychimp / random-dist.sh
Last active January 12, 2017 23:18
Using random variables in a subshell
# As an example, how to randomly distribute files to a collection of folders
find source/ -type f -print0 | \
xargs -r0 -P 10 -I{} sh -c "
folders=($(ls targets/))
mv --target-directory=targets/\${folders[\$(expr \$RANDOM % \${#folders[@]})]}/
"
### Keybase proof
I hereby claim:
* I am angrychimp on github.
* I am angrychimp (https://keybase.io/angrychimp) on keybase.
* I have a public key ASCBtywTAqDTD8u4ALnVWGevAG93Yj6a2VmCzywZjDO8qgo
To claim this, I am signing this object:
@angrychimp
angrychimp / s3-bucket-scan
Last active September 8, 2017 19:05
Scan S3 buckets for public-read permissions
#!/bin/bash
# requires jq: https://stedolan.github.io/jq/
# requires aws-cli: http://docs.aws.amazon.com/cli/latest/userguide/installing.html
for bucket in `aws s3 ls | awk '{print $NF}'`; do
errors=$(expr $(aws s3api get-bucket-acl --bucket $bucket |
jq '.Grants | .[] | if (.Permission == "READ" and (.Grantee.URI == "http://acs.amazonaws.com/groups/global/AuthenticatedUsers" or .Grantee.URI == "http://acs.amazonaws.com/groups/global/AllUsers")) then "ERROR" else null end' |
grep ERROR |
wc -l))
@angrychimp
angrychimp / update-personal-sg.sh
Last active October 9, 2017 16:33
Script to refresh an AWS VPC security group with your local IP address
#!/bin/bash
SGID=sg-123ad456
PROFILE=aws-profile
# Add current IP to ingress list
myip=$(curl -s https://rand.tools/ip/)
aws --profile $PROFILE ec2 authorize-security-group-ingress --dry-run --group-id $SGID --ip-permissions "[{\"IpProtocol\": \"tcp\", \"FromPort\": 22, \"ToPort\": 22, \"IpRanges\": [{\"CidrIp\": \"$myip/32\"}]}]"
# Remove any old IPs from ingress
@angrychimp
angrychimp / make-pkcs12.sh
Created November 13, 2017 15:13
Alias for how to make a PFX file
alias make-pkcs12='domain=$(pwd | xargs basename); openssl pkcs12 -export -inkey $domain.key -in $domain.crt -certfile $domain.ca.crt -out $domain.pfx'
@angrychimp
angrychimp / get-cf-status.py
Last active February 27, 2018 06:59
Gets the status of a CloudFormation stack and refreshes until a terminal state is reached. Run from command line using `python get-cf-status.py [stack-name-or-id]`
from __future__ import print
import boto3
import datetime
import sys
def main():
client = boto3.client('cloudformation')
stack_name = sys.argv[1]
stack = client.describe_stacks(StackName=stack_name)['Stacks'][0]
# refresh while state is not final
@angrychimp
angrychimp / parse-images.sh
Last active March 21, 2018 18:15
An example of how to use a `bash` script to perform actions on AMI records
#!/bin/bash
IFS=$'\n'
for image in $(aws ec2 describe-images --filter "Name=description,Values=Auto backed up on *" --query 'Images[].{Description:Description,Image_ID:ImageId,DateCreated:CreationDate}')
creationDate=$(echo $image | awk -F'\t' '{print $1}')
description=$(echo $image | awk -F'\t' '{print $2}')
imageId=$(echo $image | awk -F'\t' '{print $3}')
echo "$imageId | $creationDate | $description"
done
@angrychimp
angrychimp / raspbian-wifi-boot.md
Created March 22, 2018 22:07
How to get wi-fi and SSH working via image adjustments prior to a first boot.
  1. Download the image
  2. Flash the image to your microSD using Etcher
  3. Unmount/remount the microSD as necessary
  4. For wi-fi, add a file to the root folder named wpa_supplicant.conf
    • Content can be generated using
      read -p "SSID: " ssid
      read -s -p "Passphrase: " passphrase
      psk=$(wpa_passphrase $ssid $passphrase)

echo "country=US

@angrychimp
angrychimp / s3_threaded_delete.py
Created November 7, 2017 18:07
Python3/boto3 multi-threaded S3 object delete
import boto3
import sys
import json
import logging
from threading import Thread
from queue import Queue
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)