12-22-2022 01:21 AM
Hi Everyone,
I am trying to use PYATS to pull some LLDP neighbor information from some switches.
The format I get out of it is nested JSON, but I want to have the information in a easy to iterate over list format.
As you can see above the LLDP information is neste. The best for me would be to have the data in a list contaning the interface neighbors and id.
How do I convert this format to something easy to iterate over like the CDP output from PYATS?
Solved! Go to Solution.
12-23-2022 12:12 AM
Hi
You may use this function:
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
Complete example:
import json
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
lldp = {
"interfaces": {
"GigabitEthernet2": {
"port_id": {
"Gi0/3": {
"neighbors": {
"IOSvL2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet3": {
"port_id": {
"Gi0/2": {
"neighbors": {
"IOSv.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet4": {
"port_id": {
"Gi0/1": {
"neighbors": {
"IOSv2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
}
},
"total_entries": 3
}
print(json.dumps(genieLldp_2_listOfDicts(lldp), indent=1))
Result:
[
{
"local_int": "GigabitEthernet2",
"remote_int": "Gi0/3",
"neighbor": "IOSvL2.lab"
},
{
"local_int": "GigabitEthernet3",
"remote_int": "Gi0/2",
"neighbor": "IOSv.lab"
},
{
"local_int": "GigabitEthernet4",
"remote_int": "Gi0/1",
"neighbor": "IOSv2.lab"
}
]
HTH
Marcel
12-23-2022 12:12 AM
Hi
You may use this function:
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
Complete example:
import json
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
lldp = {
"interfaces": {
"GigabitEthernet2": {
"port_id": {
"Gi0/3": {
"neighbors": {
"IOSvL2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet3": {
"port_id": {
"Gi0/2": {
"neighbors": {
"IOSv.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet4": {
"port_id": {
"Gi0/1": {
"neighbors": {
"IOSv2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
}
},
"total_entries": 3
}
print(json.dumps(genieLldp_2_listOfDicts(lldp), indent=1))
Result:
[
{
"local_int": "GigabitEthernet2",
"remote_int": "Gi0/3",
"neighbor": "IOSvL2.lab"
},
{
"local_int": "GigabitEthernet3",
"remote_int": "Gi0/2",
"neighbor": "IOSv.lab"
},
{
"local_int": "GigabitEthernet4",
"remote_int": "Gi0/1",
"neighbor": "IOSv2.lab"
}
]
HTH
Marcel
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: