Advertisement
spikeysnack

whereip

Oct 31st, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """ whereip
  5.    use curl to get location of ip
  6. """
  7.  
  8. #-----------------------------------------
  9. __author__      = "Chris Reid"
  10. __category__    = "network utility"
  11. __copyright__   = "Copyright 2016"
  12. __country__     = "United States of America"
  13. __credits__     = ["Python Software Foundation", "Free Software Foundation" ]
  14. __email__       = "spikeysnack@gmail.com"
  15. __file__        = ["whereip", "whereip.py"]
  16. __license__     = """Free for all non-commercial purposes.
  17.                  Modifications allowed but original attribution must be included.
  18.                  see (http://creativecommons.org/licenses/by-nc/4.0/)"""
  19. __maintainer__  = "chris Reid"
  20. __modification_date__ = "31 Oct 2016"
  21. __version__     = "1.1"
  22. __status__      = "beta"
  23. #-----------------------------------------
  24.  
  25. #--------
  26. import os
  27. import sys
  28. import ast
  29. import subprocess
  30.  
  31. URL = "ipinfo.io" # this is the service that returns the info
  32.  
  33. #--------------------        
  34. def ip_to_dict( ip ):
  35.  
  36.     """ get a series of strings from ipinfo.io
  37.        and make a dictionary out of it        
  38.  
  39.    Args:
  40.         ip (str):   the 4-dotted ip number
  41.    Returns:
  42.         ip_info (dict):   a dictionary of location info
  43.    {
  44.    'loc': '37.3860,-122.0838',
  45.    'city': 'Mountain View',
  46.    'country': 'US',
  47.    'region': 'California',
  48.    'hostname': 'google-public-dns-a.google.com',
  49.    'ip': '8.8.8.8',
  50.    'org': 'AS15169 Google Inc.',
  51.    'postal': '94035'
  52.    }
  53.        
  54.    """
  55.  
  56.  
  57.     l = ""
  58.     command = "curl -s  " + URL+ "/" + ip
  59.     ip_info = {}
  60.  
  61.     p = subprocess.Popen( command , shell=True, stdout=subprocess.PIPE)
  62.     """ class subprocess.Popen: run system process, capture output """
  63.     retval = p.wait()
  64.     """ int|None: Wait for child process to terminate, get returncode """
  65.  
  66.     # gather lines into one string  
  67.     for line in p.stdout.readlines():
  68.         l = l + line
  69.  
  70.     ip_info = ast.literal_eval( l )
  71.     ''' dict: eval string as python dict '''
  72.  
  73.     return ip_info
  74.  
  75. # ip_to_dict
  76.  
  77.  
  78. #------------------
  79. #script starts here
  80.  
  81. if __name__ == '__main__':
  82.  
  83.     IP = ""
  84.     if len(sys.argv) > 1:
  85.         IP=sys.argv[1]
  86.  
  87.     ipinfo = ip_to_dict( IP )
  88.  
  89.     # print (ipinfo)
  90.  
  91.     # for k in  ipinfo:
  92.     #     print ( k + " = " +  ipinfo[k]  + "\n" )
  93.  
  94.     loc      = ipinfo["loc"]
  95.     city     = ipinfo["city"]
  96.     region   = ipinfo["region"]
  97.     country  = ipinfo["country"]
  98.     hostname = ipinfo["hostname"]
  99.  
  100.     print ( country + ':\t' + region + ':\t' + city + ':\t' + hostname + '\n' )
  101.  
  102.  
  103. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement