Scripts
Like most Linux users, I've accumulated several short but useful scripts over time. Here are the ones that might be of interest to others.
autolatex.sh
This is a simple script originally intended for monitoring several LaTeX source files and rebuilding and re-displaying the output whenever a file changes. It probably isn't the best script for the job, but it is also an example of how you can use inotifywait in Bash scripting to monitor files.
sshroute.py
sshroute.py is a simple tool for creating SSH tunnel scripts that use a number of intermediate servers. If you need SSH tunnels to access certain resources and must do it via another machine, you will probably end up with scripts like the following:
ssh -L80:localhost:60080 user@host1 -t "ssh -L60080:localhost:80 user@host2"
As you can imagine, if forwarding many ports or going through many hosts, writing these can get complicated and tedious. sshroute.py does this for you.
sshroute.py [PORTS] [HOSTS] [OPTIONS] DESTINATION
Ports are of the format -l start:intermediate:dest or -r start:intermediate:dest for “local” and “remote” tunnels respectively—these correspond to the -L and -R for ssh itself. start is the port number for the endpoint which connections are going to be made to, and dest is the port number for the final destination of the connection. intermediate is the port number used on all machines in between the local host and the DESTINATON.
Hosts are of the format -h [username@]hostname[:port] (the username and port parts are optional). All hosts except for the destination should be specified with -h options. The destination follows the same format.
The only other option supported at the moment is -s, which adds the #!/bin/bash -i shebang to the beginning of the output. This way, you can redirect the output to a file and use it as a script.
sshroute.py -l80:60080:80 -h host1 -h host2 user@host3:2222 -s > httpforward.sh chmod +x httpforward.sh ./httpforward.sh
apache2-gencert.sh
Generates a self-signed SSL certificate for a given domain name, puts it in /etc/apache2/ssl/ and displays the 2 lines needed for the Apache2 config file to use it.
backup-run.sh
Simple backup script which can be put in a cron job. Edit DIRS variable to set the directories you want to backup (look at the examples for syntax) and BACKUP_HOST for scp backups. To do non-interactive scp backups you will need to make an SSH key with no passphrase and add it to the allowed keys of your backup user on the remote server. Not hugely efficient, you will need enough space on your /tmp to store the compressed copies of the target dirs temporarily.
backup-clean.sh
Probably the only useful bit of Perl I have ever written… Used for keeping on the last “n” backups of each type, based on filenames like those generated by backup-run.sh, i.e. hostname-somedir-2007-01-07-0801.tar.bz2. Can be used in a cron-job, useful to run after/before something like backup-run.sh. Usage is backup-clean.pl DIR [ COUNT ] where DIR is the path to the directory containing the backups and COUNT is an optional parameter specifying the number of backups to keep (1 by default).
svnrollback
A very quick script I wrote to rollback a particular file in a svn working copy to a previous revision. Currently has next to no validation, would be nice to write it properly in Python so I can use the XML output of svn info, but I use Git now anyway.