我正在使用Netmiko用Python编写脚本来自动执行450多个路由器的升级。它们是881 / 887、1921和2901的组合。
我正在使用Netmiko,并已研究出如何确定模型并复制适当的IOS版本。我遇到的问题是重新加载路由器。我正在尝试使用send_command_expect
函数,但无法使其正常工作。
这是我试图实现的方式。任何帮助表示赞赏。
import getpass
import time
from netmiko import ConnectHandler, file_transfer
host = "10.0.0.1"
u = "cisco"
p = "cisco"
source_file = "c800-universalk9-mz.SPA.155-3.M5.bin"
router = {
'device_type': "cisco_ios",
'ip': host,
'username': u,
'password': p,
}
try:
ssh_conn = ConnectHandler(**router)
print ("Connection successful\n")
except:
print ("Login failure\n")
sys.exit()
output = ssh_conn.send_command_expect('write mem')
output += ssh_conn.send_command('reload')
output += ssh_conn.send_command('\n')enter code here
下面是错误消息:
192-168-1-6:CiscoUpgrade sudarshanv$ python3 test.py
Connection successful
Traceback (most recent call last):
File "test.py", line 25, in <module>
output += ssh_conn.send_command('reload')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/netmiko/base_connection.py", line 1112, in send_command
search_pattern))
OSError: Search pattern never detected in send_command_expect: HomeRTR\#
192-168-1-6:CiscoUpgrade sudarshanv$
答案 0 :(得分:4)
我想通了,将解决方案发布在这里出于一般目的。 解决方案是使用send_command_timing函数而不是send_command。
答案 1 :(得分:0)
问题是.send_command()
等待预期的字符串停止。由于您使用'device_type': "cisco_ios"
,因此预期的字符串为“#”。
当您发送命令“重新加载”时,“#”将永远不会出现,这就是为什么您会收到此错误。您还可以使用如下的send命令:
.send_command('your command', expect_string = '[#\?$]')
这样,您可以更改例外字符串以完成此功能。您还可以在expect_string
中使用正则表达式。
答案 2 :(得分:0)
我最近一直在玩这个游戏,这是我的2美分。祝你好运!
第1步:创建连接处理程序
导入netmiko
连接= netmiko.ConnectHandler(ip =“ 192.168.30.161”,device_type ='cisco_ios',用户名='cisco',密码='cisco123')
第2步:检查连接
连接
第3步:对于Cisco路由器和交换机,您期望Expect_string ='[confirm]'
connection.send_command('reload',Expect_string ='[confirm]') 继续加载吗? [确认]'
connection.send_command('\ n')
OR
connection.send_command('y')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-packages/netmiko/utilities.py", line 347, in wrapper_decorator
return func(self, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 1368, in send_command
prompt = self.find_prompt(delay_factor=delay_factor)
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 1107, in find_prompt
self.write_channel(self.RETURN)
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 436, in write_channel
self._write_channel(out_data)
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 394, in _write_channel
self.remote_conn.sendall(write_bytes(out_data, encoding=self.encoding))
File "/usr/local/lib/python3.8/dist-packages/paramiko/channel.py", line 846, in sendall
sent = self.send(s)
File "/usr/local/lib/python3.8/dist-packages/paramiko/channel.py", line 801, in send
return self._send(s, m)
File "/usr/local/lib/python3.8/dist-packages/paramiko/channel.py", line 1198, in _send
raise socket.error("Socket is closed")
OSError: Socket is closed
结果:
您的Cisco路由器或交换机应重新加载。
* 4月14日08:06:18.842:%SYS-5-RELOAD:cisco在vty0(192.168.30.181)上请求重新加载。重新加载原因:重新加载命令。
* 4月14日08:06:21.873 请求重新加载