Python Netmiko Pattern Not Detected

322 views Asked by At

I am using below python code to get the output of show version

from netmiko import ConnectHandler


device_cisco_ios = {
    'device_type': 'cisco_ios',
    'host': "ABCDEF32",
    'username': "XXXX",
    'password': "xxxxx",
    "session_log": 'netmiko_session.log',

}
try:
    with ConnectHandler(**device_cisco_ios) as ssh:
        cmd = ssh.send_command("sh ver")
        print(cmd)

except Exception as e:
    print(str(e))

Below is the 'netmiko_session.log' file contents

Last login: Fri Oct 27 17:57:36 2023 from 10.245.246.24


terminal width 511
terminal length 0


RP/0/RP0/CPU0:ABCDEF32#

RP/0/RP0/CPU0:ABCDEF32#terminal width 511




RP/0/RP0/CPU0:ABCDEF32#terminal length 0

sh ver
RP/0/RP0/CPU0:ABCDEF32#

RP/0/RP0/CPU0:ABCDEF32#

RP/0/RP0/CPU0:ABCDEF32#

RP/0/RP0/CPU0:ABCDEF32#

RP/0/RP0/CPU0:ABCDEF32#

RP/0/RP0/CPU0:ABCDEF32#sh ver

Cisco IOS XR Software, Version 7.3.3 LNT
Copyright (c) 2013-2022 by Cisco Systems, Inc.

Build Information:
 Built By     : xxxx
 Built On     : xxx
 Build Host   : xxx
 Workspace    : XXXX
 Version      : 7.3.3
 Label        : 7.3.3

cisco 8000 (Intel(R) Xeon(R) CPU D-1530 @ 2.40GHz)
cisco 8201-32FH (Intel(R) Xeon(R) CPU D-1530 @ 2.40GHz) processor with 32GB of memory
ABCDEF32 uptime is 24 weeks, 11 hours, 15 minutes
Cisco 8000 Series 32x400G QSFPDD 1RU Fixed System w/HBM

RP/0/RP0/CPU0:ABCDEF32#

RP/0/RP0/CPU0:ABCDEF32#exit

Below is the exception I am getting

Pattern not detected: 'RP/0/RP0/CPU0:ABCDEF32\\#terminal\\ length\\ 0' in output.

Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.
  1. Tried increasing the readtime out - didnt work
  2. Checked for Whitespace or Control Characters,used hexa decimal dump - Not found any
  3. But if I give expected string to be "ABCDEF32#" then its working fine

I am not sure why pattern is not being detected without explicitly specifing and same device some times its working fine without any exceptions, I am using netmiko 4.2.0. It would be great if someone could help me with this.

1

There are 1 answers

0
mickyhale On

I would use the proper driver for IOS XR devices, since that's what you're interacting with.

'device_type': 'cisco_xr',

The driver handles prompt detection a bit differently than IOS devices.