Reputation: 193
How could I read with tn.read in Python, from the output below, only the "Ont SN" only the thing without what is written in the brackets, so 485754437D85CA9E , "F/S/P" and the "Ont EquipmentID" ?
Number : 1
F/S/P : 0/17/7
Ont SN : 485754437D85CA9E (HWTC-7D85CA9E)
Password : 0x00000000000000000000
Loid :
Checkcode :
VendorID : HWTC
Ont Version : 159D.A
Ont SoftwareVersion : V5R019C00S100
Ont EquipmentID : EG8145V5
Ont autofind time : 2020-12-15 09:35:48+01:00
Number : 2
F/S/P : 0/17/7
Ont SN : 48575443A9517A9E (HWTC-A9517A9E)
Password : 0x00000000000000000000
Loid :
Checkcode :
VendorID : HWTC
Ont Version : 159D.A
Ont SoftwareVersion : V5R019C00S100
Ont EquipmentID : EG8145V5
Ont autofind time : 2020-12-15 08:47:37+01:00
The number of GPON autofind ONT is 2
Summarizing I would need the following data:
0/17/7
48575443A9517A9E
EG8145V5
0/17/7
485754437D85CA9E
EG8145V5
I tried to do this, and I'm trying to obtain a JSON, but it returns every time the same list with the same object
def autofind(ip, user, password):
print('Connecting to OLT {0}'.format(ip))
try:
tn = telnetlib.Telnet(ip, 23, 10)
except Exception as e:
print("Error connecting to OLT, please check the IP address")
sys.exit()
tn.read_until(b"name:")
tn.write(user.encode('utf-8') + b"\n")
time.sleep(.3)
tn.read_until(b"password:")
tn.write(password.encode('utf-8') + b"\n")
time.sleep(.3)
tn.write(b"enable\n")
time.sleep(.3)
# autofind
tn.write("display ont autofind all ".encode('utf-8') + b"\n")
return_lineid = tn.read_until('The number of GPON'.encode('utf-8'), 3).decode('utf-8')
data_return = return_lineid.splitlines()
records = []
current_record = {}
serials = []
f_s_p = []
passwords = []
autofind_list = []
autofind_object = {}
for line in data_return:
line = line.strip()
autofind_list.append(autofind_object)
if not line: # empty line
records.append(current_record)
current_record = {}
else:
if "F/S/P" in line:
key, value = line.split(':')
key = key.strip()
value = value.strip()
current_record[key] = value
f_s_p.append(current_record[key])
autofind_object['F/S/P'] = value
if "Ont SN" in line:
key, value = line.split(':')
key = key.strip()
value = value.strip()
current_record[key] = value
# regex for removing brackets for example (HWTC-7D85CA9E)
serial = re.sub(r'\(.*\)', '', value)
serials.append(serial)
autofind_object['SN'] = serial
if "Password" in line:
key, value = line.split(':')
key = key.strip()
value = value.strip()
if value == '0x00000000000000000000':
passwords.append(None)
autofind_object['Password'] = None
autofind_list.append(autofind_object)
else:
passwords.append(current_record[key])
autofind_object['Password'] = current_record[key]
print("These are the serials number in autofind: ")
print(serials)
print("These are the F/S/P:")
print(f_s_p)
print("These are the passwords:")
print(passwords)
print (autofind_list)
output:
Connecting to OLT 10.240.0.19
These are the serials number in autofind:
['485754437D85CA9E ', '48575443A9517A9E ']
These are the F/S/P:
['0/17/7', '0/17/7']
These are the passwords:
[None, None]
[{'F/S/P': '0/17/7', 'SN': '48575443A9517A9E ', 'Password': None}, {'F/S/P': '0/17/7', 'SN': '48575443A9517A9E ', 'Password': None}]
---
End of the script!
Time elapsed: 4.27075457572937 seconds
Thank you in advance for the help
Upvotes: -1
Views: 359
Reputation: 7332
Here's a snippet that should turn those lines into a list of dictionaries:
records = []
current_record = {}
for line in data_return:
line = line.strip()
if not line: #empty line
records.append(current_record)
current_record = {}
else:
key, value = line.split(':')
key = key.strip()
value = value.strip()
current_record[key] = value
Not the full answer, but hopefully a helpful hint. You can then process serial afterwards, a list comprehension would be a good way to do that.
Upvotes: 1