iViiRuS

iptrak.py

Jun 4th, 2014
417
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # IP Address Lookup Tool
  2. # Coded by Sheikh Viirus
  3.  
  4.  
  5. #IMPORTS
  6. import re
  7. import sys
  8. import urllib.request
  9.  
  10. #BYTE CONTROL
  11. def encodeString(string) : return string.encode('utf-8')
  12. def decodeString(string) : return string.decode('utf-8')
  13.  
  14. #DEBUG MESSAGES
  15. def action(msg)    : print('[#] - ' + msg)
  16. def alert(msg)     : print('[+] - ' + msg)
  17. def error(msg)     : print('[!] - ' + msg)
  18. def errorExit(msg) : raise SystemExit('[!] - ' + msg)
  19.  
  20. #GET BETWEEN
  21. def getBetween(source, start, stop):
  22.     search = encodeString(start + '(.*?)' + stop)
  23.     data   = re.compile(search).search(source)
  24.     if data:
  25.         found = decodeString(data.group(1))
  26.         return found.replace('\n', '')
  27.     else:
  28.         return False
  29.  
  30. #GET IP INFORMATION
  31. def getIP(ip):
  32.     source  = urllib.request.urlopen('http://www.whatismyipaddress.com/ip/' + ip).read()
  33.     country = getBetween(source, 'Country:</th><td>', ' <img ')
  34.     state   = getBetween(source, 'State/Region:</th><td>', '</td>')
  35.     city    = getBetween(source, 'City:</th><td>', '</td>')
  36.     isp     = getBetween(source, 'ISP:</th><td>', '</td>')
  37.     return [isp, country, state, city]
  38.  
  39. #VERIFY IP ADDRESS
  40. def verifyIP(ip):
  41.     if re.match('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', ip):
  42.         return True
  43.     else:
  44.         return False
  45.  
  46. #VERSION CHECK (sys)
  47. def versionCheck():
  48.     if sys.version_info.major != 3 or sys.version_info.minor != 3:
  49.         errorExit('Requires Python version 3.3 to be installed.')
  50.  
  51. #START
  52. versionCheck()
  53. if len(sys.argv) != 2:
  54.     error('Missing command line arguments!')
  55.     errorExit('Usage : iptrak.py <ip>')
  56. ip = sys.argv[1]
  57. if verifyIP(ip) == True:
  58.     try:
  59.         data = getIP(ip)
  60.         alert('IP Address : ' + ip)
  61.         alert('ISP   : ' + data[0])
  62.         alert('Country   : ' + data[1])
  63.         alert('State     : ' + data[2])
  64.         alert('City  : ' + data[3])
  65.     except:
  66.         errorExit('Failed to retrieve IP address information online!')
  67. else:
  68.     error('Invalid IP address!')
  69.     errorExit('Usage : iptrak.py <ip>')
RAW Paste Data