TL;DR
I would like to extract the Provider, range Start, range End and the name of the provider from the output of whois
in an automated way, but that output varies by provider, so I would like help extracting those three pieces of information for any provider.
Details
I've got a list of IPs. I want to check their provider for each IP with the help of whois for my script and want to pipe it to a Database using mysql.
I want to fill a mySQL Table with the ipranges of the provider but only if the Range of the Provider is already there.
i.e. IP=187.187.187.187
whois $IP
and then get the Providername and the Range of segment but only if i don't already have it in my Table
I got a plan that it should look something like this:
function ip2dec ...
function dec2ip ...
function awhois (){ ...
THEPROVIDER=$(whois $1 | grep PROVIDER) #<- i don't know how to grep
THERANGESTART=$(whois $1 | grep START) #<- i don't know how to grep
THERANGEENDING=$(whois $1 | grep END) #<- i don't know how to grep
}
while read line; do
DECIP=`ip2dec $line`
if [[ ! $(mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e "select iprangestart, iprangeend from $DATABASE.$TABLE where $DECIP BETWEEN iprangestart and iprangeend" 2>/dev/null) ]];
then
awhois $line
mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e
"INSERT INTO $DATABASE.$TABLE (iprangestart, iprangeend, provider)
VALUES ( \"$THERANGESTART\", \"$THERANGEENDING\", \"$THEPROVIDER\")" 2>/dev/null
fi
done < data/allips
But i don't really know how to grep the provider and the range since it has a different pattern from provider to provider