bash IP whois lookup script

Grimlockz picture Grimlockz · Feb 16, 2012 · Viewed 11.6k times · Source

I have a decent admin script working for checking what IPs are logging on too a web app but I need to make it more fancy with a whois at the start and then I thought a geoip.

At the moment I've hashed out the whois part of it - my problem is that because there are multiple IPs - the whois doesn't know what to do with them

Any ideas on this would be great? and also ideas on geoips would be lovely!

Cheers

#!/bin/bash

#Setting date and time (y and z aren't being used at the moment)
x="$(date +'%d/%b/%Y')"
y="$(date +'%T')"
z="$(date +'%T' | awk 'BEGIN { FS =":"} ; {print $1}')"

#Human readable for email title
emaildate=$(date +"%d%b%Y--Hour--%H")

#Setting date and time for grep and filename
beta="$(date +'%d/%b/%Y:%H')"
sigma="$(date +'%d-%b-%Y-%H')"

#Current SSL Access logs
log='/var/log/apache2/ssl_access.log'
#Set saved log location
newlogs=/home/user/Scripts/logs

grep [email protected] $log | grep $beta | awk 'BEGIN { FS = " " } ; { print $1 }' | sort -u >> $newlogs/adminusage"$sigma".txt

#Preform whois
#whoip=`grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $newlogs/adminusage"$sigma".txt | sort | uniq >> $testing`
#echo $whoip
#testing="/home/user/Scripts/testing.txt"
#IPlookup="/home/user/Scripts/iptest.txt"


#Preform Usage for the current hour
if
grep -v 1.1.1.1 $newlogs/adminusage"$sigma".txt
then
#whois $testing >> $IPlookup
mail -s "Admin Usage for $emaildate" email.com < $newlogs/adminusage"$sigma".txt
else
echo
fi

Answer

sorpigal picture sorpigal · Feb 16, 2012

Just use a loop and invoke whois once per iteration

Presuming that your grep returns a newline-delimited list of IP addresses, you could do something like this:

grep ... | sort | uniq | while IFS= read -r ip ; do
    whois "$ip" >> whatever
done