ktbyers / netmiko

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

MOTD prevents successful login attempts with CISCO #3424

Open FriendlyGecko opened 7 months ago

FriendlyGecko commented 7 months ago

Description of Issue/Question

When attempting to log into a device that uses a MOTD, the login fails. This is because the MOTD requires the user to first send a return character to wake it up. If I use putty to first console in and press enter, I can swap back to my netmiko script and log into the device.

Setup

1 Laptop 1 Console Cable 1 Network

Netmiko version

netmiko==4.3.0

Netmiko device_type (if relevant to the issue)

Cisco IOS, Cisco IOS Serial

Steps to Reproduce the Issue

  1. Configure a Cisco Network device with local login for line console 0
  2. Configure a MOTD banner
  3. [OPTIONAL] Configure SSH
  4. Attempt to connect to the device

Error Traceback

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\intChange\test.py", line 15, in <module>
    net_connect = netmiko.ConnectHandler(**cisco1)
  File "C:\Users\User\PycharmProjects\intChange\venv\lib\site-packages\netmiko\ssh_dispatcher.py", line 399, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "C:\Users\User\PycharmProjects\intChange\venv\lib\site-packages\netmiko\base_connection.py", line 488, in __init__
    self._open()
  File "C:\Users\User\PycharmProjects\intChange\venv\lib\site-packages\netmiko\base_connection.py", line 493, in _open
    self.establish_connection()
  File "C:\Users\User\PycharmProjects\intChange\venv\lib\site-packages\netmiko\base_connection.py", line 1129, in establish_connection
    self.serial_login()
  File "C:\Users\User\PycharmProjects\intChange\venv\lib\site-packages\netmiko\cisco_base_connection.py", line 76, in serial_login
    return self.telnet_login(
  File "C:\Users\User\PycharmProjects\intChange\venv\lib\site-packages\netmiko\cisco_base_connection.py", line 194, in telnet_login
    raise NetmikoAuthenticationException(msg)
netmiko.exceptions.NetmikoAuthenticationException: Login failed: serial

Relevant Python code

import netmiko

cisco1 = {
    "username": "admin",
    "password": "password",
    "device_type": "cisco_ios_serial",
    "serial_settings": {"port": "COM5"}
}

net_connect = netmiko.ConnectHandler(**cisco1)
net_connect.send_command("sh ip int br")
net_connect.disconnect()

Potential Fix

def telnet_login([...]):
[...]
    # Check for MOTD/Wake Up
    if not re.search(username_pattern, output, flags=re.I) and not re.search(pwd_pattern, output, flags=re.I):
        self.write_channel("\r")
        time.sleep(1 * delay_factor)
        output = self.read_channel()
        return_msg += output

I tried something similar to this but had no success. If I figure out a good answer that is flexible enough for majority use, then I will attempt to merge it. EDIT: I was inputting this change in base_connection and not cisco_base_connection

fisherbe commented 5 months ago

Just opened #3441 and looks like it might be the same issue, except my Issue is on NXOS.

ktbyers commented 5 months ago

@fisherbe @FriendlyGecko What does your MOTD look like?

@fisherbe Are you using SSH for transport or something else (original problem was using "serial" connection i.e. console cable or similar)?

fisherbe commented 5 months ago

@ktbyers ,

For my #3441 ,

Default motd: "User Access Verification"

Using SSH, not serial or telnet.

FriendlyGecko commented 3 months ago

@FriendlyGecko What does your MOTD look like?

banner motd '
test test test'

Are you using SSH for transport or something else (original problem was using "serial" connection i.e. console cable or similar)? I ran into the issue with both SSH and serial connections. I realize I wasn't explicit enough with "[OPTIONAL] Configure SSH"

ktbyers commented 3 months ago

@FriendlyGecko I don't see any issue when using SSH for this on Cisco IOS/IOS-XE

cisco3#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
cisco3(config)#banner motd '
Enter TEXT message.  End with the character '''.
test test test'
cisco3(config)#end
cisco3#exit
Connection to cisco3.lasthop.io closed by remote host.
Connection to cisco3.lasthop.io closed.

[.venv] ktbyers@pydev2 ~/netmiko/tests 
$ ssh -l pyclass cisco3.lasthop.io
Password: 

test test test

cisco3#exit
Connection to cisco3.lasthop.io closed by remote host.
Connection to cisco3.lasthop.io closed.
$ py.test -s -v -x test_netmiko_show.py --test_device cisco3 
========================================================== test session starts ===========================================================
platform linux -- Python 3.10.2, pytest-8.2.2, pluggy-1.5.0 -- /home/ktbyers/netmiko/.venv/bin/python
cachedir: .pytest_cache
rootdir: /home/ktbyers/netmiko
configfile: setup.cfg
plugins: pylama-8.4.1
collected 25 items                                                                                                                       

test_netmiko_show.py::test_failed_key SKIPPED (Not using SSH-keys)
test_netmiko_show.py::test_disable_paging PASSED
test_netmiko_show.py::test_terminal_width PASSED
test_netmiko_show.py::test_ssh_connect PASSED
test_netmiko_show.py::test_ssh_connect_cm PASSED
test_netmiko_show.py::test_send_command_timing PASSED
test_netmiko_show.py::test_send_command_timing_no_cmd_verify SKIPPED
test_netmiko_show.py::test_send_command PASSED
test_netmiko_show.py::test_send_command_no_cmd_verify SKIPPED
test_netmiko_show.py::test_complete_on_space_disabled SKIPPED
test_netmiko_show.py::test_send_command_textfsm PASSED
test_netmiko_show.py::test_send_command_ttp SKIPPED (TTP template not existing for this platform)
test_netmiko_show.py::test_send_command_genie PASSED
test_netmiko_show.py::test_send_multiline_timing PASSED
test_netmiko_show.py::test_send_multiline PASSED
test_netmiko_show.py::test_send_multiline_prompt PASSED
test_netmiko_show.py::test_send_multiline_simple PASSED
test_netmiko_show.py::test_base_prompt PASSED
test_netmiko_show.py::test_strip_prompt PASSED
test_netmiko_show.py::test_strip_command PASSED
test_netmiko_show.py::test_normalize_linefeeds PASSED
test_netmiko_show.py::test_clear_buffer PASSED
test_netmiko_show.py::test_enable_mode PASSED
test_netmiko_show.py::test_disconnect PASSED
test_netmiko_show.py::test_disconnect_no_enable SKIPPED

Can you reproduce your error using SSH and post the full exception stack here?

ktbyers commented 3 months ago

The NX-OS item is a different issue related to a null password which is covered in the linked issue:

https://github.com/ktbyers/netmiko/issues/3441

FriendlyGecko commented 3 months ago

Yeah, I was working on this issue in my pull request. It has been a good while since I had worked on it and the environment that I am using now does not appear to have the same issue. When I have access to the same lab, then I will troubleshoot it again.

I am not concerned with #3441.