-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork_analyser.py
More file actions
34 lines (26 loc) · 1.04 KB
/
Copy pathNetwork_analyser.py
File metadata and controls
34 lines (26 loc) · 1.04 KB
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
#task 5
from scapy.all import sniff
from scapy.layers.inet import IP, TCP, UDP, ICMP
def packet_callback(packet):
"""Function to process captured packets"""
if IP in packet:
src_ip = packet[IP].src
dst_ip = packet[IP].dst
protocol = packet[IP].proto
print(f"\n[Packet Captured] Source: {src_ip} → Destination: {dst_ip} | Protocol: {protocol}")
# Handling TCP Packets
if TCP in packet:
src_port = packet[TCP].sport
dst_port = packet[TCP].dport
print(f"TCP Packet | Src Port: {src_port}, Dst Port: {dst_port}")
# Handling UDP Packets
elif UDP in packet:
src_port = packet[UDP].sport
dst_port = packet[UDP].dport
print(f"UDP Packet | Src Port: {src_port}, Dst Port: {dst_port}")
# Handling ICMP Packets
elif ICMP in packet:
print("ICMP Packet Detected")
# Sniff network packets (Requires admin/root privileges)
print("Starting packet sniffer...")
sniff(prn=packet_callback, store=False)