Netmiko OSError: Search pattern never detected in send_command:

Viewed 4144

I am stuck with this error.

Can anyone help to come out from this error.

import netmiko 

Device = {"host":"xxxxxxxxxx", "device_type":"cisco_nxos", "username":"admin", "password":"xxxxxxxx"}

connect =netmiko.ConnectHandler(**Device)

connect.enable()

CLI = "show ip int bri"

print(connect.send_command(CLI))

CLI= "show run"

print(connect.send_command(CLI))

I get result for the first cmd but I get error for the 2nd cmd as :

  "OSError: Search pattern never detected in send_command:"
4 Answers

send_command is pattern-based. Meaning it searches for the device prompt to detect the end of output. for BaseConnection in netmiko, the time for each command to complete is 100 seconds but for Cisco devices is only 10 seconds because fast_cli default value is set to True. fast_cli simply multiplies the 100 seconds by 0.1 (100*0.1 = 10) just to make it faster, yet faster is not always the best option.

You need to set fast_cli to False to disable the timeout.

Always set fast_cli to False when dealing with Cisco devices either cisco_ios, cisco_xe, cisco_xr, or cisco_nxos in netmiko v3.4.0.

Please try the below code:

from netmiko import ConnectHandler

device = {
    "host": "xxxxxxxx",
    "device_type": "cisco_nxos",
    "username": "admin",
    "password": "xxxxxxxx",
    "fast_cli": False,  # Notice the item here
    "secret": "",  # Enable password (If applicable)
}

# Connect to the device
conn = ConnectHandler(**device)

# Check if connected in user mode and enter enable mode
# Make sure you set the "secret": "xxxx" in the device variable
if not conn.check_enable_mode():
    conn.enable()

intf_brief = conn.send_command("show interface brief")
run_cfg = conn.send_command("show running-config")

# Disconnect to clear the vty line
conn.disconnect()

# Do whatever you like with the outputs after the connection
# is terminated
print(intf_brief)
print(run_cfg)

Another option

You can use the send_command_timing method without setting fast_cli to False instead of send_command. The former method is delay-based. It doesn't search for the device prompt to detect the end of output rather it waits for some time.

from netmiko import ConnectHandler

device = {
    "host": "xxxxxxxx",
    "device_type": "cisco_nxos",
    "username": "admin",
    "password": "xxxxxxxx",
    "secret": "",  # Enable password (If applicable)
}

# Connect to the device
conn = ConnectHandler(**device)

# Check if connected in user mode and enter enable mode
# Make sure you have the "secret": "xxxx" is set in device var
if not conn.check_enable_mode():
    conn.enable()

# Notice send_command_timing method here
intf_brief = conn.send_command_timing("show interface brief")
run_cfg = conn.send_command_timing("show running-config")

# Disconnect to clear the vty line
conn.disconnect()

# Do whatever you like with the outputs after the connection
# is terminated
print(intf_brief)
print(run_cfg)

I don't have rep > 49 so I can't comment on Tes3awy's answer but want to say Thank You. I'm using a cisco_xr and the "fast_cli": False setting solved the issue. Interestingly, I am running my script from two different hosts. The error did not occur on the system running netmiko 3.0.0 but did occur on the system running netmiko 3.4.0.

Try 'terminal length 0' command pass before your actual command.

conn.send_command('terminal length 0')

Sharing what worked for me hoping it helps others!

Problem: Setting fast_cli to False, as per @Tes3way answer, does increase the wait time from 10 to 100 seconds. However, I found the show inventory command on a Cisco XR took longer than that, about 140 seconds to complete, hence resulting in the same error message.

Solution: Adjust the delay_factor argument of the send_command method to your own needs:

Its effect is to modify all of the delays embedded in send_command (for example, a delay_factor=2 will double all of the delays; a delay_factor=4 will quadruple all of the delays).

from netmiko import ConnectHandler

device = {
    "host": "xxxxxxxx",
    "device_type": "cisco_xr",
    "username": "admin",
    "password": "xxxxxxxx",
    "fast_cli": False,
}

conn = ConnectHandler(**device)
output = conn.send_command("show inventory", delay_factor=2)

More info here.

Related