netmiko: Timeout/Timed-out exceptions

I am trying to run config commands to a Cisco switch and keep getting the below error. The script, tools, etc is modified by me using Greg Mueller. Is there something that I am missing?

Traceback (most recent call last): File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\channel.py”, line 699, in recv out = self.in_buffer.read(nbytes, self.timeout) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\buffered_pipe.py”, line 164, in read raise PipeTimeout() paramiko.buffered_pipe.PipeTimeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\base_connection.py”, line 541, in _read_channel_expect new_data = self.remote_conn.recv(MAX_BUFFER) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\channel.py”, line 701, in recv raise socket.timeout() socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “03-02-2020_monday.py”, line 39, in <module> connection.send_config_set(commands) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\base_connection.py”, line 1704, in send_config_set output = self.config_mode(*cfg_mode_args) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\cisco_base_connection.py”, line 40, in config_mode return super().config_mode(config_command=config_command, pattern=pattern) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\base_connection.py”, line 1595, in config_mode if not self.check_config_mode(): File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\cisco\cisco_ios.py”, line 29, in check_config_mode return super().check_config_mode(check_string=check_string, pattern=pattern) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\cisco_base_connection.py”, line 30, in check_config_mode return super().check_config_mode(check_string=check_string, pattern=pattern) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\base_connection.py”, line 1582, in check_config_mode output = self.read_until_pattern(pattern=pattern) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\base_connection.py”, line 618, in read_until_pattern return self._read_channel_expect(*args, **kwargs) File “C:\Users\ksauter\AppData\Local\Programs\Python\Python38-32\lib\site-packages\netmiko\base_connection.py”, line 551, in _read_channel_expect raise NetmikoTimeoutException( netmiko.ssh_exception.NetmikoTimeoutException: Timed-out reading channel, data not available.

My script is below:

#!/usr/bin/env python

from __future__ import absolute_import, division, print_function

import json
import netmiko
import tools
import signal
import sys

#signal.signal(signal.SIGPIPE, signal.SIG_DFL)  # IOError: Broken pipe
signal.signal(signal.SIGINT, signal.SIG_DFL)  # KeyboardInterrupt: Ctrl-C


if len(sys.argv) < 3:
    print('Usage: cmdrunner.py commands.txt devices.json')
    exit()

#netmiko_exceptions = (netmiko.ssh_exception.NetMikoTimeoutException,
#                      netmiko.ssh_exception.NetMikoAuthenticationException)

username, password = tools.get_credentials()

with open(sys.argv[1]) as cmd_file:
    commands = cmd_file.readlines()

with open(sys.argv[2]) as dev_file:
     devices = json.load(dev_file)

for device in devices:
    device['username'] = username
    device['password'] = password
    try:
        print('-' * 79)
        print('Connecting to device:', device['ip'])
        connection = netmiko.ConnectHandler(**device)
        for command in commands:
            print('## Output of ' + command)
            connection.send_config_set(commands)
            print(connection.send_command(command))
            print()
        connection.disconnect()
    except netmiko_exceptions as e:
        print('Failed to ', device['ip'], e)`

tools are:
`from __future__ import absolute_import, division, print_function

from getpass import getpass

def get_input(prompt=''):
    try:
        line = raw_input(prompt)
    except NameError:
        line = input(prompt)
    return line


def get_credentials():
    """Prompt for and return a username and password."""
    username = get_input('Enter Username: ')
    password = None
    while not password:
        password = getpass()
        password_verify = getpass('Retype your password: ')
        if password != password_verify:
            print('Passwords do not match.  Try again.')
            password = None
    return username, password`

Device is: [ { “device_type”: “cisco_ios”, “ip”: “10.1.60.19”, “secret”: “secret”, “global_delay_factor”: 2, “blocking_timeout”: 16 } ]

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

You need to call .enable(). You also need to pass in a “secret” argument in “my_device”.

net_connect = Netmiko(**my_device)
net_connect.enable()
cfg_commands = ["username test password cisc0"]

output = net_connect.send_config_set(cfg_commands)
print(output)