Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-16-2010
Registered User
 

Join Date: Mar 2008
Posts: 186
Thanks: 4
Thanked 0 Times in 0 Posts
whois country help

Hello folks,

I have list of ips like


Code:
1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4



Code:
whois 1.1.1.1 |grep -E 'country|Country'

it show country=US or whatever.

so i have number of ips in text file, how i can use above script to automate output like


Code:
1.1.1.1 US
2.2.2.2 CA
3.3.3.3 FR



---------- Post updated at 09:16 AM ---------- Previous update was at 09:04 AM ----------


Code:
#!/bin/sh

while read inputline
do
  country=`whois $inputline|grep -E 'country|Country'`
   echo $inputline $country
done < /ip.txt

exit 0

i have done it like that, is there any other suggestion

---------- Post updated at 09:22 AM ---------- Previous update was at 09:16 AM ----------

in same file of ips, i have ips with hits like


Code:
50 1.1.1.1
40 2.2.2.2
30 3.3.3.3
15 4.4.4.4

how can i see its output like that with above script


Code:
IP=1.1.1.1 Country=1.1.1.1 Hits=50
IP=2.2.2.2 Country=2.2.2.2 Hits=40


Last edited by vgersh99; 05-16-2010 at 11:00 AM.. Reason: code tags, PLEASE!
Sponsored Links
    #2  
Old 05-16-2010
pseudocoder's Avatar
Registered User
 

Join Date: Sep 2007
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts
Does you script actually work? I keep experiencing that almost every whois query returns two country codes. I think the first is the code for the whois server itself (not sure), and the second for the ip which is being queried. Some whois queries don't have any country code. However following while loop works pretty nice for me:

Code:
$ cat iplist
202 xxx.xxx.xxx.x
22 xxx.xx.xx.x
71 xx.xx.xxx.xx
112 xxx.xx.x.x
$ while read hits ip; do
country=$(whois "$ip" | grep -E '[Cc]ountry' | sed 's/[Cc]ountry: *//' | tr "\n" ",")
echo -n "IP=$ip Country=$country Hits=$hits" && echo
done <iplist | sed 's/, H/ H/'
IP=xxx.xxx.xxx.x Country=NL,AT Hits=202
IP=xxx.xx.xx.x Country= Hits=22
IP=xx.xx.xxx.xx Country=NL,FR Hits=71
IP=xxx.xx.x.x Country=NL,IT Hits=112
$

Sponsored Links
    #3  
Old 05-16-2010
fpmurphy's Avatar
who?
 

Join Date: Dec 2003
Location: /dev/ph
Posts: 4,043
Thanks: 35
Thanked 282 Times in 263 Posts

Code:
while read hits ip
do
   country=$(whois $ip | awk 'tolower($1) == "country:" { print $2 }')
   echo "IP=$ip  Country=$country  Hits=$hits"
done < iplist

    #4  
Old 05-16-2010
Registered User
 

Join Date: Mar 2008
Posts: 186
Thanks: 4
Thanked 0 Times in 0 Posts
Thanks.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using 'whois' to Retrieve all IPs/Subnets for an Organization deckard IP Networking 0 09-25-2009 11:38 AM
whois scripting stesecci Shell Programming and Scripting 1 11-07-2008 08:45 AM
whois VS jwhois and timeout PWSwebmaster Shell Programming and Scripting 4 02-15-2008 01:54 AM
Traceroute and Whois mystery DumDum IP Networking 2 07-03-2002 11:24 PM



All times are GMT -4. The time now is 03:53 AM.