LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2022, 04:15 PM   #1
Varister
LQ Newbie
 
Registered: Oct 2021
Posts: 7

Rep: Reputation: Disabled
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()

Last edited by Varister; 02-12-2022 at 04:20 PM.
 
Old 02-12-2022, 04:19 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557

You must use "[code]..[/code]" to preserve formatting - this is vital for a language like Python.

(There is also a "Preview Post" button - it should be standard practise to use this when creating a new thread.)


As for the issue itself, what happens when you run it - i.e. where is the code deviating from what you expect to occur?


Last edited by boughtonp; 02-12-2022 at 04:21 PM.
 
Old 02-13-2022, 11:04 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
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.

Last edited by dugan; 02-13-2022 at 11:09 AM.
 
Old 02-14-2022, 10:13 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.

"Actum Ne Agas: Do Not Do A Thing Already Done."
 
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Analyze squid log files for analyze pattern harshaabba Linux - Software 1 10-13-2011 09:21 PM
How to capture and analyze an IP packet savio_hit Programming 3 08-25-2011 07:29 AM
A packet filter using libipq which uses ether type field to capture the packet can26_manish Programming 2 10-16-2007 05:35 AM
how do i read the data in the packet that i have captured after packet capture? gajaykrishnan Programming 23 04-19-2006 05:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration