-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathip_stats.py
33 lines (25 loc) · 1.07 KB
/
ip_stats.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
from scapy.all import sniff, IP
ip_stats = {}
def get_info_from_packet(packet) -> None:
"""
Categorizes the source and destination IP addresses of the packet
"""
global ip_stats
if packet.haslayer(IP):
if (ip_stats.get(packet.getlayer(IP).src) != None):
ip_stats[packet.getlayer(IP).src]["out"] = ip_stats[packet.getlayer(IP).src]["out"] + 1
if (ip_stats.get(packet.getlayer(IP).dst) != None):
ip_stats[packet.getlayer(IP).dst]["in"] = ip_stats[packet.getlayer(IP).dst]["in"] + 1
else :
ip_stats[packet.getlayer(IP).src] = {"in": 0, "out": 1}
ip_stats[packet.getlayer(IP).dst] = {"in": 1, "out": 0}
def simple_ip_stats(count:int) -> None:
"""
Sniffs <count> number of packets and prints the number of incoming and outgoing requests for each IP address
"""
capture = sniff(count=count, prn=get_info_from_packet)
for i in ip_stats:
print(i)
print("Incoming requests : ", ip_stats[i]["in"])
print("Outgoing requests : ", ip_stats[i]["out"])
print()