Skip to content

Instantly share code, notes, and snippets.

@shichao-an
shichao-an / prt.sh
Created August 22, 2014 04:29
Print routing table with netstat on FreeBSD/OS X
#!/bin/bash
netstat -nr -f inet
@shichao-an
shichao-an / pln.sh
Created August 25, 2014 22:18
Print LAN nodes
#!/bin/bash
# Works on OS X
ping -c 2 -i 2 255.255.255.255 | awk ' $4 ~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/{print $4}' \
| sed 's/\(.*\):/\1/g' | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4
@shichao-an
shichao-an / ebs_lvm.sh
Created August 26, 2014 05:21
Create LVM on EBS
#!/bin/bash
ebs_disk="/dev/xvdf"
ebs_partition="/dev/xvdf1"
vg_name="vg"
pv_name="pv1"
mount_point="/mnt"
# Create a primary partition on $ebs_disk using all space
@shichao-an
shichao-an / purge_mysql.sh
Created August 28, 2014 17:33
Purge MySQL on Ubuntu
#!/bin/bash
dpkg -l | grep mysql | awk '{ print $2 }' | xargs -I {} apt-get purge -y {}
@shichao-an
shichao-an / comp1.sh
Created August 28, 2014 18:22
Compare the first column by tab as field separator of the two files
#!/bin/bash
diff <(awk -v FS='\t' '{ print $1 }' a1.txt) <(awk -v FS='\t' '{ print $1 }' a2.txt)
@shichao-an
shichao-an / create_gitmodules.sh
Last active August 29, 2015 14:06
Create .gitmodules from a list of GitHub repo URLs
cat bundle.txt | xargs -I {} bash -c 'name={}; printf "[submodule \".vim/bundle/%s\"]\n\tpath = .vim/bundle/%s\n\turl = %s\n\n" ${name##*/} ${name##*/} $name' > .gitmodules
@shichao-an
shichao-an / list_at_jobs.sh
Last active August 29, 2015 14:06
List one-liner commands in "at" job queue
#!/bin/bash
# Result:
# job_id date command
atq | awk '{ printf("%s %s %s %s %s ", $1, $5, $3, $4, $6); \
system("at -c "$1"| tail -2 | head -1"); }'
@shichao-an
shichao-an / escape_bre.sh
Created September 8, 2014 18:44
Escape string for BRE
#!/bin/sh
escape() {
local raw="$1"
local quoted=$(printf %s "$raw" | sed 's/[][()\.^$?*+]/\\&/g')
echo "$quoted"
return 0
}
@shichao-an
shichao-an / request_exceptions.py
Created September 10, 2014 17:50
Custom exception example from requests
class RequestException(IOError):
"""There was an ambiguous exception that occurred while handling your
request."""
def __init__(self, *args, **kwargs):
"""
Initialize RequestException with `request` and `response` objects.
"""
response = kwargs.pop('response', None)
self.response = response
@shichao-an
shichao-an / pycurl_download_progress.py
Created September 17, 2014 18:27
PycURL progress
import pycurl
import sys
from humanize import naturalsize
import time
START_TIME = None