Monday, April 07, 2008

Geek post

I'm having a problem at work. Students using a ADSL2+ with TPG (an ISP) are experiencing slow connections when downloading recordings (pod casts) of their lectures. Some students have submitted problem tickets to the database we maintain to help diagnose exactly this type of problem.

I need to provide TPG with details of the students who have this problem to help them sort out what is going wrong within their network. When the students submit their problem we record their IP address. Anyway, here is a useful little unix bash shell script I wrote which will read through a list of ip addresses and then do a whois lookup on each one and will print out all the ones from a particular provider.



#!/bin/bash
# script that iterates through a list of ip's in a file provided through stdin
# does a whois look up on each of them and then prints out all the ones with
# a particular ISP - in this case TPG
#
#

FILE="" # variable to hold the file name
if [ "$1" == "" ]; then # if there is no file as an arg
FILE="/dev/stdin" # then get it from stdin
else
FILE="$1"
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not be read"
exit 2
fi
fi



cat $FILE | while read LINE # pass the file line by line
do
ISP=`whois $LINE | grep netname | awk '{print $2}'`
if [ "$ISP" == "TPG-AU" ]; then
echo "$LINE"
sleep 2
fi
done


I always find examples like this found on other peoples blogs useful when searching the web for snipits of tested code.

1 comment:

Keiran said...

Talk about role reversal.

Here you are writing BASH scripts to sniff IP addresses and do whois lookups.

Here I am writing PowerShell scripts to perform MS SQL Log Shipping.