cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
775
Views
3
Helpful
6
Replies

python ssh catalyst 1300 netmiko fail

Hi !

Try to connect with Python (3.13) and netmiko (4.5.0) to Catalyst 1300 ( sw 4.1.6.53).

The problem is that first there is one prompt : "login as:" and secondly a prompt : "User Name:"

ssh = ConnectHandler(device_type='cisco_s200', ip=x.x.x.x, username=xx, password=xx, secret=xx, default_enter='\r', session_log= "debug_log.txt")

Error msg returned :

"Pattern not detected: '(?:Username:|login as|ssword|(?m:[>#]\\s*$))' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information."

Tried to search for a solution with no luck.

Also looked at "cisco_base_connection.py",  but the variable "username_pattern" is only specified for serial and telnet.

Any help would be appreciated

2 Accepted Solutions

Accepted Solutions

Yes, The idea is to tell ConnectHandler to use your custom class (CiscoS200Connection) instead of the default class for cisco_s200.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

View solution in original post

Hi !

Thanks again !

Would probably work, but I ended up with taking a copy of "cisco_s300.py" to "cisco_s300_org.py".  Then copy "cisco_s200.py" to "cisco_s300.py.This is because we have no smb300 switches. Edited "cisco_s300.py" to this :

...

def special_login_handler(self, delay_factor: float = 1.0) -> None:
"""Cisco C13xx presents with the following on login

login as: user

User Name: user
Password:****
"""
output = ""
uname = "User"
login = "login"
password = "ssword"
pattern = rf"(?:{uname}|{login}|{password}|{self.prompt_pattern})"

while True:
new_data = self.read_until_pattern(pattern=pattern, read_timeout=25.0)
output += new_data

# Fully logged-in, switch prompt detected.
if re.search(self.prompt_pattern, new_data):
return

if uname in new_data or login in new_data:
assert isinstance(self.username, str)
self.write_channel(self.username + self.RETURN)
elif password in new_data:
assert isinstance(self.password, str)
self.write_channel(self.password + self.RETURN)
else:
msg = f"""
Failed to login to Cisco C13xx.

Pattern not detected: {pattern}
output:

{output}

"""

 

Probably not the best solution, but works ok

View solution in original post

6 Replies 6

@HaraldNordaas34991 i think your issue is that Netmiko default behavior is not designed for the two step authentication process. You would need to add to your code and exception to explicitly handle both prompts in sequence other than relying on Netmiko default pattern matching. You could try

from netmiko import ConnectHandler
from netmiko.base_connection import BaseConnection
import time

def _handle_login(self):
    """Handle the two-step authentication process"""
    
    # First prompt: "login as:"
    output = self.read_until_pattern(pattern=r"login as:")
    self.write_channel(self.username + self.RETURN)
    time.sleep(1)  # Small delay to ensure next prompt is ready
    
    # Second prompt: "User Name:"
    output = self.read_until_pattern(pattern=r"User Name:")
    self.write_channel(self.username + self.RETURN)
    time.sleep(1)  # Small delay before password prompt
    
    # Handle password prompt
    output = self.read_until_pattern(pattern=r"[Pp]assword:")
    self.write_channel(self.password + self.RETURN)
    
    return output

# Example usage with connection parameters
connection_params = {
    'device_type': 'cisco_s200',
    'ip': 'x.x.x.x',
    'username': 'your_username',
    'password': 'your_password',
    'secret': 'your_enable_secret',
    'default_enter': '\r',
    'session_log': 'debug_log.txt',
    'auth_timeout': 30,  # Increased timeout for auth process
}

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Hi !

Thanks !

A very quick response, but is it possible to explain a bit more

I have added the extra line of code, but how should I change the connection so the "_handle_login(self)"  is called ? 

ssh = ConnectHandler(device_type='cisco_s200', ip=x.x.x.x, username=xx, password=xx, secret=xx, default_enter='\r', session_log= "debug_log.txt", auth_timeout=30)

 

 

Yes, The idea is to tell ConnectHandler to use your custom class (CiscoS200Connection) instead of the default class for cisco_s200.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Hi !

Thanks again !

Would probably work, but I ended up with taking a copy of "cisco_s300.py" to "cisco_s300_org.py".  Then copy "cisco_s200.py" to "cisco_s300.py.This is because we have no smb300 switches. Edited "cisco_s300.py" to this :

...

def special_login_handler(self, delay_factor: float = 1.0) -> None:
"""Cisco C13xx presents with the following on login

login as: user

User Name: user
Password:****
"""
output = ""
uname = "User"
login = "login"
password = "ssword"
pattern = rf"(?:{uname}|{login}|{password}|{self.prompt_pattern})"

while True:
new_data = self.read_until_pattern(pattern=pattern, read_timeout=25.0)
output += new_data

# Fully logged-in, switch prompt detected.
if re.search(self.prompt_pattern, new_data):
return

if uname in new_data or login in new_data:
assert isinstance(self.username, str)
self.write_channel(self.username + self.RETURN)
elif password in new_data:
assert isinstance(self.password, str)
self.write_channel(self.password + self.RETURN)
else:
msg = f"""
Failed to login to Cisco C13xx.

Pattern not detected: {pattern}
output:

{output}

"""

 

Probably not the best solution, but works ok

If it works, its great!

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Try this way

  from netmiko import ConnectHandler
ssh = ConnectHandler(device_type='cisco_s200',ip='x.x.x.x',username='xx',password='xx',secret='xx',timeout=10)

You are mssing quotation marks on the varialble´s values. 
 

You can also try with device_type='cisco_ios'

Another good way to put this is:

from netmiko import ConnectHandler

cisco_device = {
'device_type' : 'cisco_ios',
'ip': 'x.x.x.x',
'username' : 'xx',
'password' : 'xx',
'secret': 'xx'
}

ssh = ConnectHandler(**cisco_device)
onput = net_connect.send_command('show ip int brief')
print(onput)