使用python根据ip获取目标地理位置信息

眉间皱痕 提交于 2020-10-27 18:57:32

信息安全很重要,你的地理位置可能暴露了!!!

使用python和GeoLite2获取目标的地理位置

  1 #! /usr/bin/env python
  2 #-*- coding:utf-8 -*-
  3 
  4 '''
  5 Created on 2019年12月8日
  6 
  7 @author: Admin
  8 '''
  9 
 10 from copy import copy
 11 import optparse
 12 import re
 13 
 14 import geoip2.database
 15 
 16 
 17 reader = geoip2.database.Reader('GeoLite2-City.mmdb')
 18 
 19 # 查询IP地址对应的物理地址
 20 def ip_get_location(ip_address):
 21     # 载入指定IP相关数据
 22     response = reader.city(ip_address)
 23     
 24     #读取国家代码
 25     Country_IsoCode = response.country.iso_code
 26     #读取国家名称
 27     Country_Name = response.country.name
 28     #读取国家名称(中文显示)
 29     Country_NameCN = response.country.names['zh-CN']
 30     #读取州(国外)/省(国内)名称
 31     Country_SpecificName = response.subdivisions.most_specific.name
 32     #读取州(国外)/省(国内)代码
 33     Country_SpecificIsoCode = response.subdivisions.most_specific.iso_code
 34     #读取城市名称
 35     City_Name = response.city.name
 36     #读取邮政编码
 37     City_PostalCode = response.postal.code
 38     #获取纬度
 39     Location_Latitude = response.location.latitude
 40     #获取经度
 41     Location_Longitude = response.location.longitude
 42     
 43     if(Country_IsoCode == None):
 44         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     print('[*] Target: ' + ip_address + ' GeoLite2-Located ')
 64     print(u'  [+] 国家编码:        ' + Country_IsoCode)
 65     print(u'  [+] 国家名称:        ' + Country_Name)
 66     print(u'  [+] 国家中文名称:    ' + Country_NameCN)
 67     print(u'  [+] 省份或州名称:    ' + Country_SpecificName)
 68     print(u'  [+] 省份或州编码:    ' + Country_SpecificIsoCode)
 69     print(u'  [+] 城市名称 :       ' + City_Name)
 70     print(u'  [+] 城市邮编 :       ' + City_PostalCode)
 71     print(u'  [+] 纬度:            ' + str(Location_Latitude))
 72     print(u'  [+] 经度 :           ' + str(Location_Longitude))
 73     print('===============End======================')
 74 
 75 # 检验和处理ip地址
 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     if re.match(ip_match, ip_address):
 81         try:
 82             ip_get_location(ip_address)
 83         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         # 如果ip地址范围一样,则直接执行
 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()

 

结果:

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!