Search code examples
pythonpython-3.xnetmiko

Python | Netmiko


I am writing a script in Python using Netmiko to Automate the Upgrade of 450+ routers. They are a mix of 881/887, 1921 and 2901.

I am using Netmiko and have worked out on how to determine the Model and copy the appropriate IOS version. What I am having issues with is reloading the router. I am trying to use the send_command_expect function but I have been unable to get it to work.

Here is how I am trying to achieve it. Any help is appreciated.

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

Below is the error message:

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$

Solution

  • I figured it out, Posting the solution here for the general good. The solution is to use send_command_timing function instead of send_command.