我想在下面请求您对我的代码的帮助。我有如下类似的搜索问题,但lldp输出不同,我尝试应用相同的操作但失败。 我无法将设备lldp输出解析到json文件中。有关信息,我正在使用netmiko库ssh到每个设备以获取主机名,ip和lldp信息的信息。到目前为止,该代码能够从设备输出中提取并解析主机名和ip值到下面的json文件中。
json1 = {
"channel": "scanner",
"action": "device_scan",
"table": "D2",
"device":[]
}
#data = {"hostname":"","ipaddress":"","lldpinfo": []}
data = "hostname","ipaddress","lldpinfo"
这是发送到设备并按要求返回输出并保存到下面的json文件中的命令
temp[data[0]] = connection.find_prompt().rstrip('>')
temp[data[1]] = device['ip']
temp[data[2]] = connection.send_command('show lldp neighbors')
json1["device"].append(temp)
with open('devices_info.json', 'w') as fp:
json.dump(json1, fp)
上面的命令将返回输出并保存到json文件中,而不是json中的lldp结构。请参见下图。
{"channel":"scanner","action":"device_scan","table":"D2","device":
[{"hostname":"rtr1.wtx","ipaddress":"10.10.1.1","lldpinfo":"\nLocal
Interface
Parent Interface Chassis Id Port info System Name\ge-
2/0/0 ae0 ac:4b:c8:92:67:c0 368
apg-hp \nge-2/0/1 ae0 ac:4b:c8:92:67:c0
369 apg-hp \nge-4/0/6 -
ac:4b:c8:92:67:c0 629 wtx-dl \nge-0/0/4 -
00:23:3e:58:3e:50 138739712 TVCX-vp \nge-0/0/2 -
ac:4b:c8:92:67:c0 gei_1/5 TVCX-vp \nge-3/3/0
- ac:4b:c8:92:67:c0 gei_2/3 TVCX-vp
\nge-2/2/4 - ac:4b:c8:92:67:c0 783
rtr-02-re \n\n{master}"}]}
上面,该代码能够提取和解析主机名,ipaddress和lldpinfo的值,但不能提取和解析lldpinfo的值。以下是该设备返回的lldp的确切原始输出
**********SCAN DEVICES**********
Local Interface Parent Interface Chassis Id Port info
System Name
ge-2/0/0 ae0 ac:4b:c8:92:67:c0 368
apg-hp
ge-2/0/1 ae0 ac:4b:c8:92:67:c0 369
apg-hp
ge-4/0/6 - ac:4b:c8:92:67:c0 629
wtx-dl
ge-0/0/4 - ac:4b:c8:92:67:c0 138739712
TVCX-vp
ge-0/0/2 - ac:4b:c8:92:67:c0 gei_1/5
TVCX-vp
ge-3/3/0 - ac:4b:c8:92:67:c0 gei_2/3
TVCX-vp
ge-3/3/4 - ac:4b:c8:92:67:c0 783
rtr-02-re
{master}
我希望lldpinfo的json文件输出具有如下形式
{
"channel": "scanner",
"action": "device_scan",
"table": "D2",
"device": [
{
"hostname": "rtr1.wtx",
"ipaddress": "10.10.1.1",
"lldpifo": [
{
"local-port": "xe-3/0/4.0",
"parent-interface": "ae31.0",
"chassis-id": "b0:c6:9a:63:80:40",
"port-info": "xe-0/0/0/0.0",
"system-name": "myhost"
}
]
]
}
我已经按照论坛管理员的建议使用字典列表对其进行了测试,但是缺少错误和主机名值。截至今天,我仍然无法将lldp输出正确解析到上面的json文件。感谢您在此问题上的支持。谢谢。