在 Python 中与 tn.read 一起阅读
Reading with tn.read in Python
我怎么能用 tn.read 在 Python 中读取,从下面的输出中,只有“Ont SN”只有括号中没有写的东西,所以 485754437D85CA9E ,“F/S/P" 和 "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
总结一下,我需要以下数据:
0/17/7
48575443A9517A9E
EG8145V5
0/17/7
485754437D85CA9E
EG8145V5
我尝试这样做,我试图获得一个 JSON,但它 returns 每次都有相同的列表和相同的对象
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)
输出:
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
提前感谢您的帮助
下面是一个片段,可以将这些行转换为字典列表:
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
不是完整答案,但希望能提供有用的提示。然后您可以在之后处理序列,列表理解将是一个很好的方法。
我怎么能用 tn.read 在 Python 中读取,从下面的输出中,只有“Ont SN”只有括号中没有写的东西,所以 485754437D85CA9E ,“F/S/P" 和 "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
总结一下,我需要以下数据:
0/17/7
48575443A9517A9E
EG8145V5
0/17/7
485754437D85CA9E
EG8145V5
我尝试这样做,我试图获得一个 JSON,但它 returns 每次都有相同的列表和相同的对象
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)
输出:
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
提前感谢您的帮助
下面是一个片段,可以将这些行转换为字典列表:
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
不是完整答案,但希望能提供有用的提示。然后您可以在之后处理序列,列表理解将是一个很好的方法。