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.

moving into a vrf on a cisco device causes Netmiko to time out.

See original GitHub issue

Hello, so I have a scenario where I need to check some configuration in the global table and then in another vrf, however when doing this by sending multiple commands to netmiko I get a time out.

relevant code:

def ios_connector(i,q,ios_cmds,print_lock,outfile):
    while True:
        ip = q.get()
        IOS = {
                'device_type': 'cisco_ios',
                'host': ip,
                'username': conf.USERNAME,
                'password': conf.PASSWORD,
                #'secret': conf.SECRET,
                }
        # attempt to establish SSH session and quit with appropriate error if not successful
        try:
            starttime_time = datetime.now()
            print('INFO: connecting to '+str(ip))
            net_connect = ConnectHandler(**IOS)
            # collect output
            connection = net_connect.find_prompt()
            res = []
            for cmd in ios_cmds:
                output = net_connect.send_command(cmd)
                res.append(output)
            with print_lock:
                if len(res) > 0:
                    outfile.write(connection + " has returned the following output:\n\n")
                    for line in res:
                        outfile.write(line)
                    outfile.write('\n\n===\n\n')
                else:
                    outfile.write(connection + "\nno matches\n\n===\n\n")

        except (AuthenticationException):
            print("ERROR: Authentication Failure to {}\n".format(ip))
            q.task_done()
        except (NetMikoTimeoutException):
            print("ERROR: Timed out while trying to connect to {}\n".format(ip))
            q.task_done()
        except Exception as e:
            print('ERROR: Connection refused by ' + ip +'\n' + e)
            q.task_done()
        except (OSError) as e:
            print('ERROR: OSError\n' + e)
            q.task_done()
        except (EOFError) as e:
            print('ERROR: EOF error\n' + e)
            q.task_done()
        finally:
            net_connect.disconnect()
            endtime = datetime.now()
            print('Total time: {}'.format(endtime - starttime_time))
            q.task_done()

from the debug it looks like it’s a problem with find_prompt() as the prompt changes to include the vrf name, however it still has the trailing # as you would expect.

debug:

DEBUG:paramiko.transport:starting thread (client mode): 0x3747ba30 DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.1 DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-Cisco-1.25 INFO:paramiko.transport:Connected (version 2.0, client -1.25) DEBUG:paramiko.transport:kex algos:[‘diffie-hellman-group-exchange-sha1’, ‘diffie-hellman-group14-sha1’] server key:[‘ssh-rsa’] client encrypt:[‘aes128-ctr’, ‘aes192-ctr’, ‘aes256-ctr’] server encrypt:[‘aes128-ctr’, ‘aes192-ctr’, ‘aes256-ctr’] client mac:[‘hmac-sha2-256’, ‘hmac-sha2-512’, ‘hmac-sha1’, ‘hmac-sha1-96’] server mac:[‘hmac-sha2-256’, ‘hmac-sha2-512’, ‘hmac-sha1’, ‘hmac-sha1-96’] client compress:[‘none’] server compress:[‘none’] client lang:[‘’] server lang:[‘’] kex follows?False DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group-exchange-sha1 DEBUG:paramiko.transport:HostKey agreed: ssh-rsa DEBUG:paramiko.transport:Cipher agreed: aes128-ctr DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256 DEBUG:paramiko.transport:Compression agreed: none DEBUG:paramiko.transport:Got server p (2048 bits) DEBUG:paramiko.transport:kex engine KexGex specified hash_algo <built-in function openssl_sha1> DEBUG:paramiko.transport:Switch to new keys … DEBUG:paramiko.transport:Adding ssh-rsa host key for redacted DEBUG:paramiko.transport:userauth is OK INFO:paramiko.transport:Authentication (password) successful! DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes DEBUG:paramiko.transport:[chan 0] Max packet out: 4096 bytes DEBUG:paramiko.transport:Secsh channel 0 opened. DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok DEBUG:netmiko:read_channel: THIS DEVICE IS PART OF A

                    ------------------------



                         PRIVATE NETWORK

                         ---------------





         ************************************************

         * Unauthorised access or use of this equipment *

         *   is prohibited and constitutes an offence   *

         *     under the Computer Misuse Act 1990.      *

         *    If you are not authorised to use this     *

         *     system, terminate this session now.      *

         ************************************************

switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b’\n’ DEBUG:netmiko:read_channel:

switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:[find_prompt()]: prompt is switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:In disable_paging DEBUG:netmiko:Command: terminal length 0

DEBUG:netmiko:write_channel: b’terminal length 0\n’ DEBUG:netmiko:Pattern is: terminal\ length\ 0 DEBUG:netmiko:_read_channel_expect read_data: t DEBUG:netmiko:_read_channel_expect read_data: erminal length 0

switch-01# DEBUG:netmiko:Pattern found: terminal\ length\ 0 terminal length 0

switch-01# DEBUG:netmiko:terminal length 0

switch-01# DEBUG:netmiko:Exiting disable_paging DEBUG:netmiko:write_channel: b’terminal width 511\n’ DEBUG:netmiko:Pattern is: terminal\ width\ 511 DEBUG:netmiko:_read_channel_expect read_data: t DEBUG:netmiko:_read_channel_expect read_data: erminal width 511

switch-01# DEBUG:netmiko:Pattern found: terminal\ width\ 511 terminal width 511

switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b’\n’ DEBUG:netmiko:read_channel:

switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:[find_prompt()]: prompt is switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b’\n’ DEBUG:netmiko:read_channel:

switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:[find_prompt()]: prompt is switch-01# DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b’routing-context vrf vnName\n’ DEBUG:netmiko:Pattern is: routing-context\ vrf\ vnName DEBUG:netmiko:_read_channel_expect read_data: r DEBUG:netmiko:_read_channel_expect read_data: outing-context vrf vnName

switch-01%vnTrust# DEBUG:netmiko:Pattern found: routing-context\ vrf\ vnName routing-context vrf vnName

switch-01%vnTrust# DEBUG:netmiko:read_channel:

[repeated a lot]

DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b’\n’ DEBUG:netmiko:Pattern is: # DEBUG:netmiko:_read_channel_expect read_data:

switch-01%vnName# DEBUG:netmiko:Pattern found: #

switch-01%vnName# DEBUG:netmiko:exit_config_mode: DEBUG:netmiko:write_channel: b’exit\n’ DEBUG:paramiko.transport:EOF in transport thread

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
llama85commented, Mar 10, 2021

@ktbyers works like a charm! thank you.

0reactions
ktbyerscommented, Mar 9, 2021

@leearmitage I would try to execute the following:

            for cmd in ios_cmds:
                output = net_connect.send_command(cmd, expect_string=r"#")
            net_connect.set_base_prompt()

So here I use the expect_string argument so it doesn’t care about the prompt changing (this should allow both commands to be executed).

I then reset the base_prompt after the for-loop is done. In general, you probably need to execute that set_base_prompt() anytime the prompt fundamentally changes like that.

Let me know if that works (or not).

Read more comments on GitHub >

github_iconTop Results From Across the Web

send_config_set problem: Timed-out reading channel, data ...
I have an issue when using send_config_set using a Cisco IOS 88x router. We got a template that has around 65 commands, they...
Read more >
Understanding and Troubleshooting Idle Timeouts - Cisco
Calls do not disconnect. This is probably caused by a high dialer idle timeout value along with a loose interesting traffic definition. Idle...
Read more >
Netmiko4 read_timeout - Python for Network Engineers
As part of this method, Netmiko entered a loop searching for either the trailing router prompt or waiting until its read_timeout timer had ......
Read more >
Network Automation Appliance with netmiko - Support | GNS3
PING 192.168.122.210 (192.168.122.210) 56(84) bytes of data. 64 bytes from 192.168.122.210: icmp_seq=1 ttl=255 time= ...
Read more >
NAPALM Documentation - Read the Docs
The NAPALM-ios driver supports all Netmiko arguments as either standard arguments (hostname, username, password, timeout) or as optional_args ( ...
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