How can I create a Python program to analyze a packet capture file and identify the geolocations of IP addresses using GeoIP2?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I create a Python program to analyze a packet capture file and identify the geolocations of IP addresses using GeoIP2?
The following is what I have so far. I am trying to analyze geotest.pcap file and list each packet's city, country, source, and destination IP using GeoIP2.
Code:
import dpkt
import socket
import geoip2
import optparse
gi = geoip2.database("/home/user/GeoLite2-City.mmdb")
def retGeoStr(ip):
try:
rec = gi.record_by_name(ip)
city = rec['city']
country = rec['country_code3']
if city != '':
geoLoc = city + ', ' + country
else:
geoLoc = country
return geoLoc
except Exception as e:
return 'Unregistered'
def printPcap(pcap):
for (ts, buf) in pcap:
try:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
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
def main():
parser = optparse.OptionParser("usage%prog "+\
"-p <pcap filename> ")
parser.add_option('-p', dest='pathName', type='string',\
help='specify pcap filename')
(options, args) = parser.parse_args()
pcapFile = options.pcapFile
if options.pcapFile == None:
print (parser.usage)
exit(0)
f = open('geotest.pcap')
pcap = dpkt.pcap.Reader(f)
printPcap(pcap)
if __name__ == "__main__":
main()
Take out the try/except statements. All they're doing is preventing you from seeing and fixing actual issues.
If you want to use try/except, then the try block should be one statement long, and it should only catch the specific exception subclass thrown by that statement.
Have you first done a very thorough search of the Python package repositories, and of places such as GitHub and SourceForge?
No matter what it is that you're planning to do these days, it is a certainty that someone else has already done it better than you did. And also, that various people have written and debugged "packages" to help you do a lot of the heavy lifting without debugging all of it yourself.
No matter what it is you're doing, plan not to "start from scratch" on anything.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.