cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1322
Views
0
Helpful
6
Replies

python netmiko reload SG350 not working

rapture
Level 1
Level 1

Hi!

I'am trying to configure a SG350 Switch via Python and Netmiko. 

 

Typing reload, I have to answer to two questions:
You haven't saved your changes. Are you sure you want to continue ? (Y/N)[N] Y
This command will reset the whole system and disconnect your current session. Do you want to continue ? (Y/N)[N]

Via Netmiko seems that the first question is correctly answered but the second isn't.

 

My code:

connection.send_command("reload", expect_string = "You") # what expected after typed reload
connection.send_command_timing("Y") # answer to the first question
connection.send_command_timing("Y") # aswer to the second question (not working)

*******************************

* Some debug:

*******************************

DEBUG:netmiko:read_channel: reload

You haven't saved your changes. Are you sure you want to continue ? (Y/N)[N]
DEBUG:netmiko:Pattern found: (reload)


plutone#reload
DEBUG:netmiko:read_channel:
DEBUG:netmiko:write_channel: b'Y\n'
DEBUG:netmiko:read_channel: Y

DEBUG:netmiko:read_channel: This command will reset the whole system and disconnect your current session. Do you want to continue ? (Y/N)[N] N
plutone#
DEBUG:netmiko:read_channel:
DEBUG:netmiko:write_channel: b'Y\n'
DEBUG:netmiko:read_channel: Y
DEBUG:netmiko:read_channel:

% Unrecognized command
plutone#
DEBUG:paramiko.transport:EOF in transport thread

 

Please HELP! 

6 Replies 6

Hi

 If you are doing it with NetMiko because you are learning it and want to learn Python and NetMiko, so, this is the way to go. Doing, breaking and fixing.  If you share the code, would be easier to get helped.

 You can also use the https://community.cisco.com/t5/for-developers/ct-p/4409j-developer-home   I think this question make more sense there.

 However, if what you want is just reload the switch and you are not worring about which program language, then use Expect.  Expect is pretty simple and work pretty much well with interactive consoles like this. 

 

 Take a look on this very simple script and have fun.

Unoftunately I need to use Python and Netmiko because I have more to do... 

Please read the answer below...

Thank you.

rapture
Level 1
Level 1

Hi! Thank you for the answer.

I need to update the configuration of 700 switches around the world.

I am using python e netmiko.

I am able to apply the configuration via python and netmiko but to apply I have to send a reload command and I have to send a write memory command. The problem is the answer after the command! Expecially if there are two question after the command.

import sys
import logging
import netmiko
logging.basicConfig(stream = sys.stderr, level = logging.DEBUG)


def connectsw(conn_dict):
    connection = ''
    try:
        connection = netmiko.ConnectHandler(**conn_dict)
    except:
        print('{ip} credenziali errate'.format(ip = conn_dict['ip']))

    if connection:
        connection.clear_buffer()
        connection.send_command("reload", expect_string = "You") # what expected after typed reload
        connection.send_command_timing("Y") # answer to the first question
        connection.send_command_timing("Y") # aswer to the second question (not working)

if __name__ == '__main__': conn_dict = { 'device_type': 'cisco_ios', 'ip': '1.2.3.4', 'username': 'username', 'password': 'password', 'port': 22 } connectsw(conn_dict)

 

The code is the same as the previous one message... 

Thank you.

 

 

if connection:
        connection.clear_buffer()
        connection.send_command("reload", expect_string = "You") # what expected after typed reload
        connection.send_command_timing("Y") # answer to the first question
        connection.send_command_timing("Y") # aswer to the second question (not working)

 

Try to add some counter between the first Y and the second Y.

Probably the device is expecting Y and the script is sending YY.

Try to send the first Y and wait like 5 seconds and send the second Y

 

That´s why expect is better because it reads the prompt to react.

I have already tryied with time.sleep(10) for example... But nothing... The second Y is Unrecognized command.

So you are saying I am doing in the right way... 

Hate cisco! Hate theese switches! 

Thank you!

Cool man!  There´s always a way for everything. 

Please, move your post to the local I mentioned. There you can get more people to help you.

Mean while, I´ll take a look here and see what I get.