question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSH is_alive() causes slow Cisco IOS device to get out of sync with netmiko

See original GitHub issue

Because the null character sent by is_alive() on SSH sessions causes the IOS device to echo a new prompt (as noted in https://github.com/ktbyers/netmiko/issues/568#issuecomment-329571918), if the device is sufficiently slow in responding, the device can get “out of sync” to where, in looking for the prompt, it hits one of these extra echoed prompts and stops there instead of reading to the latest (expected) prompt.

Here’s an excerpt of a session illustrating this problem:

[28/Mar/2019 20:03:26] DEBUG [netmiko:988] Command: terminal length 0

[28/Mar/2019 20:03:26] DEBUG [netmiko:385] write_channel: b'terminal length 0\n'
[28/Mar/2019 20:03:26] DEBUG [netmiko:506] Pattern is: csr1000v
[28/Mar/2019 20:03:26] DEBUG [netmiko:523] _read_channel_expect read_data: t
[28/Mar/2019 20:03:26] DEBUG [netmiko:523] _read_channel_expect read_data: erminal le
[28/Mar/2019 20:03:26] DEBUG [netmiko:523] _read_channel_expect read_data: ng
[28/Mar/2019 20:03:26] DEBUG [netmiko:523] _read_channel_expect read_data: th
[28/Mar/2019 20:03:27] DEBUG [netmiko:523] _read_channel_expect read_data:  0
csr1000v#
[28/Mar/2019 20:03:27] DEBUG [netmiko:535] Pattern found: csr1000v terminal length 0
csr1000v#
[28/Mar/2019 20:03:27] DEBUG [netmiko:993] terminal length 0
csr1000v#
[28/Mar/2019 20:03:27] DEBUG [netmiko:994] Exiting disable_paging
[28/Mar/2019 20:03:27] DEBUG [netmiko:385] write_channel: b'terminal width 511\n'
[28/Mar/2019 20:03:27] DEBUG [netmiko:506] Pattern is: csr1000v
[28/Mar/2019 20:03:27] DEBUG [netmiko:523] _read_channel_expect read_data: t
[28/Mar/2019 20:03:27] DEBUG [netmiko:523] _read_channel_expect read_data: erminal
[28/Mar/2019 20:03:28] DEBUG [netmiko:523] _read_channel_expect read_data:  widt
[28/Mar/2019 20:03:28] DEBUG [netmiko:523] _read_channel_expect read_data: h 5
[28/Mar/2019 20:03:29] DEBUG [netmiko:523] _read_channel_expect read_data: 1
[28/Mar/2019 20:03:29] DEBUG [netmiko:523] _read_channel_expect read_data: 1
csr1000v#
[28/Mar/2019 20:03:29] DEBUG [netmiko:535] Pattern found: csr1000v terminal width 511
csr1000v#
[28/Mar/2019 20:03:30] DEBUG [netmiko:463] read_channel: 
[28/Mar/2019 20:03:30] DEBUG [netmiko:434] Sending the NULL byte
[28/Mar/2019 20:03:30] DEBUG [netmiko:385] write_channel: b'\x00'
[28/Mar/2019 20:03:30] DEBUG [netmiko:434] Sending the NULL byte
[28/Mar/2019 20:03:30] DEBUG [netmiko:385] write_channel: b'\x00'
[28/Mar/2019 20:03:30] DEBUG [netmiko:434] Sending the NULL byte
[28/Mar/2019 20:03:30] DEBUG [netmiko:385] write_channel: b'\x00'

Here, after establishing the session (above), we call send_config_set(), but observe what happens:

[28/Mar/2019 20:03:30] DEBUG [netmiko:1548] ---> send_config_set()
[28/Mar/2019 20:03:30] DEBUG [netmiko:1463] ---> config_mode()
[28/Mar/2019 20:03:30] DEBUG [netmiko:1444] ---> check_config_mode()
[28/Mar/2019 20:03:30] DEBUG [netmiko:385] write_channel: b'\n'
[28/Mar/2019 20:03:30] DEBUG [netmiko:506] Pattern is: #
[28/Mar/2019 20:03:30] DEBUG [netmiko:523] _read_channel_expect read_data: ^@
csr1000v#
[28/Mar/2019 20:03:30] DEBUG [netmiko:535] Pattern found: # ^@
csr1000v#
[28/Mar/2019 20:03:30] DEBUG [netmiko:1451] <--- check_config_mode()
[28/Mar/2019 20:03:30] DEBUG [netmiko:385] write_channel: b'config term\n'
[28/Mar/2019 20:03:30] DEBUG [netmiko:506] Pattern is: csr1000v
[28/Mar/2019 20:03:30] DEBUG [netmiko:523] _read_channel_expect read_data: ^@
[28/Mar/2019 20:03:30] DEBUG [netmiko:523] _read_channel_expect read_data: 
csr1000v#^@
csr1000v#
[28/Mar/2019 20:03:30] DEBUG [netmiko:535] Pattern found: csr1000v ^@
csr1000v#^@
csr1000v#

Note that we just sent config term\n but the prompt that we just read back was one induced by is_alive() (see the ^@ character) - the output from the device hasn’t yet reached the point where it echoed config term followed by the actual latest prompt.

Continuing…

[28/Mar/2019 20:03:30] DEBUG [netmiko:1444] ---> check_config_mode()
[28/Mar/2019 20:03:30] DEBUG [netmiko:385] write_channel: b'\n'
[28/Mar/2019 20:03:30] DEBUG [netmiko:506] Pattern is: #
[28/Mar/2019 20:03:31] DEBUG [netmiko:523] _read_channel_expect read_data: 

[28/Mar/2019 20:03:31] DEBUG [netmiko:523] _read_channel_expect read_data: csr1000v#confi
[28/Mar/2019 20:03:31] DEBUG [netmiko:535] Pattern found: # 
csr1000v#confi
[28/Mar/2019 20:03:31] DEBUG [netmiko:1451] <--- check_config_mode()

At this point, the check_config_mode() call after sending “config term” fails here, because due to the out-of-sync situation, it has found a non-config-mode prompt and hence thinks the system was unable to enter config mode (when in fact the output that’s being read from the device simply hasn’t reached that point yet).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
glennmatthewscommented, Mar 28, 2019

Got it. I’ll rip out the is_alive check from our code and we can address any issues as they arise. Thanks for the help!

0reactions
ktbyerscommented, Mar 28, 2019

@glennmatthews I am open to trying to figure out a better solution. I just know up until now is_alive() has been problematic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: Python netmiko interactive session on Cisco IOS
Solved: This is simple Python code to get `show hostname` output in Cisco IOS. script.py from netmiko import ConnectHandler cisco_881 = { 'device_type': ......
Read more >
netmiko API documentation - GitHub Pages
CiscoBaseConnection is netmiko SSH class for Cisco and Cisco-like ... Use Secure Copy or Inline (IOS-only) to transfer files to/from network devices.
Read more >
Netmiko and what constitutes done
We send the above command down the SSH channel and it will work successfully (as we successfully match the trailing router prompt). Note,...
Read more >
SSH Configuration with Netmiko - YouTube
Introduction to Python for Cisco Networking Professionals · Getting Started with NETCONF · Mix - Kevin Wallace Training, LLC · Parse Cisco IOS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found