forked from stephanlascar/unifi-gateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaquire.py
58 lines (42 loc) · 1.62 KB
/
aquire.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
from collections import namedtuple
from pprint import pprint
import os
InterfaceData = namedtuple('InterfaceData', ['rx_bytes', 'rx_packets', 'rx_errors', 'rx_dropped',
'rx_fifo', 'rx_frames', 'rx_compressed', 'rx_multicast',
'tx_bytes', 'tx_packets', 'tx_errors', 'tx_dropped',
'tx_fifo', 'collitions', 'carrier', 'tx_compressed'])
def network_statistics():
with open('/proc/net/dev') as _f:
data = {}
dump = _f.readlines()
for _line in dump[2:]:
line = _line.split(':')
tempdata = []
for _d in line[1].split():
tempdata.append(int(_d))
data[line[0].strip()] = InterfaceData(*tempdata)
return data
def get_macaddress(interface):
if not os.path.isdir(f'/sys/class/net/{interface}'):
raise KeyError('The specified interface {interface} is not found')
with open(f'/sys/class/net/{interface}/address') as _f:
mac = _f.read().strip()
return mac
def list_interfaces():
return os.listdir('/sys/class/net/')
def uptime():
with open('/proc/uptime', 'r') as f:
_ut, _ = f.readline().split(' ')
uptime_fl = float(_ut)
return int(uptime_fl)
if __name__ == '__main__':
print('Network statistics')
netstat = network_statistics()
pprint(netstat)
print()
print('Listing interfaces')
print(list_interfaces())
print('Interface Mac addresses:')
for i in list_interfaces():
mac = get_macaddress(i)
print(f'{i:10} {mac}')