Description: You will be creating a program extractIP.py which will read from a file called wireShark.txt and extract all the ip addresses which can be of the format: 1,2 or 3 digits. 1, 2 or 3 digits.1, 2, or 3 digits. 1,2,3 digits Here are some examples of valid IPv4 addresses: 10.0.0.1, 12.123.123.123, 126.255.255.254, 191.255.255.254, 192.168.1.1, 239.255.255.255 And you will output them to another file called IPAddresses.txt , one per line, listing source and destination. Example: Source Destination 12.123.123.123 123.123.123.123 Requirements: You will read from a file called wireShark.txt which will be located in the same directory as your code You will write to a file called IPAddresses.txt which will be located in the same directory as your code All files will be opened and closed correctly. You will not use regex to parse the file. Instead, you will check the characters individually Your loops will be structured correctly. You will use one or more functions which will take a parameter or more, and return a value You will use lists You will use the split function You will write a function called CheckLine(currentLine) which will check if currentline is of the format: No. Time Source Destination Protocol Length Info Once you have identified this particular line, you will know that the following line has the IP addresses for source and destination in the second and third elements. 1 0.000000 192.168.1.180 239.255.255.250 SSDP 372 NOTIFY * HTTP/1.1 Your functions and main code will be in separate sections of your program. Your IP addresses will be formatted in IPv4 format, i.e. 1, 2 or 3 digits .1, 2 or 3 digits.1,2 or 3 digits. 1,2,3 digits It is ok to reuse the code you wrote for past homework including homework 4.
wireShark.txt
id source destination length
1 12.13.13.11 12.15.17.199 34
2 12.1.132.132 121.15.17.199 32
3 12.13.133.132 121.1532.17.199 31
4 12.131.13.132 12.15.17.199 38
5 12.1345.134.132 12.15.17.199 349
6 12.13a.134.132 12.15.17.199 349
#source code:
def check_ip(ip):
numbers=["0","1","2","3","4","5","6","7","8","9"]
che=ip.split(".")
check_digit=0
check_count=0
for i in range(len(che)):
for j in range(len(che[i])):
if che[i][j] not
in numbers:
check_digit=1
break
if(check_digit==0):
for i in range(len(che)):
if((len(che)==4)
and (len(che[i])<4 and len(che[i])>0)):
pass
else:
check_count=1
break
return check_count,check_digit
f=open("wireShark.txt","r")
data=[]
for line in f.readlines():
data.append(line.replace("\n",""))
ip_list=[]
for i in range(1,len(data),1):
list=data[i].split(" ")
ip_list.append([list[1],list[2]])
for i in range(len(ip_list)):
check1,check1d=check_ip(ip_list[i][0])
check2,check2d=check_ip(ip_list[i][1])
if(check1==0 and check2==0 and check1d==0 and
check2d==0):
f=open("ipaddress.txt","a+")
f.write(ip_list[i][0]+"
"+ip_list[i][1]+"\n")
f.close()
print("File created successfully")
#output:
#if you have any doubts comment below......
Get Answers For Free
Most questions answered within 1 hours.