from scapy.all import sniff
import matplotlib.pyplot as plt
# ---------------------------------------
# Packet Counters
# ---------------------------------------
protocol_counts = {
"TCP": 0,
"UDP": 0,
"ICMP": 0,
"Other": 0
}
# ---------------------------------------
# Packet Handler
# ---------------------------------------
def process_packet(packet):
if packet.haslayer("TCP"):
protocol_counts["TCP"] += 1
elif packet.haslayer("UDP"):
protocol_counts["UDP"] += 1
elif packet.haslayer("ICMP"):
protocol_counts["ICMP"] += 1
else:
protocol_counts["Other"] += 1
# ---------------------------------------
# Capture Packets
# ---------------------------------------
def capture_packets(packet_limit=200):
print("Capturing packets...")
sniff(prn=process_packet, count=packet_limit)
print("Capture complete.")
# ---------------------------------------
# Visualization
# ---------------------------------------
def visualize():
labels = list(protocol_counts.keys())
values = list(protocol_counts.values())
plt.figure(figsize=(6,6))
plt.pie(
values,
labels=labels,
autopct='%1.1f%%',
startangle=140
)
plt.title("Network Protocol Distribution")
plt.show()
# ---------------------------------------
# MAIN
# ---------------------------------------
if __name__ == "__main__":
capture_packets(300)
print("\nProtocol counts:")
for proto, count in protocol_counts.items():
print(f"{proto}: {count}")
visualize()
No comments:
Post a Comment