I use awk to parse country and network information from whois
.
#!/bin/bash
IP=$1
if out=$(grep $IP /tmp/mygeoip)
then
echo "$out" | awk '{$1="";print}'
exit
fi
if [[ ($IP =~ ^10\.) || ($IP =~ ^192.168\.) || ($IP =~ ^172.16\.) ]]
then
echo "LAN"
exit 0
fi
# __^__ __^__
# ( ___ )-----------( ___ )
# | / | AWK version | \ |
# |___| |___|
# (_____)-----------(_____)
result=$(whois $IP | awk '/country/ {country=$2} /netname/ {netname=$2} END {print country,netname}')
echo $IP $result >> /tmp/mygeoip
echo $result
$ net.ip.geo 192.168.90.238
LAN
$ net.ip.geo 92.247.20.226
BG MTELNET
$ net.ip.geo 129.45.92.28
DZ Optimum-Telecom-Algeria
$
It uses a temporary cache in /tmp/mygeoip
so that query on same IP is looked up in the cache not from whois
.