Use python obtain the target location information based on ip

Information security is important, your location may be exposed! ! !

Using python and GeoLite2 acquisition target location

  . 1  # ! / Usr / bin / Python the env 
  2  # - * - Coding: UTF-. 8 - * - 
  . 3  
  . 4  '' ' 
  . 5  the Created dated 12 is ON. 8 Day 2019 years
   . 6  
  . 7  @author: the Admin
   . 8  ' '' 
  . 9  
10  from Copy Import Copy
 . 11  Import the optparse
 12 is  Import Re
 13 is  
14  Import geoip2.database
 15  
16  
. 17 Reader = geoip2.database.Reader ( ' GeoLite2-City.mmdb ' )
 18 is  
. 19  # query IP address corresponding to the physical address
20 is  DEF ip_get_location (ip_address):
 21 is      # Loading specify an IP data 
22 is      Response = reader.city (ip_address)
 23 is      
24      # read country code 
25      Country_IsoCode = response.country.iso_code
 26 is      # read country name 
27      COUNTRY_NAME = Response. country.name
 28      # read the name of the country (Chinese display) 
29      Country_NameCN = response.country.names [ ' zh-CN ' ]
 30      # to read the state (foreign) / Province (domestic) name 
31      Country_SpecificName = response.subdivisions.most_specific .name
32      # read state (foreign) / Province (China) Code 
33 is      Country_SpecificIsoCode = response.subdivisions.most_specific.iso_code
 34 is      # read city name 
35      CITY_NAME = response.city.name
 36      # read ZIP code 
37 [      City_PostalCode = response.postal .code
 38 is      # acquires latitude 
39      Location_Latitude = response.location.latitude
 40      # acquires longitude 
41 is      Location_Longitude = response.location.longitude
 42 is      
43 is      IF (Country_IsoCode == None):
 44 is         Country_IsoCode = "None"
 45     if(Country_Name == None):
 46         Country_Name = "None"
 47     if(Country_NameCN == None):
 48         Country_NameCN = "None"
 49     if(Country_SpecificName == None):
 50         Country_SpecificName = "None"
 51     if(Country_SpecificIsoCode == None):
 52         Country_SpecificIsoCode = "None"
 53     if(City_Name == None):
 54         City_Name = "None"
 55     if(City_PostalCode == None):
 56         City_PostalCode = "None"
 57     if(Location_Latitude == None):
 58         Location_Latitude = "None"
 59     if(Location_Longitude == None):
 60         Location_Longitude = "None"
 61     
 62     print('================Start===================' )
 63 is      Print ( ' [*] the Target: ' + ip_address + ' GeoLite2-the Located ' )
 64      Print (U '   [+] Country Code:         ' + Country_IsoCode)
 65      Print (U '   [+] Country Name:         ' + COUNTRY_NAME )
 66      Print (U '   [+] national Chinese name:     ' + Country_NameCN)
 67      Print (U '   [+] province or state name:     ' + Country_SpecificName)
68      Print (U '   [+] province or state encoding:     ' + Country_SpecificIsoCode)
 69      Print (U '   [+] City Name:        ' + city_name)
 70      Print (U '   [+] City Zip:        ' + City_PostalCode)
 71      Print ( U '   [+] latitude:             ' + STR (Location_Latitude))
 72      Print (U '   [+] longitude:            ' + STR (Location_Longitude))
 73 is      Print ( 'End ====================== =============== ' )
 74  
75  # examination and processing ip address 
76  DEF seperate_ip (ip_address):
 77      ip_match r = " ^ (:( ?: 25 [0-5] | 2 [0-4] [0-9] | 1 [0-9] [0-9] | 0?? [0-9] [1-9] |? 0 [1-9] 0) \) (:( 25 [0-5] |?.? 2 [0-4] [0-9] | [01] ? [0-9] [0-9]) \) {2} (?: 25 [0-4] |?. 2 [0-4] [0-9] | 1 [0-9] [0- 9] | 0 [0-9] [1-9] |?? 0 [1-9] 0) $? " 
78      ip_match_list r = " ^ (:( ?: 25 [0-5] |? 2 [0 -4] [0-9] | 1 [ 0-9] [0-9] | 0 [0-9] [1-9] |?? 0 [1-9] 0) \) (?:?. (25 [0-5] | 2 [ 0-4] [0-9] | [01] [0-9] [0-9]??) \) {2} (?: 25 [0-4. ] | 2 [0-4] [0-9 ] | 1 [0-9] [0-9] | 0 [0-9] [1-9]) - (?: 25 [0-4]?? | 2 [0-4] [0-9] | 1 [0-9] [0-9] | 0 [0-9] [1-9] |?? 0 [1-9] 0) $? " 
79      
80      IFre.match (ip_match, ip_address):
 81          the try :
 82              ip_get_location (ip_address)
 83          the except Exception AS E:
 84              Print E
 85      elif re.match (ip_match_list, ip_address):
 86          ip_start ip_address.split = ( ' - ' ) [0 ] .split ( ' . ' ) [. 3 ]
 87          ip_end, ip_address.split = ( ' - ' ) [. 1 ]
 88          # if the ip address range, as is directly performed 
89          IF (ip_start == ip_end,):
 90             try:
 91                 seperate_ip(ip_address.split('-')[0])
 92             except Exception as e:
 93                 print e
 94         elif ip_start > ip_end:
 95             print 'the value of ip, that you input, has been wrong! try again!'
 96             exit(0)
 97         else:
 98             ip_num_list =  ip_address.split('-')[0].split('.')
 99             ip_num_list.pop()
100             for ip_last in range(int(ip_start), int(ip_end)+1):
101                 ip_add = '.'.join(ip_num_list)+'.'+str(ip_last)
102                 try:
103                     ip_get_location(ip_add)
104                 except Exception as e:
105                     print e
106     else:
107         print 'Wrong type of ip address!'
108         print '100.8.11.58  100.8.11.58-100  alike!'
109     
110 # 主方法数体
111 def main():
112     parser = optparse.OptionParser('Usage%prog -i <single ip address> -I <multi ip address> \n  \n{ Egg: python getIpLocation.py -i(I) 100.8.11.58(100.8.11.58-100) }')
113     parser.add_option('-i', dest='SIp',type='string', help='specify pcap filename')
114     parser.add_option('-I', dest='MIp',type='string', help='specify pcap filename')
115     (options, args) = parser.parse_args()
116     if options.SIp == None and options.MIp == None:
117         print (parser.usage)
118         exit(0)
119     ip_address  = options.SIp
120     ip_address_multi  = options.MIp
121     if ip_address==None:
122         seperate_ip(ip_address_multi)
123     else:
124         seperate_ip(ip_address)
125 
126 if __name__ == '__main__':
127     main()

 

result:

 

 

 

Guess you like

Origin www.cnblogs.com/perilong16/p/12008715.html