-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_ripper.py
46 lines (35 loc) · 1.31 KB
/
ip_ripper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pyshark
# Function to extract IP addresses from a pcap file
def extract_ip_addresses(pcap_file):
unique_ips = set()
# Read the pcap file
cap = pyshark.FileCapture(pcap_file)
for packet in cap:
# Check if the packet has IP layer
if 'IP' in packet:
ip_src = packet.ip.src
ip_dst = packet.ip.dst
unique_ips.update([ip_src, ip_dst])
# Check if the packet has IPv6 layer
if 'IPv6' in packet:
ipv6_src = packet.ipv6.src
ipv6_dst = packet.ipv6.dst
unique_ips.update([ipv6_src, ipv6_dst])
# Close the capture file
cap.close()
return unique_ips
# Function to write IP addresses to a text file
def write_ips_to_file(file_path, ip_addresses):
with open(file_path, 'w') as file:
for ip in sorted(ip_addresses):
file.write(ip + '\n')
# Main function to prompt for input and write output
def main():
pcap_file = input("Enter the path to the pcap file: ") # Prompt for pcap file path
output_file = 'extracted_ips.txt' # Output file name
ip_addresses = extract_ip_addresses(pcap_file)
write_ips_to_file(output_file, ip_addresses)
print(f"IP addresses extracted and saved to {output_file}")
# Running the main function
if __name__ == "__main__":
main()