ktbyers / netmiko

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

MOTD prevents successful login attempts with CISCO #3424

Open FriendlyGecko opened 2 months ago

FriendlyGecko commented 2 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 2 weeks ago

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

ktbyers commented 2 weeks 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 1 week ago

@ktbyers ,

For my #3441 ,

Default motd: "User Access Verification"

Using SSH, not serial or telnet.