From stacky wiki
Jump to: navigation, search

Here are some scripts I've written for various purposes.

While at Berkeley, I would use this following script to log in to AirBears (the wireless network).

# Replace USERNAME with your username and PASSWORD with your password.
# You may need to install the python mechanize module. The command
# sudo apt-get install python-mechanize
# might do the trick

    from mechanize import Browser
    import re
    br = Browser()"")
    page = br.submit().read()
    errors = re.findall('<h2 id="status" class="error">[^<]*</h2>',page)
    if len(errors)==0:
        print "Logged into AirBears (probably)."
        for error in errors: print error[30:-5]
except IOError, e: print "Couldn't connect to wireless login page:\n", e
except KeyboardInterrupt: print "Ouch! Ok, quitting."


A modified version of Harald Hackenberg's unpnup bash script for converting two-page-per-page pdfs into one-page-per-page pdfs.

I've stopped using this script in favor of the awesome program Briss.

#script:	unpnup
#		This script reformats pdf files where two portait pages 
#		are joined on one landscape page (like pnup does). After
#		completion each page is on one single fullframe page
# 		Copyright (c) 2007 F5 GmbH
# Author: Harald Hackenberg <>
#programs required:
#		   pdftk, pdftops, poster, epstopdf
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See for
# the full text of the license.
# modified by Anton Geraschenko, 03-2009

if [ $# != 1 ] || [[ "$1" != *".pdf" ]]; then
	echo "Usage: `basename $0` FILE.pdf"
	tmpdir=`mktemp -d`
	echo "bursting $1"
	pdftk "$1" burst output $tmpdir/pg_%04d.pdf
	cd $tmpdir
	echo -n "converting "
	for file in pg*.pdf; do
	## if this loop runs into troublesome pages, try pdftops 
        ## then ps2pdf then pdftops rather than just pdftops
		echo -n "${file%.pdf} "
		pdftops -eps $file
		poster -pA4 -mA5 -c0% ${file%pdf}eps > ${file%pdf}tps
		epstopdf --outfile=x$file ${file%pdf}tps | grep -i error
	echo ""
	cd - > /dev/null
	pdftk $tmpdir/xpg*.pdf cat output "${1%.pdf}_unpnuped.pdf"
	rm -r $tmpdir


A bash script I use for converting djvu to pdf so that I can read the document on a kindle or iLiad.

# djvu2pdf script by Anton Geraschenko 2009; use at your own risk.
# requires djvulibre-bin and libtiff-tools
# "bug": Blank pages get removed. You can tell this happened
#        if you see the message "ddjvu: Cannot render image"

# exit codes

echo "Usage: `basename $0` [OPTIONS] FILE.djvu
Convert a djvu file into a pdf.

-b		Keep blank pages. Not yet implemented.
-o OUTFILE	Set the output file to OUTFILE.
-q		Quiet mode."

clean_up ()
[ $QUIET ] || echo "cleaning up"
if [ -e $tmpdir ]; then
	rm $tmpdir/pg[0-9]*.*
	rmdir $tmpdir

while getopts "o:qb" OPTION ; 
    case "$OPTION" in
	b) echo "Sorry, the -b option is not yet implemented. Blank pages will be stripped out." ;;
        o) OUTFILE="$OPTARG" ;;
        q) QUIET=1
	   set +v ;;
        *) USAGE 1>&2 
           exit $X_BAD_OPTION ;;
shift $(($OPTIND - 1))

if [ $# != 1 ] || [[ "$1" != *.djvu ]]; then
	USAGE 1>&2
	exit 1
	trap 'clean_up; exit $X_USER_INTERRUPT' SIGINT
	tmpdir=`mktemp -d`
	pages=`djvudump "$1" | head -n2 | tail -n1 | sed -e 's/^.*files \([0-9]*\) pages)/\1/'`
	[ $QUIET ] || echo -n "converting page"
	for i in `seq -w $pages`; do
		[ $QUIET ] || echo -n " $i"
		ddjvu -format=tiff -page=$i "$1" $tmpdir/pg$i.tiff &&
		tiff2pdf -j -o $tmpdir/pg$i.pdf $tmpdir/pg$i.tiff || [ $?=10 ] || 
		( echo encountered trouble;  exit $X_DDJVU_OR_TIFF2PDF; )
		# ddjvu exits with exit code 10 when the page is blank
		# if any other trouble comes up, exit
	[ $QUIET ] || (echo ;echo -n "creating ${OUTFILE-${1%djvu}pdf}")
	pdftk $tmpdir/pg[0-9]*.pdf cat output "${OUTFILE-${1%djvu}pdf}"

Scripts for managing notes

## tar the files into HalgSource.tgz
tar cfz HalgSource.tgz Halg.tex HalgPreamble.tex HalgBib.bib HalgLec??.tex HalgBibliography.tex

## move HalgSource.tgz to the math servers and execute the mathupdate script
scp HalgSource.tgz $math:/home/u2/grad/anton/public_html/written/Halg
ssh $math bash /home/u2/grad/anton/public_html/written/Halg/

## untar the source
cd /home/u2/grad/anton/public_html/written/Halg/
tar xfz HalgSource.tgz -C ./

## make sure all files are readable (maybe not necessary)
chmod 666 HalgLec??.tex

## uncomment any \input commands that I had commented and uncomment table of contents
## (before I implemented this, people would often tell me that only the most recent lecture is posted)
cp Halg.tex halg.tex
sed -e 's/\% \\input/\\input/g' -e 's/\% \\tableofcontents/\\tableofcontents/g' halg.tex > Halg.tex

## compile and convert to pdf
latex Halg.tex
latex Halg.tex
latex Halg.tex
dvips Halg.dvi -o
ps2pdf Halg.pdf

## get rid of garbage
rm `ls | grep -ve .pdf -ve .sh -ve .tgz -ve .html -ve .sty -ve .txt`


It's ping, but with trimmed output.


ping ${*?"
Usage: `basename $0` [OPTIONS] HOST"} | (trap '' SIGINT; sed -e 's/ttl=[0-9]* time=//' -e 's/.*: icmp_seq=//')

# trap '' SIGINT is there so that if you hit CTRL-C, you still get the summary statistics from ping