This is for a school assignment Relatively new to Python, and need some help. I'm required to read a PCAP file, and print out the source and destination IP's to a new file. I'm not allowed to use regex, I can't read the whole file at once, and I must use loops, functions (must accept one parameter and return a value), splits, and lists and IPs are in IPv4 format.
Are classes too complex for this problem? EDIT: Where I am so far is below: The search performed below is pulling the wrong IPs. I was advised to search by find the No. Time Source Destination Protocol
and then print the IPs from the line underneath it. I'm working on how to filter based on XXX.XXX.XXX format and will let you know how it goes :)
def pcapreader():
#open the file and print each line using readlines to a variable
#must replace file path with your present file location
with open (r"filepath", "r") as f:
f1=f.readlines()
for x in f1:
if "Internet Protocol Version 4, Src:" in x:
ips = x.split("Src: ")[1].split(",")
src = ips[0]
dst = ips[1].split("Dst: ")[1]
print("Src: {}\nDst:{}".format(src, dst))
f.close()
def main ():
pcapreader()
main()
I've attached a sample of the PCAP I need to read as well.
Any help would be appreciated! Thanks so much! :)