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.

CiscoXrBase: Reduce the time duration for session preparation

See original GitHub issue

netmiko: 3.2.0 Cisco XR: 7.0.1

[Problem Description]

It takes long time (approx. 4.7 secs) until a new session preparation is completed by ConnectHandler(). I think this issue is not specific to CiscoXrBase but also applicable to other platforms calling BaseConnection::_test_channel_read() method.

I’d like to reduce this time duration and I could improve the behavior by my local patch, but I have a question before submitting a pull request.

netconn = ConnectHandler(**device)

Please see the below CiscoXr syslog and netmiko logger for details.

  • CiscoXr (syslog)

The below CiscoXr syslog says SSH connection itself is established at 19:12:15.922.

RP/0/RP0/CPU0:2020 Aug 14 19:12:15.922 JST: SSHD_[68443]: %SECURITY-SSHD-6-INFO_SUCCESS : Successfully authenticated user 'cisco' from '10.71.38.195' on 'vty3'(cipher 'aes128-ctr', mac 'hmac-sha2-256')
  • netmiko (logger)

The below netmiko logger says session_preparation() is completed at around 19:12:20.676, which means it took about 4.754 secs after a SSH connection is established.

2020-08-14 19:12:17.171 [   DEBUG] read_channel: 

RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:17.273 [   DEBUG] read_channel: 
2020-08-14 19:12:19.276 [   DEBUG] read_channel: 
2020-08-14 19:12:19.381 [   DEBUG] read_channel: 
2020-08-14 19:12:19.382 [   DEBUG] write_channel: b'\n'
2020-08-14 19:12:19.488 [   DEBUG] read_channel: 
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:19.696 [   DEBUG] read_channel: 
2020-08-14 19:12:19.697 [   DEBUG] [find_prompt()]: prompt is RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:19.902 [   DEBUG] read_channel: 
2020-08-14 19:12:19.903 [   DEBUG] In disable_paging
2020-08-14 19:12:19.904 [   DEBUG] Command: terminal length 0

2020-08-14 19:12:19.905 [   DEBUG] write_channel: b'terminal length 0\n'
2020-08-14 19:12:19.906 [   DEBUG] Pattern is: RP/0/RP0/CPU0:NCS\-5501c\-ME04
2020-08-14 19:12:19.931 [   DEBUG] _read_channel_expect read_data: terminal length 0

2020-08-14 19:12:20.033 [   DEBUG] _read_channel_expect read_data: Fri Aug 14 19:12:19.798 JST

2020-08-14 19:12:20.138 [   DEBUG] _read_channel_expect read_data: RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:20.138 [   DEBUG] Pattern found: RP/0/RP0/CPU0:NCS\-5501c\-ME04 terminal length 0
Fri Aug 14 19:12:19.798 JST
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:20.139 [   DEBUG] terminal length 0
Fri Aug 14 19:12:19.798 JST
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:20.140 [   DEBUG] Exiting disable_paging
2020-08-14 19:12:20.141 [   DEBUG] write_channel: b'terminal width 511\n'
2020-08-14 19:12:20.142 [   DEBUG] Pattern is: RP/0/RP0/CPU0:NCS\-5501c\-ME04
2020-08-14 19:12:20.160 [   DEBUG] _read_channel_expect read_data: terminal width 511

2020-08-14 19:12:20.265 [   DEBUG] _read_channel_expect read_data: Fri Aug 14 19:12:20.025 JST
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:20.266 [   DEBUG] Pattern found: RP/0/RP0/CPU0:NCS\-5501c\-ME04 terminal width 511
Fri Aug 14 19:12:20.025 JST
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-14 19:12:20.676 [   DEBUG] read_channel: 

[Troubleshoot]

class CiscoXrBase(CiscoBaseConnection):
    def establish_connection(self):
        """Establish SSH connection to the network device"""
        super().establish_connection(width=511, height=511)

    def session_preparation(self):
        """Prepare the session after the connection has been established."""
        self._test_channel_read()
        self.set_base_prompt()
        self.disable_paging()
        self.set_terminal_width(command="terminal width 511")
        # Clear the read buffer
        time.sleep(0.3 * self.global_delay_factor)
        self.clear_buffer()

After some code trace and experiment, I found the time duration could be reduced by the following enhancement.

  • disable_paging() and set_terminal_width() can be removed when we calls establish_connection(width=511, height=0)
  • _test_channel_read() always takes about 2 secs+ due to extra sleep(final_delay * delay_factor) in _read_channel_timing().
  • _test_channel_read() can be replaced by read_until_pattern() with regular expression for CiscoXr prompt.
  • set_base_prompt() can be removed if we set a prompt string directly to self.base_prompt.

[Question]

Would you think if it’s ok for now to improve the behavior in CiscoXrBase only, even though this might be a common issue?

If yes, I’m going to submit a pull request only for CiscoXrBase changes.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
taisasakcommented, Aug 18, 2020

@ktbyers

Thank you for your comments. That would be great if both _test_channel_read() and extra sleep before clear_buffer() could be removed.

diff --git a/netmiko/cisco/cisco_xr.py b/netmiko/cisco/cisco_xr.py
index ad945f5..493c821 100644
--- a/netmiko/cisco/cisco_xr.py
+++ b/netmiko/cisco/cisco_xr.py
@@ -10,12 +10,9 @@ class CiscoXrBase(CiscoBaseConnection):

     def session_preparation(self):
         """Prepare the session after the connection has been established."""
-        self._test_channel_read()
         self.set_base_prompt()
         self.disable_paging()
-        self.set_terminal_width(command="terminal width 511")
         # Clear the read buffer
-        time.sleep(0.3 * self.global_delay_factor)
         self.clear_buffer()

     def send_config_set(self, config_commands=None, exit_config_mode=False, **kwargs):

I modified the patch like above and verified it could also reduce the time duration a lot. Please see the below logs for details.

  • CiscoXr (syslog)
RP/0/RP0/CPU0:2020 Aug 18 15:25:53.038 JST: SSHD_[66111]: %SECURITY-SSHD-6-INFO_SUCCESS : Successfully authenticated user 'cisco' from '10.71.38.195' on 'vty1'(cipher 'aes128-ctr', mac 'hmac-sha2-256')
  • netmiko (logger)
2020-08-18 15:25:53.112 [   DEBUG] read_channel: 
2020-08-18 15:25:53.113 [   DEBUG] write_channel: b'\n'
2020-08-18 15:25:53.217 [   DEBUG] read_channel: 

2020-08-18 15:25:53.218 [   DEBUG] read_channel: 
2020-08-18 15:25:53.218 [   DEBUG] write_channel: b'\n'
2020-08-18 15:25:53.323 [   DEBUG] read_channel: 


RP/0/RP0/CPU0:NCS-5501c-ME04#
RP/0/RP0/CPU0:NCS-5501c-ME04#
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-18 15:25:53.525 [   DEBUG] read_channel: 
2020-08-18 15:25:53.526 [   DEBUG] [find_prompt()]: prompt is RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-18 15:25:53.728 [   DEBUG] read_channel: 
2020-08-18 15:25:53.729 [   DEBUG] In disable_paging
2020-08-18 15:25:53.730 [   DEBUG] Command: terminal length 0

2020-08-18 15:25:53.730 [   DEBUG] write_channel: b'terminal length 0\n'
2020-08-18 15:25:53.731 [   DEBUG] Pattern is: RP/0/RP0/CPU0:NCS\-5501c\-ME04
2020-08-18 15:25:53.756 [   DEBUG] _read_channel_expect read_data: terminal length 0

2020-08-18 15:25:53.860 [   DEBUG] _read_channel_expect read_data: Tue Aug 18 15:25:53.791 JST
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-18 15:25:53.861 [   DEBUG] Pattern found: RP/0/RP0/CPU0:NCS\-5501c\-ME04 terminal length 0
Tue Aug 18 15:25:53.791 JST
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-18 15:25:53.861 [   DEBUG] terminal length 0
Tue Aug 18 15:25:53.791 JST
RP/0/RP0/CPU0:NCS-5501c-ME04#
2020-08-18 15:25:53.862 [   DEBUG] Exiting disable_paging
2020-08-18 15:25:53.967 [   DEBUG] read_channel: 

Yes, I tested on the below IOS-XR platforms and versions and it was all working. However, I haven’t yet tested it on XR7 (which is a new IOS-XR software architecture running on Cisco 8000 and some NCS 540 platforms)

Regarding the above XR7 (new IOS-XR software architecture), I checked with the XR code base and SSH server implementation seems to be same as other XR architectures (Evolved XR 64bit and Classic XR 32bit). I also verified the below control was also working well with Cisco 8000 in my local lab. It’s not required anymore in this discussion, though.

super().establish_connection(width=511, height=0)

Thanks, Taisuke

0reactions
ktbyerscommented, Sep 12, 2020

Develop branch should have new performance improvements for IOS-XR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shortening the preparation time of the single prolonged breath ...
We show here how to reduce the mean preparation time from 26 to 3.5 min without compromising breath-hold duration.
Read more >
Virtual Session Preparation - Lotus Life Counseling
Despite the perks, having sessions remotely can sometimes affect our ability to be fully present during session time. Below are a few helpful...
Read more >
RTR sessions time out while waiting for "preparing" phase of ...
I was trying to GET a 60mb file off of a host earlier today and the session got stuck at "preparing" for over...
Read more >
Set session duration - AWS IAM Identity Center (successor to ...
The minimum session duration is 1 hour, and can be set to a maximum of 12 hours. IAM Identity Center automatically creates IAM...
Read more >
Prepare a Session - Pearson Assessment Support
You must prepare a session to set up test elements before test day. The time the system takes to prepare sessions depends on...
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