1

ISSUE:1 Appreciate someone could advise me further. I want to ssh to my equipment using python script and using netmiko module. I get the ssh details (json file) via api request and used the data to run the script. However I'm getting error below. I hope someone could advise and show me the way.

I miss something here, but I don't know how to resolve it.Please correct and lead me. Thanks.

The json file is create as snippet below;

response = requests.post(url,data=data,headers=headers,verify=False)
dictionary_info = response.json() 
with open('devices.json', 'w') as fp:
  json.dump(dictionary_info, fp, indent=4, sort_keys=True)

devices.json file as follows;

{
"device": [
     {
        "login": "test1",
        "ip": "10.10.10.1",
        "password": "test1",
        "device_type": "cisco_ios"
    },
    {
        "login": "test1",
        "ip": "10.10.10.2",
        "password": "test1",
        "device_type": "cisco_ios"
    },
    {
        "login": "test1",
        "ip": "10.10.10.3",
        "password": "test1",
        "device_type": "cisco_ios"
    },
    {
        "login": "test1",
        "ip": "10.10.10.4",
        "password": "test1",
        "device_type": "cisco_ios"
    }
],
"status": "SUCCESS"
}

when i run the script below (snippet) , it will return an error below

File "devices.py", line 18, in <module>
print('Connecting to device:',device['ip'])
TypeError: string indices must be integers

for device in devices:
try:
    print('~' * 79)
    print('Connecting to device:',device['ip'])
    connection = netmiko.ConnectHandler(**device)
    print(connection.send_command('show interface'))
    connection.disconnect()
except netmiko_exceptions as e:
    print('Failed to ', device['ip'], e)

I should be able to run the script and ssh to each devices based on the ssh details provided by the json file. Somehow the code need to be modified further just I don't know how. Please assist me. Thanks


I edit the content of devices.json..I remove the curly bracket {}, the object name 'device' and 'status' and just remain the list[] as follows;

   [
    {
        "username": "scnpa",
        "ip": "10.10.10.1",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "username": "scnpa",
        "ip": "10.10.10.2",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "username": "scnpa",
        "ip": "10.10.10.3",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "username": "scnpa",
        "ip": "10.10.10.4",
        "password": "123",
        "device_type": "cisco_ios"
    }
   ]

Today, I just try n error...and run back the script and it works..using editable json file above... I know this is not the solution as final I will get the json file that i shared earlier (with curly bracket {} and etc) or is there a way, I can get like the format above [{},{},{},{}]? Create file from json api response and only get the {{},{}.{},{}] content...?

or remain the format as it is...

Maybe need to modified the python script at the for loop part. I try remove the device and I'm getting different error.. change the 'device' to other name and still give me error 'TypeError: string indices must be integers'. No idea...

for device in devices:
try:
    print('~' * 79)
    print('Connecting to device:',device['ip'])
    connection = netmiko.ConnectHandler(**device)
    print(connection.send_command('show interface'))
    connection.disconnect()


ISSUE:2 I CREATE SEPARATE POST

I have another questions regarding reading the response json file (string) query from the API. For example json response as follow

{
"status": "SUCCESS",
"device": [
    {
        "model":"XXXX-A",
        "username": "scnpa1",
        "ip": "10.10.10.1",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "model":"XXXX-A",
        "username": "scnpa2",
        "ip": "10.10.10.2",
        "password": "456",
        "device_type": "cisco_ios"
    }
]

}

How to ensure only specific keys/values such as name and password and able to print and create the file as a json file (list) that can be read as an input by python script. Expect it will be like this below

{
"status": "SUCCESS",
"device": [
    {
        "username": "scnpa1",
        "ip": "10.10.10.1",
        "password": "123"
    },
    {
        "username": "scnpa2",
        "ip": "10.10.10.2",
        "password": "456"
    }
]

}

I use the code below but it will create json file with all the parameters

response = requests.post(url,data=data,headers=headers,verify=False)
dictionary_info = response.json() 
with open('devices.json', 'w') as fp:
  json.dump(dictionary_info, fp, indent=4, sort_keys=True)

Please advise me. Thanks

chenoi
  • 575
  • 3
  • 8
  • 30
  • Did you load the file correctly? How do you create `devices`? – Burhan Khalid Jan 09 '19 at 09:16
  • Hi sir, devices created based on response call from api and use json.dump() – chenoi Jan 10 '19 at 00:19
  • @chenoi can you show how you create the variable `devices`? And also, please, show the output of `print(devices)` right before the `for` loop. – JoshuaCS Jan 10 '19 at 03:20
  • Hi, Thanks...the issue1 resolved. I have issue2 that related to pickup specifc parameters from the response json made. Perhaps you could advise me on this matter. Thanks – chenoi Jan 10 '19 at 04:52
  • Issue 2 have been separately created to be more clear and focus. Thanks https://stackoverflow.com/questions/54138775/how-to-select-specific-key-value-of-an-object-in-json-via-python – chenoi Jan 11 '19 at 00:24

1 Answers1

0

You should change the loop to

for device in devices['mydevice']:
Danil
  • 4,781
  • 1
  • 35
  • 50