The Smurf Script Cookbook

A practical guide to scripting

Tired of textbook examples when you just want a quick tool?  Getting tired of digging through Usenet posts and FAQ's for a single line to solve a problem only to find more posts of people looking for the same answer? 

Here is a collection of scripts based on practical examples.  These are available for public consumption, but they may contain bugs and/or logical errors, as they were mostly meant for special cases rather than general solutions. 

This is a work in progress, and I am by no means an expert.  However, I do believe in learning by example, and most of these scripts have required significant research to implement.  I'll add more as I find/write them.

Optimization tips are always welcome.  If you have a comment or script you'd like to add, send me a mail at vorbau [at] smurf.to.  Unfortunately, I cannot help you with your script errors.  That's why debugging was invented.



Perl: Specialized Perl Scripts


Name Category Description
acct_check.pl
InfoSec
Lightweight account checker that does basic account protection and checksums.
ceasar_brute.pl
RegEx / Crypto
Brute force algorithm for a ceasar cipher (I think).  Didn't work, anyway.  Uses eval for dynamic translations
core_find.pl
SysAd
Filename globbing example of how to search a filesystem quickly.  Adds some accounting for nuking core files.
count.pl
Random
A counting script.  Use to generate number lists for things ( pr0n harvesting)

db_update.pl
Database
Monster table update driven by Perl.  Uses DBI and Oracle DBD.
fortune.pl
CGI
Cheezy CGI script to output fortunes.
greedy.pl
Random
I know I said these were practical scripts, but this one has a good example for sorting keys in a hash.
hours.pl
RegEx
Count some hours from a file so you know when you can skip out of work early.  Does some in-line code with RegEx that's available in newer Perl.
login_totals.pl
SysAd
Do some login accounting for your users and make them pay (in blood)!
mail_extract.pl
SysAd
Extracts a single message from an mbox format file.  Good for retrieving messages form backup.
mail_year_extract.pl
SysAd / RegEx
Same as above, but faster than below.  Uses Perl's Loop idioms and some dynamic RegEx.
mbox_year.pl
SysAd / RegEx
Slower than above by a bit, due to ugly file seeking.  The seek calculations didn't actually work, so it became really sloooow.
mail_check.pl
Random
Parses your inbox and gives you a nice listing of your messages.  I don't think I actually wrote this one, but it's got some ugly-as-butt RegEx.
mp3search.pl
CGI
Search your mp3s!
nslookup.pl
CGI
Cheezy nslookup gateway.
passwd_check.pl
Security
Lightweight password checker.  Works like anlpasswd, or similar utils.  Does dictionary cracks and more complicated single password rules (like l33t speak and such).  Has a configuration file too!
procmail_find.pl
SysAd
Cron job to search users' homedirs for procmail logs.  Used in conjunction with the Procmail Sanitizer.
progress.pl
Misc
Simple twiddle/progress bar program.  Calculates current task progress and prints the percentage and a progress bar that has a twiddle.
scanner.pl
Security / Databases
NMAP to DB script.  Include it in a nightly cronjob, or something.
strip_ws.pl
Random
See the one-liner below.  Use with your WM's "Select" function, ie:
"mozilla -remote \"openURL(`~/bin/strip_ws.pl \\\\\\\"%s\\\\\\\"`, new-window)\"
url-strip.pl
Random
Strips URLs from HTML files.  Works best with count.pl above and wget.  NAMSAYN?

Shell: Mity-might tools


Name Category Description
Xswitch.sh
bourne
System
Switch your X configs on the fly.  This was before I RTFM'd. 
backup.sh
bash
SysAd
Simple backup script that reports errors to the given email address.  Meant to be run from cron.  Uses a bash-specific function.
bindver.sh
bourne
System
Check the BIND version on that nameserver.  Skr1pt k1dd13z ahoy!
bored.sh
bourne
Random
Yep.  For the truely bored.  Run a command at a given interval indefinitely.
cdburn.sh
bourne
System
Burn all the files in the current directory.  Pipes the output from mkisofs to cdrecord and burns.
ipcheck.awk
awk
Random
Check if the IP address passed is valid.  I usually use Awk in-line (see below).
irc.sh
bourne
System
Run your IRC sessions in screen.  Handles dead screens will reconnect you if you've detached.
killg.sh
bourne
System
Kill with grep.  This is meant to be a way to nuke nasty processes that killall can't seem to find.  There's a shortened one-liner below.
search.sh
bourne
Random
Same as the mp3search.pl CGI above, only does it about 10 times slower.  How not to search text files.
testansi.sh
bourne
Random
Test if your TERM is throwing ANSI color or not.  Also shows the ANSI colors.  There's more codes it could do, but that's left to an exercise of the reader.

Inline: Quick One-Liners


I'll add more as I remember them.

Name Category Description
Simple For For Any Occasion
Random
for i in `<command>`; do <another command> $i; done

for i in list1 list2 list3; do <command> $i; done

Simple While
Random
<command> | while read i; do <some stuff> $i; done

y | while read i; do echo "\m/ JEFFK RULEZ \m/"; done
Log Parsing
System Get that spammer's IP addresses:

for i in `grep nobody /var/log/maillog | cut -f8 -d\ `; do echo $i | perl -e '<> =~ /\w+\@\[(\d+\.\d+\.\d+\.\d+)]/; print "$1\n";'; done | sort | uniq
Process Killing
System
Nuke those nasty processes that don't want to die:

kill -9 `ps -ax | grep $1 | grep `whoami` | head -1 | awk '{print $1} '`;
File Renaming
Random
This one is from the UNIX FAQ:

for i in `ls`; do mv $i `echo $i | tr '[A-Z]' '[a-z]'`; done
Whitespace Stripping
Random
Perl In-line:

perl -e '$s=""; while (<>) { s/\s*//g; $s.=$_; } print $s; }'