shell hacks

I use bash scripting incessantly as a quick-and-dirty way of automating things I’m too lazy to do by hand. It’s here so I can find it more easily. Maybe someday I’ll just learn Perl properly (or maybe use bash’s built-in string processing) instead of forgetting regularly how to use bash, egrep, sed, awk, uniq, diff, and a dozen other commandline tools.

Set field separator to something more sane, so that (for instance) when piping filenames to ‘while read N’ it does not split on spaces in a name:

IFS="$(echo -e "\n\r")"

Convert DOS line endings to UNIX (since I never have fromdos/dos2unix when I need them):

tr -d '\r'

Of all unversioned files in an SVN repository in the current directory, do an ‘svn add’ on any .cpp, .h, or .java files:

svn status | egrep "^?.*.(cpp|h|java)$" | awk '{print $2}' | while read N; do svn add "$N"; done

Look for a class in all JAR files in a directory, recursively: (This is rudimentary; it has the bug that it lists every single JAR and you must find the listing with a class after it.)

find |egrep ".jar$" | while read N; do echo $N; unzip -l "$N" | grep "ClassName"; done > jars.txt

Give a summary of each unique line and how many times it occurs (I always forget that this uniq tool exists…)

sort | uniq -c

Dump incoming packets to console in both hex and ASCII (note that you can also write a dump with -w, and read from this dump with -r rather than listening on an interface):

tcpdump -X -i eth0

Turn off checksum offloading when it’s interfering with some low-level analysis:

ethtool --offload eth0 rx off tx off

Linux only: Use tc to simulate restricted bandwidth and higher latency: (N.B. tc treats ‘mbps’ as megabytes/second and ‘mbit’ as megabits/second!)

IFACE=eth0
BANDWIDTH=10mbit
LATENCY=10ms
tc qdisc add dev $IFACE root handle 1: htb default 12
tc class add dev $IFACE parent 1:1 classid 1:12 htb rate $BANDWIDTH ceil $BANDWIDTH
tc qdisc add dev $IFACE parent 1:12 netem delay $LATENCY

[To jog my memory: Handles are written x:y. x is an integer identifying a qdisc (queuing discipline). y is an integer identifying a class belonging to qdisc x. A qdisc's handle has y=0 (and 1: stands for 1:0 here implicitly), and a class must have a nonzero y value. Handles are local to an interface. A root class has the HTB (hierarchical token bucket) qdisc as its parent.]

To turn that off:

tc qdisc del dev $IFACE off

Try to access a URL at a server using two-way SSL authentication: (of course, you’ll need your public & private key, and the public key for the CA’s cert)

wget --certificate=client.crt --private-key=client.key --ca-certificate=ca.crt https://blahblahblah/blah...

Useful tools whose names I will forget when I need them:

lwp-request - HTTP GET, PUT, POST

Some links that contain helpful commands:

OpenSSL: Creating CAs and using them to create certs

Converting these into PKCS12 or JKS format

Other common OpenSSL commands

Manipulating strings in bash

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>