Chamod_
Chamod_

Reputation: 1

Netmiko Pattern Not Detected

I ran this code with cisco 2960X switch.

from netmiko import ConnectHandler

network_device= {
    "host": "192.168.xxx.xxx",
    "username": "xxxx",
    "password": "xxxx",
    "device_type": "cisco_ios",
    "session_log": "netmiko_session.log"
}

connect= ConnectHandler(**network_device)
connect.enable()

interface_name= "GigabitEthernet1/0/10"

def send_command(command):
    return connect.send_command(command)
try:
    send_command('enable')
    send_command('configure terminal')
except Exception as e:
    print(e)
    print("Failed!")

But after I got below error.

Pattern not detected: 'Switch\\#' 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.

Failed!

Please check below to netmiko_session.log

Switch#
Switch#terminal width 511
Switch#terminal length 0
Switch#
Switch#enable
Switch#
Switch#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Switch(config)#

I renamed switch hostname. But also same error going.

Upvotes: 0

Views: 10446

Answers (1)

Missclicker
Missclicker

Reputation: 21

If you want to do configuration changes, you should consider using ssh.send_config_set method:

from netmiko import ConnectHandler

network_device= {
    "host": "192.168.xxx.xxx",
    "username": "xxxx",
    "password": "xxxx",
    "device_type": "cisco_ios",
    "session_log": "netmiko_session.log"
}

connect = ConnectHandler(**network_device)
config_commands = [
    'hostname NEW_NAME',
    'interface Gi1/0/10',
    'description NEW_IF_NAME'
]
connect.enable()
connect.send_config_set(config_commands)
connect.save_config()
connect.disconnect()

When you send the command "conf terminal' prompt changes to reflect configuration mode, and it starts to look like Switch(config)# while netmiko still waits for Switch#

So alternatively, if you want to send commands one by one, you can set the expected prompt or use "send_command_timing"

# this one will just wait 2 seconds before thinking that the command was applied to the device
connect.send_command_timing('hostname NEW_NAME', read_timeout=2)
# this one will expect a custom string from the device after the command is entered
connect.send_command('hostname NEW_NAME', expect_string='(config)#')
# be aware, that if you enter "configure interface" mode prompt will also change
connect.send_command('interface gi1/0/10', expect_string='(config-if)#')

Upvotes: 0

Related Questions