ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.62k stars 1.31k forks source link

send_config_set command not entering commands in configuration mode #1758

Closed SusethiraRam closed 4 years ago

SusethiraRam commented 4 years ago

I'm facing issue with send_config_set command in nxos device with variant NEXUS7K alone. send-config_set command is executing commands in privilege mode and it is not entering configuration mode.

Here device is accessed via terminal server.

Device output: switch# switch# hostname SCLLAB-NSO1-CORE01 ^ % Invalid command at '^' marker. switch# username admin password ***** role network-admin ^ % Invalid command at '^' marker. switch# vrf context management ^ % Invalid command at '^' marker. switch# ip route 0.0.0.0/0 10.198.15.1 ^ % Invalid command at '^' marker. switch# interface mgmt0 ^ % Invalid command at '^' marker. switch# vrf member management ^ % Invalid command at '^' marker. switch# ip address 10.198.15.19/24 ^ % Invalid command at '^' marker. switch#

Below are the error snippet.

Traceback (most recent call last): File "provision_device.py", line 413, in overall_result = main(inventory_file) File "provision_device.py", line 378, in main configure_nxos_device(console_server_ip, dev_oob_ip, console_port, dev_name, platform) File "provision_device.py", line 93, in configure_nxos_device response = net_connect.send_config_set(config_command, delay_factor=5, cmd_verify=False) File "/usr/lib/python3.6/site-packages/netmiko/base_connection.py", line 1770, in send_config_set output += self.exit_config_mode() File "/usr/lib/python3.6/site-packages/netmiko/base_connection.py", line 1652, in exit_config_mode raise ValueError("Failed to exit configuration mode") ValueError: Failed to exit configuration mode

Python code where the issue is experiences is below,

def configure_nxos_device(console_server_ip, dev_oob_ip, console_port, dev_name, platform):
    print(f"Applying basic configuration on {dev_name}\n")
    try:
        net_connect = ConnectHandler(device_type="terminal_server",
                                     host=console_server_ip, username=CONSOLE_USER, password=CONSOLE_PASSWORD,
                                     port='30' + str(int(console_port)), global_delay_factor=5)
        config_command = ['hostname ' + dev_name,
                          'username admin password ' + DEVICE_PASSWORD + ' role network-admin',
                          'vrf context management', 'ip route 0.0.0.0/0 10.198.15.1', 'interface mgmt0',
                          'vrf member management', 'ip address ' + dev_oob_ip + '/24']
        net_connect.session_preparation()
        net_connect.write_channel("\r\n")
        time.sleep(SLEEP)
        net_connect.write_channel("\r\n")
        time.sleep(SLEEP)
        response = net_connect.read_channel()
        time.sleep(SLEEP)
        # Login to router with admin credentials
        if device_ssh(net_connect, console_server_ip, dev_name, dev_oob_ip) is True:
            print(f"Logged into Device {dev_oob_ip}\n")
            net_connect.write_channel("\r\n")
            # Dynamically reset the class back to the proper Netmiko class
            if prompt("#", net_connect, response):
                # Erase the device configurations
                net_connect.write_channel("\r\n")
                net_connect.send_command('write erase', expect_string=r'y/n')
                net_connect.send_command('y\r', expect_string=r'#')
                net_connect.send_command("reload", expect_string=r'y/n')
                net_connect.send_command_timing('y\r')
                response = net_connect.read_channel()
                print("Rebooting the device")
                print("*" * 60)
                print(f"\n {response} \n")
                print("*" * 60)
                if prompt("Abort Power On Auto Provisioning and continue with normal setup", net_connect,
                          response, max_loops=60):
                    net_connect.send_command('yes', expect_string=r'Do you want to enforce secure password standard')
                    net_connect.send_command('yes', expect_string=r'Enter the password')
                    net_connect.send_command(DEVICE_PASSWORD, expect_string=r'Confirm the password', max_loops=2, delay_factor=5, cmd_verify=False)
                    net_connect.send_command(DEVICE_PASSWORD,
                                             expect_string=r'Do you want to enable admin vdc', max_loops=2,
                                             delay_factor=5, cmd_verify=False)
                    net_connect.send_command('n', expect_string=r'the basic configuration dialog', max_loops=2,
                                             delay_factor=5, cmd_verify=False)
                    net_connect.send_command_timing('no')
                    response = net_connect.read_channel()
                    if device_ssh(net_connect, console_server_ip, dev_name, dev_oob_ip) is True:
                        net_connect.write_channel("\r")
                        response = net_connect.read_channel()
                        if prompt("#", net_connect, response):
                            logging.info("Connected after reboot\n")
                            logging.info("Configuring device with default "
                                         "configurations\n")
                            net_connect.write_channel("\r")
                            response = net_connect.send_config_set(config_command, delay_factor=5, cmd_verify=False)
                            net_connect.send_command_timing('write memory')
                            time.sleep(SLEEP)
                            logging.info("#" * 60)
                            logging.info("\nConfiguration applied:\n\n" + str(response))
                            logging.info("#" * 60)
                            net_connect.disconnect()
                            if net_connect.is_alive() == False:
                                logging.info(f"Connection to {dev_oob_ip} has closed\n")
                            else:
                                logging.info(f"Connection to {dev_oob_ip} has not closed\n")
                        else:
                            logging.error("Expected device password prompt not "
                                          "received after reboot")
                            return None
                    else:
                        logging.error(f"Device not connected after reboot - {response}")
                        return None
                else:
                    logging.error(f"Expected Auto Provisioning prompt not received after reboot "
                                  f"- {response}")
                    return None
            else:
                logging.error(f"Could not connect - {response}")
                return None
        else:
            print(f"Unable to login Device - {dev_name}\n")
            return False
        return True
    except NetMikoAuthenticationException as net_err:
        logging.error(f"Issue observed while accessing the provided IP - {str(net_err)}")
        return None
    except EOFError as eof_err:
        logging.error("Hit EOFError while accessing %s(%s) via SSH from %s - %s"
                      % (hname, ip, socket.getfqdn(), str(eof_err)))
        return "NOT REACHABLE"
    except OSError as os_err:
        logging.error("Hit OSError while accessing %s(%s) via SSH from %s - %s"
                      % (hname, ip, socket.getfqdn(), str(os_err)))
        return "NOT REACHABLE"
    except NetMikoTimeoutException as timeout_err:
        logging.error("Device connection timeout while accessing %s(%s) via SSH from %s - %s"
                      % (hname, ip, socket.getfqdn(), str(timeout_err)))
        return "NOT REACHABLE"
    except ssh_exception.SSHException as ssh_err:
        logging.error("Signature verification (ssh-rsa) failed for %s(%s) - %s" % (hname, ip, str(ssh_err)))
        return "FAILED"

The below is the netmiko logging details.

DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:send_command_timing final output: Cisco Nexus Operating System (NX-OS) Software TAC support: http://www.cisco.com/tac Copyright (c) 2002-2016, Cisco Systems, Inc. All rights reserved. The copyrights to certain works contained in this software are owned by other third parties and used and distributed under license. Certain components of this software are licensed under the GNU General Public License (GPL) version 2.0 or the GNU Lesser General Public License (LGPL) Version 2.1. A copy of each such license is available at http://www.opensource.org/licenses/gpl-2.0.php and http://www.opensource.org/licenses/lgpl-2.1.php

DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\r' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\r' DEBUG:netmiko:read_channel: switch# INFO:root:Connected after reboot

INFO:root:Configuring device with default configurations

DEBUG:netmiko:write_channel: b'\r' DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: switch# switch# switch# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'hostname SCLLAB-NSO1-CORE01\n' DEBUG:netmiko:write_channel: b'username admin password Wt1g1tHb role network-admin\n' DEBUG:netmiko:write_channel: b'vrf context management\n' DEBUG:netmiko:write_channel: b'ip route 0.0.0.0/0 10.198.15.1\n' DEBUG:netmiko:write_channel: b'interface mgmt0\n' DEBUG:netmiko:write_channel: b'vrf member management\n' DEBUG:netmiko:write_channel: b'ip address 10.198.15.19/24\n' DEBUG:netmiko:read_channel: hostname SCLLAB-NSO1-CORE01 ^ % Invalid command at '^' marker. switch# username admin password Wt1g1tHb role network-admin ^ % Invalid command at '^' marker. switch# vrf context management ^ % Invalid command at '^' marker. switch# ip route 0.0.0.0/0 10.198.15.1 ^ % Invalid command at '^' marker. switch# interface mgmt0 ^ % Invalid command at '^' marker. switch# vrf member management ^ % Invalid command at '^' marker. switch# ip address 10.198.15.19/24 ^ % Invalid command at '^' marker. switch# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: switch# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:Pattern is: DEBUG:netmiko:_read_channel_expect read_data:

DEBUG:netmiko:Pattern found:

DEBUG:netmiko:write_channel: b'\n' switch# tmiko:read_channel: switch# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel:

Please do needful.

ktbyers commented 4 years ago

Are you using a redispatch?

On quick glance, it looks like you are using the terminal_server device_type? Once you get connected to your end-device, you need to be using the proper device_type for that device (i.e. cisco_nxos in this case).

SusethiraRam commented 4 years ago

Yes you are correct. I missed redispatch part in my code.

redispatch(net_connect, device_type='cisco_nxos')

Thanks for correcting it. Now send_config_set command is working fine.