-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_port_statistics.py
executable file
·98 lines (77 loc) · 2.46 KB
/
get_port_statistics.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
import requests
from dotenv import load_dotenv
import os
import sys
import datetime
from bs4 import BeautifulSoup
from hurry.filesize import size
load_dotenv()
NETGEAR_PASSWORD = os.getenv('NETGEAR_PASSWORD')
NETGEAR_IPV4 = os.getenv('NETGEAR_IPV4')
data = {'password': NETGEAR_PASSWORD}
admin_url = (f'http://{NETGEAR_IPV4}')
# Routes
login_url = (f'{admin_url}/login.cgi')
port_statistics_url = (f'{admin_url}/port_statistics.htm')
s = requests.Session()
post_response = s.post(
login_url,
data=data
)
response = s.get(port_statistics_url)
# Abort if wrong password
text = 'The password is invalid.'
if text in post_response.text:
print('🤦🏽 Wrong Password!')
sys.exit()
# Abort if maximum connections reached
text = 'The maximum number of attempts has been reached'
if text in post_response.text:
print('🔥 Slow down! Maximum connections reached')
sys.exit()
soup = BeautifulSoup(response.text, 'html.parser')
port_statistics = {}
switch_port_cells = soup.select('td.firstCol')
# Create Dict of Switch Ports
for cell in switch_port_cells:
switch_port = int(cell.get_text().strip())
port_statistics[switch_port] = {
'BytesReceived': 0,
'BytesSent': 0,
'CRCErrorPackets': 0
}
# Populate Dict
# Bytes Received
rx_cells = soup.select('input[name="rxPkt"]')
switch_port = 1
for cell in rx_cells:
# Convert to bytes Hex to Decimal
nbytes_rx = int(cell['value'].strip(), 16)
# Store a nice human readable string
port_statistics[switch_port]['BytesReceived'] = nbytes_rx
switch_port += 1
# Bytes Sent
tx_cells = soup.select('input[name="txpkt"]')
switch_port = 1
for cell in tx_cells:
# Convert to bytes Hex to Decimal
nbytes_tx = int(cell['value'].strip(), 16)
port_statistics[switch_port]['BytesSent'] = nbytes_tx
switch_port += 1
# CRC Error Packets
crc_cells = soup.select('input[name="crcPkt"]')
switch_port = 1
for cell in crc_cells:
# Convert to bytes Hex to Decimal
crc_packet = int(cell['value'].strip(), 16)
port_statistics[switch_port]['CRCErrorPackets'] = crc_packet
switch_port += 1
# Output in a human readable format
for port_id, stats in port_statistics.items():
all_zero = all(v == 0 for v in stats.values())
if not all_zero:
nice_rx = size(stats["BytesReceived"])
nice_tx = size(stats["BytesSent"])
print(
f'''Port {port_id}: [RX: {nice_rx}] [TX: {nice_tx}] [CRC: {stats["CRCErrorPackets"]}]''')