0

First of all I have search the similar issues of mine but none able to answer my question above. I hope you guys could advise me further.

I' running a script to extract data from a list of network equipment and save the value onto json file for example below

json-1 = {
  "channel": "scanner",
  "action": "create_device",
  "table": "U2",
  "device":[]
}
data = "device_name","ip_address","lldp_neighbors"

Then a line of code is used to get data of devicename,ipaddress and lldp, return the value, extract it and save it onto the data list above. For example

my[data[0]] = connection.find_prompt().rstrip('>') #to get hostname
my[data[1]] = device['ip'] #to get ip address
my[data[2]] = connection.send_command('show lldp neighbors | display xml') 
#to get lldp data in xml format

json1["device"].append(my) #append my data to device

For my[data[2]], lldp neighbors will return data in xml format and convert that xml data onto json format file like below

LLDP NEIGHBORS DETAILS:-

"lldp_neighbors": [
{
"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": "host.jnpr.net"
}

My questions here is how can i add lldp neighbors detail above (json data) onto temp[data[2]] of json-1 so that the final json file json.dump(json-1, fp) generated will be like below, nested json file

{
  "channel": "scanner",
  "action": "create_device",
  "table": "U2",
  "device": [
    {
      "device_name": "rtr1.wer",
      "ip_address": "1.1.1.1",
      "lldp_neighbors": [
      {
       "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": "host.jnpr.net"
      }
    ]
  ]
}

I really hope someone could point me to the right path...i'm stuck ...please assist me. Thank you.

chenoi
  • 575
  • 3
  • 8
  • 30

1 Answers1

0

Your data should be a dictionary type, now a tuple type


data = "device_name","ip_address","lldp_neighbors"
# change to
data = {"device_name": "","ip_address": "","lldp_neighbors": []}
my[data["device_name"]] = connection.find_prompt().rstrip('>') #to get hostname
my[data["ip_address"]] = device['ip'] #to get ip address
my[data["lldp_neighbors"]] = connection.send_command('show lldp neighbors | display xml') 
mamian
  • 66
  • 5
  • Hi, thanks for your response and advise. I have tried and it give me an error of ... my[data["lldp_neighbors"]] = connection.send_command('show lldp neighbors | display xml') TypeError: unhashable type: 'list' – chenoi Feb 21 '19 at 23:53
  • I comment code line related to lldp_neighbors and remain only for hostname and ip ...code can be executed without error and generated mydevicesoutput.json but in the json only show ip address without the key and no hostname key and value in the json file generated as below {"channel": "scanner", "action": "create_device", "table": "U2", "device": [{"": "1.1.1.1"}, {"": "1.1.1.2"}]} No hostname key/value and no ipaddress key... – chenoi Feb 22 '19 at 00:07
  • can you print result of `my` – mamian Feb 24 '19 at 06:20