Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Geolocate a number of IP addresses via shell script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Geolocate a number of IP addresses via shell script
Authored by: jrobert on Jan 20, '10 08:44:47AM
Very useful. This version (only the first 'if' test is changed) will keep it from tripping over itself if no args are typed and make no args synonymous with the -h flag:
#! /bin/sh

if test $# = 0 || test "$1" = "-h"
then
    echo 'Usage: geoloc [[-f file] | [IP list]]'
    echo '(reads list of IP numbers from STDIN if no argument)'
    exit 0
fi

if test $# = 1
then
    curl http://www.geoiptool.com/en/?IP=$1 2>/dev/null | awk '
/<td.*>(Host Name|IP Address|Country|Region|City|Postal|Calling|Longitude|Latitude)/ {
record="t";gsub("[\t ]*<[^>]*>",""); printf("%-13s ",$0);next;
}
record == "t" { gsub("[\t ]*<[^>]*>[\t ]*","");print $0;record="f";next}
{next}
END{print ""}
'
else
    if test $# = 0;then flist=`cat`
    elif test "$1" = -f; then flist=`cat $2`
    else flist=$*
    fi
    for i in $flist
    do
	sh $0 $i
    done
fi
exit


[ Reply to This | # ]