The corresponding physical location of the IP address simply parses the city

Source: Internet
Author: User
Tags get ip
#!/usr/bin/python #-*-coding:utf-8-* import dpkt Import socket import Pygeoip import optparse gi = Pygeoip.
    GeoIP (' GeoLiteCity.dat ') # Query database-related urban information and output Def Printrecord (TGT): rec = gi.record_by_name (TGT) city = rec[' Cities '] # The original code for region = rec[' Region_name '], has been discarded ' region_name ' region = rec[' Region_code '] country = rec[' country_name ' ] longs = rec[' longitude '] lat = rec[' latitude '] print ' [*] Target: ' + TGT + ' geo-located. ' print ' [+] ' +str (city) + ', ' +str (region) + ', ' +str (country) print ' [+] Latitude: ' +str (LAT) + ', Longitude: ' + str ( Longs def printpcap (pcap): # traverse the array for [timestamp, packet] records for (TS, buf) in Pcap:try: # Get Ethernet Section Sub-Data ETH = Dpkt.ethernet.Ethernet (buf) # Get IP layer Data IP = eth.data # to store in Inet_n The IP address in Toa is converted to a string src = Socket.inet_ntoa (ip.src) DST = Socket.inet_ntoa (IP.DST) print
         ' [+] src: ' + src + '--> DST: ' + DST   print ' [+] SRC: ' + retgeostr (src) + '--> DST: ' + retgeostr (DST) Except:pass # returns the physical bit corresponding to the specified IP address Set def retgeostr (IP): Try:rec = Gi.record_by_name (IP) city = rec[' city '] country = rec[' countr
        Y_code3 '] If City!= ': geoloc = city + ', ' + country Else:geoloc = Country return geoloc except Exception, E:return ' unregistered ' def Main (): parser = Optparse. Optionparser (' [*] Usage:./geoprint.py-f <file> ') parser.add_option (' F ', dest= ' fileName ', type= ' string ', help= ' Specify target filename ' (options,args) = Parser.parse_args () filename = options.filename if filename = No
        Ne:print Parser.usage exit (0) else:f = open (fileName) Pcap = Dpkt.pcap.Reader (f)
 Printpcap (pcap) if __name__ = = ' __main__ ': Main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.