使用 Netmiko 从文本文件发送思科命令失败。send_config_set有效,但send_config_from_file不起作用


  • 我一直使用这个链接作为指南。

https://pyneng.readthedocs.io/en/latest/book/18_ssh_telnet/netmiko.html: ~:文本= Netmiko % 20 % 20 % 20模块% 20,pip % 20安装% 20 Netmiko

XXXXXX:/Test # cat test2.py
from netmiko import ConnectHandler
with open('commands_ios') as f:
commands_list = f.read().splitlines()
cisco_D = {
'device_type': 'cisco_ios',
'host':   '10.1.1.1',
'username': 'username',
'password': 'password',
}
net_connect = ConnectHandler(**cisco_D)
output = net_connect.send_config_from_file('commands_ios.txt')
print(output)

XXXXXX:/Test # python3 test2.py

Traceback (most recent call last):
File "test2.py", line 14, in <module>
output = net_connect.send_config_from_file('commands_ios.txt')
File "/usr/lib/python3.6/site-packages/netmiko/base_connection.py", line 1808, in send_config_from_file
with io.open(config_file, "rt", encoding="utf-8") as cfg_file:
FileNotFoundError: [Errno 2] No such file or directory: 'commands_ios.txt'
AL108564:/Test #

您可以像下面这样使用readlines而不是splitlines。如果你有。txt>你应该这样修改=>"commands_ios.txt">

with open('commands_ios.txt') as f:
commands_list = f.readlines() #(or commands_list = f.splitlines())

#commands_list = "".join(commands_list ) => If you need convert string
#commands_list = lines.strip() => If you need remove spaces
#print(commands_list)

脚本运行正常;commands_ios.txt文件由Cisco show命令组成,通常在启用模式下输入,send_config_from_file只在全局配置模式下输入命令,因此会抛出错误。

最新更新