def get_country_by_ip(ip): ip_ver = ipaddress.ip_address(str(ip)) if ip_ver.version == 4: if os.path.exists(settings.IPV6_IP2LOCATION_PATH): database = IP2Location.IP2Location(settings.IPV6_IP2LOCATION_PATH) rec = database.get_all(ip) return rec.country_short else: if os.path.exists(settings.IPV4_IP2LOCATION_PATH): database = IP2Location.IP2Location(settings.IPV4_IP2LOCATION_PATH) rec = database.get_all(ip) return rec.country_short return False
def getGeoLocations_fromIPAddress(df_out): country_short, country, city, zipcode, region, locale, referral_code = ( [] for i in range(7)) IP2LocObj = IP2Location.IP2Location() for index, rows in df_out.iterrows(): if pd.notnull(rows['ip']): IP2LocObj.open( "data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN" ) rec = IP2LocObj.get_all(rows['ip']) country_short.append(rec.country_short) zipcode.append(rec.zipcode) region.append(rec.region) country.append(rec.country_long) city.append(rec.city) locale.append('en-' + rec.country_short.lower()) else: country_short.append(np.NaN) zipcode.append(np.NaN) region.append(np.NaN) country.append(np.NaN) city.append(np.NaN) locale.append(np.NaN) random = str(uuid.uuid4()).upper().replace("-", "") referral_code.append(random[0:10]) return region, city, zipcode, country, country_short, locale, referral_code
def search(): #my_ip=jsonify({'ip': request.environ['REMOTE_ADDR']}) ip = str(request.environ['REMOTE_ADDR']) #return ip IP = IP2Location.IP2Location() IP.open("/home/prakash/git/ip2location-python-7.0.0/data/IP-COUNTRY.BIN") rec = IP.get_all(ip) query = rec.country_long queryIP = IP2Location.IP2Location() #query=query.replace(' ','%20') final_url = ( 'https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=' + query + '&userip=INSERT-USER-IP') json_obj = urllib2.urlopen(final_url) data = json.load(json_obj) return render_template('b.html', data=data)
def update_log(log_file, traefik_ip): dir_path = os.path.dirname(os.path.realpath(__file__)) target_path = dir_path + '/data/IP2LOCATION-LITE-DB3.BIN' exclude_ip_path = dir_path + '/data/exclude-ips.txt' ex_ip_set = {} update_con = sl.connect(db_path) tempdf=pd.read_sql_query('SELECT time from connections ORDER BY time', update_con) last_time=tempdf.iloc[len(tempdf)-1]['time'] ipdb = IP2Location.IP2Location(target_path) try: ex_ip_set = set(line.strip() for line in open(exclude_ip_path)) except: pass data = [] i = 0 with open(log_file) as f: for line in f: data.append(json.loads(line)) while i < len(data): if data[i]['ClientHost'] != traefik_ip and data[i]['ClientHost'] not in ex_ip_set and data[i]['time'] > last_time: details = ipdb.get_all(data[i]['ClientHost']) sql_work = "INSERT INTO connections (ip, requestmethod, requestpath, requestprotocol, requestscheme, statuscode, time, city, region, country_short, country_long) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" update_con.execute(sql_work, (data[i]['ClientHost'], data[i]['RequestMethod'], data[i]['RequestPath'], data[i]['RequestProtocol'], data[i]['RequestScheme'], data[i]['DownstreamStatus'], data[i]['time'], details.city, details.region, details.country_short, details.country_long)) i = i + 1 update_con.commit() update_con.close()
def add_geo_loc(self, as_graph): as_prefix = self.as_prefix # use IP2Location database to get the link between AS and longtitude ip2_loc_obj = IP2Location.IP2Location() if self.mode == "IPv4": ip2_loc_obj.open("IP2LocationDBs/IP2LOCATION-LITE-DB5.BIN") elif self.mode == "IPv6": ip2_loc_obj.open("IP2LocationDBs/IP2LOCATION-LITE-DB5.IPV6.BIN") for key in as_prefix: # calculate the average longtitude of a set of prefixes for one asn sum_longtitude = 0.0 for elem in as_prefix[key]: ip_adress = elem.split('/') try: rec = ip2_loc_obj.get_all(ip_adress[0]) except ValueError: continue sum_longtitude += rec.longitude # set the longtitude as a attribute of the node in graph if len(as_prefix[key]) > 0: as_graph.add_node(key, longtitude=sum_longtitude / len(as_prefix[key])) return as_graph
def IpTracker(ip): # ip="115.112.67.196" database = IP2Location.IP2Location( os.path.join("data", "IP2LOCATION-LITE-DB11.BIN")) rec = database.get_all(str(ip)) return rec
def geocheck(address): iptocheck = address.strip() IP2LocObj = IP2Location.IP2Location(); IP2LocObj.open("IP2LOCATION-LITE-DB1.BIN"); rec = IP2LocObj.get_all(iptocheck); country = rec.country_long return country
def get_geo_info(params): df = params[0] output_file = params[1] ip2locationdb = params[2] ip2location = IP2Location.IP2Location() ip2location.open(ip2locationdb) # country f = lambda x: ip2location.get_country_short(x) df['country_ip2location'] = df['src_net'].apply(f) df['country_ip2location'] = df['country_ip2location'].str.decode( "utf-8").str.replace(",", "") # region f = lambda x: ip2location.get_region(x) df['region'] = df['src_net'].apply(f) df['region'] = df['region'].str.decode("utf-8").str.replace(",", "") f = lambda x: ip2location.get_latitude(x) df['latitude'] = df['src_net'].apply(f) f = lambda x: ip2location.get_longitude(x) df['longitude'] = df['src_net'].apply(f) df = df.fillna(0) return ([df, output_file])
def ipinfo(): ip = request.args.get('ipaddress') if ip is None: return 'Please give an ipAddress in URL like http://0.0.0.0:5000/ipinfo?ipaddress=123.456.789.123' checkip = ipregex.match(ip) if not checkip: return "No valid ip address" IP2LocObj = IP2Location.IP2Location() IP2LocObj.open("./IP2LOCATION-LITE-DB11.BIN") response = IP2LocObj.get_all(ip) dict = {} dict["statusCode"] = "OK" dict["statusMessage"] = "" dict["ipAddress"] = ip dict["countryCode"] = response.country_short.decode("utf-8") dict["countryName"] = response.country_long.decode("utf-8") dict["regionName"] = response.region.decode("utf-8") dict["cityName"] = response.city.decode("utf-8") dict["zipCode"] = response.zipcode.decode("utf-8") dict["latitude"] = response.latitude dict["longitude"] = response.longitude dict["timeZone"] = response.timezone.decode("utf-8") jsonarray = json.dumps(dict) return jsonarray
def q1(): for x in logs: if (x[3] in already_sorted): continue with geoip2.database.Reader( "C:\GeoLite2-Country_20200804\GeoLite2-Country.mmdb" ) as reader: try: response = reader.country(x[3]).country.name except Exception: #response = "-" IP2LocObj = IP2Location.IP2Location() IP2LocObj.open( "C:\IP2Location-Python-master\data\IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN" ) rec = IP2LocObj.get_all(x[3]) if (rec.country_long not in q1Answ): q1Answ[rec.country_long] = 1 else: q1Answ[rec.country_long] = q1Answ[rec.country_long] + 1 if (response not in q1Answ): q1Answ[response] = 1 else: q1Answ[response] = q1Answ[response] + 1 already_sorted.append(x[3])
def __init__(self, source_list=["mmdb"]): self.source_list = source_list self.lookup = None self.qqwry = None self.reader = None self.database = None #bgp. if "bgp" in source_list: self.lookup = lookup.lookup() #czdb. if "czdb" in source_list: self.czdb_path = "qqwry.dat" if not os.path.exists(self.czdb_path): qqwry.update_db(self.czdb_path) self.qqwry = qqwry.QQWry(self.czdb_path) #mmdb. if "mmdb" in source_list: self.reader = geoip2.database.Reader('GeoLite2-City.mmdb') #ip2location: if "ip2location" in source_list: self.database = IP2Location.IP2Location() self.database.open("IP2LOCATION-LITE-DB11.BIN")
def ip2location(ip): regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$''' ip2locdata = dict() if (re.search(regex, ip)): IP2LocObj = IP2Location.IP2Location() IP2LocObj.open('ip2loc.BIN') ip2loc_result = IP2LocObj.get_all(ip) #print(ip2loc_result) ip2locdata['IP'] = ip ip2locdata['Country Short'] = ip2loc_result.country_short ip2locdata['Country Llong'] = ip2loc_result.country_long ip2locdata['Region'] = ip2loc_result.region ip2locdata['City'] = ip2loc_result.city ip2locdata['ISP'] = ip2loc_result.isp ip2locdata['Latitude'] = ip2loc_result.latitude ip2locdata['Longitude'] = ip2loc_result.longitude ip2locdata['Domain'] = ip2loc_result.domain ip2locdata['Zip Code'] = ip2loc_result.zipcode ip2locdata['Time Zone'] = ip2loc_result.timezone ip2locdata['Net Speed'] = ip2loc_result.netspeed ip2locdata['Idd Code'] = ip2loc_result.idd_code ip2locdata['Area Code'] = ip2loc_result.area_code ip2locdata['Weather Code'] = ip2loc_result.weather_code ip2locdata['Weather Name'] = ip2loc_result.weather_name ip2locdata['MCC'] = ip2loc_result.mcc ip2locdata['MNC'] = ip2loc_result.mnc ip2locdata['Mobile Brand'] = ip2loc_result.mobile_brand ip2locdata['Elevation'] = ip2loc_result.elevation ip2locdata['Usage Type'] = ip2loc_result.usage_type return ip2locdata else: #ip2locdata['IP'] = "Error" ip2locdata['IP'] = ip ip2locdata['Country Short'] = '-' ip2locdata['Country Llong'] = '-' ip2locdata['Region'] = '-' ip2locdata['City'] = '-' ip2locdata['ISP'] = '-' ip2locdata['Latitude'] = '-' ip2locdata['Longitude'] = '-' ip2locdata['Domain'] = '-' ip2locdata['Zip Code'] = '-' ip2locdata['Time Zone'] = '-' ip2locdata['Net Speed'] = '-' ip2locdata['Idd Code'] = '-' ip2locdata['Area Code'] = '-' ip2locdata['Weather Code'] = '-' ip2locdata['Weather Name'] = '-' ip2locdata['MCC'] = '-' ip2locdata['MNC'] = '-' ip2locdata['Mobile Brand'] = '-' ip2locdata['Elevation'] = '-' ip2locdata['Usage Type'] = '-' return ip2locdata
def get_client_country(self, ip): database = IP2Location.IP2Location("IP-COUNTRY.BIN") try: rec = database.get_all(ip) except ValueError: database = IP2Location.IP2Location("IPV6-COUNTRY.BIN") rec = database.get_all(ip) country_code = rec.country_short if not country_code: return settings.DEFAULT_COUNTRY country_code = country_code.lower() country = self.countries.filter(code=country_code).first() if not country: return settings.DEFAULT_COUNTRY return CountrySerializer(country).data.get('code')
def ip2loc(db_file, addr): ''' Return all found data for addr ''' ip2loc = IP2Location.IP2Location() ip2loc.open(db_file) return ip2loc.get_all(addr)
def __init__(self): self.sig_dir = Path(settings.SIGNATURE_DIR) self.malwaredomainlist = self.sig_dir / 'malwaredomainlist' self.maltrail = self.sig_dir / 'maltrail-malware-domains.txt' self.iplocbin = self.sig_dir / 'IP2LOCATION-LITE-DB5.IPV6.BIN' self.result = {} self.domainlist = None self.IP2Loc = IP2Location.IP2Location()
def __init__(self, dbPath): print( "Warning: Autonomous system lookup not implemented yet for IP2Location!" ) self.dbPath = dbPath self.IPv6Resolver = IP2Location.IP2Location( os.path.join(self.dbPath, "IP2LOCATION-LITE-DB1.IPV6.BIN"), "SHARED_MEMORY")
def getInformation(self): ip2loc = IP2Location.IP2Location() ip2loc.open( "data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN" ) rec = ip2loc.get_all(self.host) self.ip2locInfo = rec
def __init__(self, args): self.download_path = args.download_path database_file = os.path.join(args.download_path, self.DATABASE_FILE) if not os.path.isfile(database_file): self.download_database() self.database = IP2Location.IP2Location(database_file)
def GeoLocate(ip): """ Method for translating ip-address to geolocation (country) """ try: IP2LocObj = IP2Location.IP2Location() IP2LocObj.open('../2. Profiler/sources/IP2GEODB.BIN') return IP2LocObj.get_all(ip).country_long except Exception as e: print e return 'Geolocation failed'
def iptrace(ip): database = IP2Location.IP2Location() database.open("src/IP2LOCATION-LITE-DB11.BIN") loc = database.get_all(ip) print("IP : " + ip) print("City : " + loc.city) print("State : " + loc.region) print("Country : " + loc.country_long) proxy = IP2Proxy.IP2Proxy() # open IP2Proxy BIN database for proxy lookup proxy.open("src/IP2PROXY-LITE-PX8.BIN") record = proxy.get_all(ip) isproxy = str(record['is_proxy']) if isproxy == '1': print('Proxy : Yes') if record['proxy_type'] == "PUB": print("Proxy Type : Public") else: print('Proxy Type: ' + record['proxy_type']) print('ISP : ' + record['isp']) print('Domain : ' + record['domain']) print('Usage Type : ' + record['usage_type']) print('ASN : ' + record['asn']) print('Company Name : ' + record['as_name']) else: pass lats = [] lons = [] r = requests.get("http://api.ipstack.com/" + ip + "?access_key=" + api_key) resp = r.json() print("Latitude : {longitude}".format(**resp)) print("Longitude : {longitude}".format(**resp)) if resp['latitude'] and resp['longitude']: lats = resp['latitude'] lons = resp['longitude'] maps_url = "https://maps.google.com/maps?q=%s,+%s" % (lats, lons) openWeb = input("Open GPS location in web broser? (Y/N) ") if openWeb.upper() == 'Y': webbrowser.open(maps_url, new=2) else: print() get_ip() proxy.close() database.close()
def query_from_ip2location(self, ip): try: database = IP2Location.IP2Location() database.open("IP2LOCATION-LITE-DB11.BIN") rec = database.get_all(ip) if rec is not None: return {"country":rec.country_short, "city":rec.city, "latitude":rec.latitude, "longitude":rec.longitude} except: return {"country":"*", "city":"*", "latitude":"*", "longitude":"*"}
def get(ip_address, api_key=None, db_path=None, username=None, password=None): # process request try: ip2loc = IP2Location.IP2Location() ip2loc.open(db_path) except: raise ServiceError() # content res = ip2loc.get_all(ip_address) if res is None: raise IpAddressNotFoundError(ip_address) # prepare return value ip_location = IpLocation(ip_address) # format data if res.country_short != ' ' \ or res.country_short == 'N/A' \ or res.country_short == '??': ip_location.country = res.country_short.decode('utf-8') else: ip_location.country = None if res.region != ' ' \ or res.region == 'N/A': ip_location.region = res.region.decode('utf-8') else: ip_location.region = None if res.city != ' ' \ or res.city == 'N/A': ip_location.city = res.city.decode('utf-8') else: ip_location.city = None if res.latitude != ' ' \ or res.latitude == 'N/A': ip_location.latitude = float(res.latitude) else: ip_location.latitude = None if res.longitude != ' ' \ or res.longitude == 'N/A': ip_location.longitude = float(res.longitude) else: ip_location.longitude = None return ip_location
def fetch_data(self): location_obj = iplocation.IP2Location() location_obj.open("data/data.bin") try: rec = location_obj.get_all(self.ip.get()) self.location_data_frame.represent_data(rec) except OSError or UnboundLocalError: msgbox.showerror( 'Oops', 'We could not extract any data from the given IP-Adress, please try again! For information about valid IP-Adresses and their respective formats, check the FAQ or contact the developer in the "help" menu cascade.' )
def get_country_code(_ip): """ 获取 国家代码 https://www.ip2location.com/development-libraries/ip2location/python :param _ip: :return: """ if tools.is_number(_ip): _ip = long2ip(int(_ip)) database = IP2Location.IP2Location(tools.ROOT_PATH + "/P/IP2Location/data/IPV6-COUNTRY.BIN") rec = database.get_all(_ip) return str(rec.country_short, encoding='utf-8')
def process_log(log_file, traefik_ip, ip_token): dir_path = os.path.dirname(os.path.realpath(__file__)) exclude_ip_path = dir_path + '/data/exclude-ips.txt' target_path = dir_path + '/data/IP2LOCATION-LITE-DB3.BIN' ex_ip_set = {} try: ex_ip_set = set(line.strip() for line in open(exclude_ip_path)) print(datetime.datetime.now().strftime('%c') + ' - Exclude IP list loaded.') except: print(datetime.datetime.now().strftime('%c') + ' - Exclude IP list not found, skipping') try: ipdb = IP2Location.IP2Location(target_path) print(datetime.datetime.now().strftime('%c') + ' - Found IP2Location database.') except: print(datetime.datetime.now().strftime('%c') + ' - IP Location DB not found, fetching...') get_ipdb('https://www.ip2location.com/download/?token=' + ip_token + '&file=DB3LITEBIN') ipdb = IP2Location.IP2Location(target_path) data = [] i = 0 try: with open(log_file, 'r') as f: for line in f: try: data.append(json.loads(line)) except: print(datetime.datetime.now().strftime('%c') + ' - Logfile is not in JSON format!') print(datetime.datetime.now().strftime('%c') + ' - Please use "--log.format=json" option in your Traefik config.') print(datetime.datetime.now().strftime('%c') + ' - https://doc.traefik.io/traefik/v2.0/observability/logs/') raise SystemExit while i < len(data): if data[i]['ClientHost'] != traefik_ip and data[i]['ClientHost'] not in ex_ip_set: details = ipdb.get_all(data[i]['ClientHost']) sql_work = "INSERT INTO connections (ip, requestmethod, requestpath, requestprotocol, requestscheme, statuscode, time, city, region, country_short, country_long) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" cur.execute(sql_work, (data[i]['ClientHost'], data[i]['RequestMethod'], data[i]['RequestPath'], data[i]['RequestProtocol'], data[i]['RequestScheme'], data[i]['DownstreamStatus'], data[i]['time'], details.city, details.region, details.country_short, details.country_long)) i = i + 1 except: print(datetime.datetime.now() + 'Error opening logfile. Please verify logfile setting in config.ini.') raise SystemExit con.commit()
def init_geo(geoip_path, lib_type=GeoLibType.GEOIP): _Context.geoip = None _Context.ip2location = None try: if lib_type == GeoLibType.GEOIP: import pygeoip _Context.geoip = pygeoip.GeoIP(geoip_path, pygeoip.MEMORY_CACHE) elif lib_type == GeoLibType.IP2LOCATION: import IP2Location _Context.ip2location = IP2Location.IP2Location(geoip_path) else: raise Exception('unsupported_geo_lib_type') except: logging.exception('geo_init_failed|type=%d,path=%s', lib_type, geoip_path)
def GeoLocate(ip): """ Method for translating ip-address to geolocation (country) """ try: IP2LocObj = IP2Location.IP2Location() IP2LocObj.open("sources\IP2GEODB.BIN") return IP2LocObj.get_all(ip).country_long except Exception: if options.ping: try: return IP2LocObj.get_all(dns.resolver.query( ip, 'A')[0]).country_long except Exception: return "Geolocation failed" else: return "Domain translation disabled"
def getServerInfoList(): IP2LocObj = IP2Location.IP2Location() IP2LocObj.open('app/main/static/IP2LOCATION-LITE-DB5.BIN') # LocalIPList = ['47.111.168.213', '47.102.41.81', '47.88.63.126', '161.117.84.146', '39.97.166.176', '47.92.85.186', '45.58.54.216'] IpReplaceDic = { '47.88.63.126': '104.156.230.107', '161.117.84.146': '103.233.82.251', '47.111.168.213': '47.92.85.186', '45.58.54.216': '45.58.54.216', '39.97.166.176': '123.125.71.38', '47.92.85.186': '124.239.26.95', '47.102.41.81': '101.227.66.81' } IPList = [] file1 = open('app/main/static/ip.txt') for line in file1: IPList.append(line.strip()) file1.close() res = [] for ip in IPList: if ip in IpReplaceDic: ip = IpReplaceDic[ip] loc = IP2LocObj.get_all(ip) # prev format is "country_short": str(loc.country_short, encoding="utf-8") # error: decoding str is not supported info = { 'country_short': loc.country_short, 'country_long': loc.country_long, 'region': loc.region, 'city': loc.city, 'latitude': loc.latitude, 'longitude': loc.longitude } res.append(info) response = make_response(json.dumps(res)) response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET,POST' response.headers[ 'Access-Control-Allow-Headers'] = 'x-requested-with,content-type' resp = json.dumps(res) return resp
def details(): results = {} address = request.headers.getlist("X-Forwarded-For")[0] try: IP2LocObj = IP2Location.IP2Location() IP2LocObj.open("data/IP2LOCATION-LITE-DB1.BIN") address_details = IP2LocObj.get_all(address) except Exception as e: results['error'] = str(e) return Response(json.dumps(results), mimetype='application/json') results['ipAddress'] = address_details.ip results['countryCode'] = address_details.country_short.decode() results['countryName'] = address_details.country_long.decode() return Response(json.dumps(results), mimetype='application/json')
def setup(): f = open(ipfile, "r") ip_list = f.readlines() f.close() database = IP2Location.IP2Location( os.path.join("IP2LOCATION-LITE-DB5.BIN")) info = check_all_ip_x_y(database, ip_list) world_map = folium.Map([0.000000, 0.000000], zoom_start=3) for ip in info: world_map.add_child( folium.CircleMarker([info[ip][0], info[ip][1]], radius=4, color='red', fill=True, fill_color='white', fill_opacity=0.6)) #world_map.render() world_map.save("ebola.html")