ktbyers / netmiko

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

send_config_set problem with ios command prompt #2956

Closed ferberat closed 2 years ago

ferberat commented 2 years ago

If we define the prompt with hostname and prompt command on IOS, we run in a problem with the search pattern. This occurs with send_config_set.

Config: hostname Loc-S123987345 prompt %h-STP-1234%p

The "Pattern not detected: 'Loc\-S123987345\-S.*' in output." doesn 't match the message in ssh channel: Loc-S123987345(config)#

I found a workaround: search_pattern=net_connect.find_prompt() net_connect.config_mode(pattern=search_pattern[0:14]) net_connect.send_config_set(config)

BR Franz

########################### Logfile:

DEBUG:netmiko:read_channel: Loc-S123987345-STP-1234# Loc-S123987345-STP-1234# DEBUG:netmiko:Pattern found: (#|>) Loc-S123987345-STP-1234# DEBUG:netmiko:read_channel: DEBUG:netmiko:Clear buffer detects data in the channel DEBUG:netmiko:read_channel: DEBUG:netmiko:[find_prompt()]: prompt is Loc-S123987345-STP-1234# DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: Loc-S123987345-STP-1234# DEBUG:netmiko:Pattern found: ([>#]) Loc-S123987345-STP-1234# DEBUG:netmiko:write_channel: b'configure terminal\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: configure terminal

DEBUG:netmiko:Pattern found: (configure\ terminal) configure terminal DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: Enter configuration commands, one per line. End with CNTL/Z. Loc-S123987345(config)# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel

################################# Routerlogs:

net_connect.send_config_set(config) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.8/site-packages/netmiko/base_connection.py", line 2140, in send_config_set output += self.config_mode() File "/usr/local/lib/python3.8/site-packages/netmiko/cisco_base_connection.py", line 49, in config_mode return super().config_mode( File "/usr/local/lib/python3.8/site-packages/netmiko/base_connection.py", line 1979, in config_mode output += self.read_until_prompt(read_entire_line=True) File "/usr/local/lib/python3.8/site-packages/netmiko/base_connection.py", line 734, in read_until_prompt return self.read_until_pattern( File "/usr/local/lib/python3.8/site-packages/netmiko/base_connection.py", line 651, in read_until_pattern raise ReadTimeout(msg) netmiko.exceptions.ReadTimeout:

Pattern not detected: 'Loc\-S123987345\-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.

net_connect.find_prompt() 'Loc-S123987345(config)#'

ktbyers commented 2 years ago

Netmiko should automatically do this here (to a certain extent):

https://github.com/ktbyers/netmiko/blob/develop/netmiko/cisco/cisco_ios.py#L41

It looks like that Cisco IOS/IOS-XE prompt command is incompatible with Netmiko:

cisco3-STP-1234#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
cisco3(config)#do show run | inc prompt
prompt %h-STP-1234%p

Basically config mode reverts to having the prompt just be "hostname(config)" whereas outside of config mode it is what prompt is defined as.

So abbreviating it by, search_pattern[0:14] or by base_prompt[:16] won't fix it.

So you would probably need to either:

  1. Not use the prompt command
  2. Not use send_config_set and instead use config_mode(), send_command(), exit_config_mode() to send configurations or write your own custom function to do this.
ferberat commented 2 years ago

Hi Kirk,

thanks, this works with netmiko:

show_hostname=net_connect.send_command('sh run | in  hostname ')
search_pattern=show_hostname.split()[1]
net_connect.config_mode(pattern=search_pattern)
for line in config:
    net_connect.send_command(line, expect_string=search_pattern)
net_connect.exit_config_mode()

I need the serach_pattern option in config_mode command

I tried also napalm, I think napalm use netmiko. with napalm i have the same problem. If i use a hostname > len 16 i got an error. But currently I didn' t use napalm and the workaround is ok.

I think it is better to remove the prompt command on router :-)

Thanks, BR Franz

jmartin-79 commented 1 year ago

...

  1. Not use send_config_set and instead use config_mode(), send_command(), exit_config_mode() to send configurations or write your own custom function to do this.

This worked perfect for say interface range commands on IOSXE..