-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscapy_watcher.py
79 lines (62 loc) · 1.96 KB
/
scapy_watcher.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""scapy_watcher.py: watches network packets, calculates speed and totals per-port."""
__author__ = "Alex 'Chozabu' P-B"
__copyright__ = "Copyright 2016, Chozabu"
import netifaces
from threading import Thread
from scapy.all import *
last_time = time.time()
portcounts = {}
def calc_speeds():
global last_time
# update speed counters
new_time = time.time()
if new_time > last_time+1:
last_time+=1
for k in portcounts.keys():
v = portcounts[k]
diff = v['total'] - v['last']
v['last'] = v['total']
v['speed_raw'] = diff
v['speed'] = v['speed']*.9 + diff *.1
#find and print port using most bandwidth each second
topport = -1
topport_speed = -1
for k in portcounts.keys():
v = portcounts[k]
if v['speed'] > topport_speed:
topport = k
topport_speed = v['speed']
if topport != -1:
print("heaviest port:", topport, portcounts[topport])
def pkt_callback(pkt):
#pkt.show() # debug statement
#print(pkt.fields)
#print(pkt.fields_desc)
#print("dport", pkt['TCP'].dport, "sport", pkt['TCP'].sport)
#print()
datalen = len(pkt)
sport = pkt['TCP'].sport
if sport not in portcounts:
portcounts[sport] = {"total": 0, "last": 0, "speed_raw": 0, "speed": 0, "port": sport}
portcounts[sport]['total'] += datalen
calc_speeds()
def run():
ifaces = [iface for iface in netifaces.interfaces()
if netifaces.AF_INET in netifaces.ifaddresses(iface)]
sniff(iface=ifaces, prn=pkt_callback, filter="tcp", store=0)
def launch_watcher():
run()
def start_background_thread():
try:
t = Thread(
daemon=True,
target=launch_watcher,
kwargs={})
except:
t = Thread(
target=launch_watcher,
kwargs={})
t.daemon = True
t.start()
if __name__ == '__main__':
run()