我只是使用netmiko模块在CISCO交换机设备上玩,并在json文件中使用我的凭据,如IP地址,密码,用户名,并将该json文件调用到python脚本中以获取这些详细信息。在执行此操作时我收到了广告错误,我已在下面提供了详细信息,请说明我做错了什么......
$ cat CiscoNet_6.py
#!/usr/bin/python3
from __future__ import absolute_import, division, print_function
import netmiko
import paramiko
import json
######################################
## JSON: Javascript object Notation ##
######################################
# creating a tuple
netmiko_exceptions = (netmiko.ssh_exception.NetMikoAuthenticationException,
netmiko.ssh_exception.NetMikoTimeoutException)
with open('devices.json') as dev_file:
devices = json.load(dev_file)
for device in devices:
try:
print('-'*79)
print('Net Device Address Is: -->', device['ip'])
#print('Net Device Address Is: -->', device)
connection = netmiko.ConnectHandler(**device)
print(connection.send_command('show clock'))
print('-'*79)
connection.disconnect()
except netmiko_exceptions as e:
#print('Authentication failed to', 'Device')
print('Failed to', device['ip'], e)
#print('Failed to', device, e)
我的JSON文件devices.json
:
[
{
"ip: "192.168.0.200",
"device_type": "cisco_ios",
"username": "nettest",
"password": "cisco123"
}
]
错误:
Traceback (most recent call last):
File "./CiscoNet_6.py", line 18, in <module>
devices = json.load(dev_file)
File "/python/v3.6.1/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/python/v3.6.1/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/python/v3.6.1/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/python/v3.6.1/lib/python3.6/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 3 (char 7)
答案 0 :(得分:0)
也许这就是为什么
"ip*"*: "192.168.0.200",