简体   繁体   中英

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$

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.

the problem is .send_command() wait for expected string to stop. since you use 'device_type': "cisco_ios" the expected string is '#'.

when you send the command 'reload', '#' will never come, thats why you get this error. you can also use send command like below:

.send_command('your command', expect_string = '[#\?$]')

this way you can change the excepted string to finish this function. You can also use regex expression inside expect_string .

I have been playing around with this lately and here is my 2 cents. Good luck guys!

Step 1: Create a connection handler

import netmiko

connection = netmiko.ConnectHandler(ip="192.168.30.161", device_type='cisco_ios', username='cisco', password='cisco123')

Step 2: Check connection

connection

Step 3: For Cisco routers and switches, You are expecting expect_string='[confirm]'

connection.send_command('reload', expect_string='[confirm]') 'Proceed with reload? [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

Outcome:

Your Cisco router or switch should reload.

*Apr 14 08:06:18.842: %SYS-5-RELOAD: Reload requested by cisco on vty0 (192.168.30.181). Reload Reason: Reload command.

*Apr 14 08:06:21.873 Reload requested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM