Advertisement
BaSs_HaXoR

IPTrak Python IP Address Lookup Tool (CLI)

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