我通过字符串执行 Cisco 命令没有问题cmd = 'show version | include IOS'
from netmiko import ConnectHandler
cisco_device = {
'device_type': 'cisco_ios',
'ip': 'Router1',
'username': 'u',
'password': 'p'
}
# string OK
cmd = 'show version | include IOS'
print(type(cmd))
net_connect = ConnectHandler(**cisco_device)
output = net_connect.send_command(cmd)
print(output)
输出
user@linux:~$ python script-str.py
<class 'str'>
Cisco IOS Software, 3700 Software (C3725-ADVENTERPRISEK9-M), Version 12.4(15)T7, RELEASE SOFTWARE (fc3)
user@linux:~$
但是,当我将其更改为列表时,它不起作用。
from netmiko import ConnectHandler
cisco_device = {
'device_type': 'cisco_ios',
'ip': 'Router1',
'username': 'u',
'password': 'p'
}
# list problem
cmd = ['show version | include IOS']
print(type(cmd))
net_connect = ConnectHandler(**cisco_device)
output = net_connect.send_command(cmd)
print(output)
输出
user@linux:~$ python script-list.py
<class 'list'>
Traceback (most recent call last):
File "script-list.py", line 14, in <module>
output = net_connect.send_command(cmd)
File "/home/user/.local/lib/python3.7/site-packages/netmiko/utilities.py", line 347, in wrapper_decorator
return func(self, *args, **kwargs)
File "/home/user/.local/lib/python3.7/site-packages/netmiko/base_connection.py", line 1378, in send_command
command_string = self.normalize_cmd(command_string)
File "/home/user/.local/lib/python3.7/site-packages/netmiko/base_connection.py", line 1532, in normalize_cmd
command = command.rstrip()
AttributeError: 'list' object has no attribute 'rstrip'
user@linux:~$
第二个代码有什么问题list
以及如何解决?