tags:

views:

248

answers:

3

I'm looking for a one-liner that based on a list of IPs will append the country from where the IP is based

So if I have this as and input:

87.229.123.33
98.12.33.46
192.34.55.123

I'd like to produce this:

87.229.123.33 - GB
98.12.33.46 - DE
192.34.55.123 - US

I've already got a script that returns the country for IP but I need to glue it all together with awk, so far this is waht I came up with:

$ get_ips | nawk '{ print $1; system("ip2country " $1) }'

This is all cool but the ip and the country are not displayed on the same line, how can I merge the system output and the ip on one line ?

If you have a better way of doing this, I'm open to suggestions.

+1  A: 

I'll answer with a perl one-liner because I know that syntax better than awk. The "chomp" will cut off the newline that is bothering you.

get_ips | perl -ne 'chomp; print; print `ip2country $_`'
amarillion
+5  A: 

You can use printf instead of print:

{ printf("%s - ", $1); system("ip2country " $1); }
hvintus
Should it really be "system(system(" ? Does this solution give output in the required format, "87.229.123.33 - GB"?
Reef
Oops, I removed the extra 'system' call. That was a typo.
hvintus
Your solution outputs something like "87.229.123.33GB" and "192.34.55.123US".
Reef
Reef, you are right, I should have added " - " to the format string.
hvintus
+1  A: 

The proper one-liner solution in awk is:

awk '{printf("%s - ", $1) ; system("ip2country \"" $1 "\"")}' < inputfile

However I think it would be much faster if You would use a python program looking like that:

import GeoIP
gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
for line in file("ips.txt", "r")
    line = line[:-1] # strip the last from the line
    print line, "-", gi.country_code_by_addr(line)

As You can see, the geoip object is initialized only once and then it is reused for all queries. See a python binding for geoip. Also be aware that Your awk solution forks a new process 2 times per line!

I don't know how many entries You need to process, but if it's much of it, You should consider something that doesn't fork and keeps the geoip database in memory.

Reef
are you sure the ip2country shouldn't be inside quotes in the sysetm call ?
duckyflip
Right. There was a missing " in the system call. The thing about awk is that trouble with the quoting happens all the time, I once had to write an 80-char line with 56 of ", ` and ' characters.
Reef