-
Notifications
You must be signed in to change notification settings - Fork 5
/
atten_checker.py
68 lines (57 loc) · 2.45 KB
/
atten_checker.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
import getpass
import telnetlib
import time
import re
import csv
from termcolor import colored, cprint
host = input("Enter OLT IP: ")
user = 'zte' # input("Enter username: ")
password = 'zte' # getpass.getpass()
tn = telnetlib.Telnet(host)
tn.read_until(b"Username:")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password:")
tn.write(password.encode('ascii') + b"\n")
tn.write("terminal length 0".encode('ascii') + b"\n")
time.sleep(1)
tn.write("sho run".encode('ascii') + b"\n")
time.sleep(1)
output_str = ''
fetch = ''
while fetch.find('\nend') == -1:
fetch = str(tn.read_very_eager().decode('ascii'))
output_str += fetch
if output_str.__contains__('More'):
tn.write(" ".encode('ascii') + b"\n")
time.sleep(0.1)
output_file = open('telnet_output.txt', 'w')
output_file.write(output_str)
output_file.close()
data = output_str.splitlines()
onus = re.findall('interface gpon-onu_\d+/\d+/\d+:\d+', output_str)
names = re.findall('(?<=name ).*', output_str)
results = []
with open('attenuation.csv', 'w', newline='') as csvfile:
atten_writer = csv.writer(csvfile, delimiter=',')
atten_writer.writerow(['Name', 'Port', 'Attenuation UP', 'Attenuation Down'])
for o in onus:
try:
name = re.search('(?<=name ).*', output_str[output_str.find(o):]).group(0).replace('\r', '')
cmd = "show pon power attenuation " + o[10:]
tn.write(cmd.encode('ascii') + b"\n")
time.sleep(0.5)
fetch = str(tn.read_very_eager().decode('ascii'))
up_db = re.search('\d\d.\d\d\d(?=\(dB\))', fetch).group(0).replace('\r', '')
down_db = re.search('\d\d.\d\d\d(?=\(dB\))', fetch).group(0).replace('\r', '')
result = "%s: %s: Up: %s Down: %s" % (name, o, up_db, down_db)
if((float(up_db) <= 28) and (float(down_db) <= 28)):
print(colored("%s: %s: Up: %s Down: %s" % (name, o, up_db, down_db), 'green'))
elif((float(up_db) <= 30) and (float(down_db) <= 30)):
print(colored("%s: %s: Up: %s Down: %s" % (name, o, up_db, down_db), 'yellow'))
elif((float(up_db) > 30) or (float(down_db) > 30)):
cprint("%s: %s: Up: %s Down: %s" % (name, o, up_db, down_db), 'white', 'on_red')
results.append(result)
atten_writer.writerow([name, o, up_db, down_db])
except Exception as ex:
print(colored("%s: %s: seems to be offline!" % (name, o), 'red'))