Wednesday, June 26, 2013

IP Geolocator (Python Script)

Here's the 1st modified python script to perform geolocation checking on whois. By supplying an IP address, it will locate the country and the whois[OrgName].

import re
import sys
import urllib2
import BeautifulSoup
usage = "Run the script: ./ IPAddress"
if len(sys.argv)!=2:
if len(sys.argv) > 1:
    ipaddr = sys.argv[1]
geody = "" + ipaddr
html_page = urllib2.urlopen(geody).read()
soup = BeautifulSoup.BeautifulSoup(html_page)
# Filter paragraph containing geolocation info.
paragraph = soup('p')[3]
# Remove html tags using regex.
geo_txt = re.sub(r'<.*?>', '', str(paragraph))
print geo_txt[32:].strip()