Scripts: Difference between revisions

From stacky wiki
No edit summary
m (Reverted edits by 123.64.246.247 (talk) to last revision by Anton)
Line 30: Line 30:
except KeyboardInterrupt: print "Ouch! Ok, quitting."
except KeyboardInterrupt: print "Ouch! Ok, quitting."
</pre></code>
</pre></code>
'''Source(s):'''  [http://www.downloadranking.com  Scripts solutions]


=unpnup=
=unpnup=

Revision as of 06:18, 16 November 2012

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

AirBearsLogin.py

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

#!/usr/bin/python
#
# 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

try:
    from mechanize import Browser
    import re
    br = Browser()
    br.open("https://auth.berkeley.edu/cas/login?service=https%3a%2f%2fwlan.berkeley.edu%2fcgi-bin%2flogin%2fcalnet.cgi%3fsubmit%3dCalNet")
    br.select_form(nr=0)
    br["username"]="USERNAME"
    br["password"]="PASSWORD"
    page = br.submit().read()
    errors = re.findall('<h2 id="status" class="error">[^<]*</h2>',page)
    if len(errors)==0:
        print "Logged into AirBears (probably)."
    else:
        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."

unpnup

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.

#!/bin/bash
#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 <hackenberggmx.at>
#
#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 http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.
#
# modified by Anton Geraschenko, 03-2009

if [ $# != 1 ] || [[ "$1" != *".pdf" ]]; then
	echo "Usage: `basename $0` FILE.pdf"
else
	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
	done
	echo ""
	cd - > /dev/null
	pdftk $tmpdir/xpg*.pdf cat output "${1%.pdf}_unpnuped.pdf"
	rm -r $tmpdir
fi

djvu2pdf

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

#!/bin/bash
# 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
X_DDJVU_OR_TIFF2PDF=1
X_BAD_OPTION=2	
X_USER_INTERRUPT=13

USAGE () 
{
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 ()
{
echo
[ $QUIET ] || echo "cleaning up"
if [ -e $tmpdir ]; then
	rm $tmpdir/pg[0-9]*.*
	rmdir $tmpdir
fi
}

while getopts "o:qb" OPTION ; 
do
    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 ;;
    esac
done
shift $(($OPTIND - 1))

if [ $# != 1 ] || [[ "$1" != *.djvu ]]; then
	USAGE 1>&2
	exit 1
else
	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
	done
	[ $QUIET ] || (echo ;echo -n "creating ${OUTFILE-${1%djvu}pdf}")
	pdftk $tmpdir/pg[0-9]*.pdf cat output "${OUTFILE-${1%djvu}pdf}"
	clean_up
fi

Scripts for managing notes

update.sh

#!/bin/bash
#
## 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/mathupdate.sh

mathupdate.sh

## 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 Halg.ps
ps2pdf Halg.ps Halg.pdf

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

pinq

It's ping, but with trimmed output.

#!/bin/bash

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