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: mdfischer on Jan 14, '10 09:31:08AM

A bit more involved than some the elegant one liners, but handles entries from stdin, a file or the command arguments, and formats the results a bit better.

<snip>
#! /bin/sh

if 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

This script uses www.geoiptool.com to look up geolocation information for
IP numbers. You can suppress fields by removing them from the line
Host Name|IP Address|Country|Region|City|Postal|Calling|Longitude|Latitude
in Line 5 above.

I have named the command file geoloc. You can name it whatever. Syntax is

geoloc [[-f file] | IP list]]
(reads list of IP numbers from STDIN if no argument)

It should work on any standard Unix or Linux system with curl installed
as well as Mac OS X which includes curl in System 10.5. You will have to download
and install curl if you do not have it. You can find binaries for most systems at
http://curl.haxx.se/download.html
</snip>



[ Reply to This | # ]
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 | # ]