ktbyers / netmiko

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

Inconsistent execution of script error:paramiko.buffered_pipe.PipeTimeout #3419

Open umesh-pathak opened 5 months ago

umesh-pathak commented 5 months ago

Description of Issue/Question

The script created to change hostname on severla routers and then save the config.When i run the script it works for dew devices and then throw errror.

as below

Connecging to device: 198.19.98.31 host name changed save config working Connecging to device: 198.19.98.33 host name changed save config working Connecging to device: 198.19.98.38 host name changed Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/paramiko/channel.py", line 699, in recv out = self.in_buffer.read(nbytes, self.timeout) File "/usr/local/lib/python3.6/site-packages/paramiko/buffered_pipe.py", line 164, in read raise PipeTimeout() paramiko.buffered_pipe.PipeTimeout

Setup

We have linux based bastion/jump sever ,I am running the script from that machine.

Netmiko version

Python 3.6.8 (default, Nov 14 2023, 16:29:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information.

import netmiko netmiko.version '3.1.1'

Netmiko device_type (if relevant to the issue)

(Paste device_type between quotes below)

cisco routers



### Steps to Reproduce the Issue

### Error Traceback
(Paste the complete traceback of the exception between quotes below)

Connecging to device: 198.19.98.31
host name changed
save config working
Connecging to device: 198.19.98.33
host name changed
save config working
Connecging to device: 198.19.98.38
host name changed
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/paramiko/channel.py", line 699, in recv
    out = self.in_buffer.read(nbytes, self.timeout)
  File "/usr/local/lib/python3.6/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 "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 550, in _read_channel_expect
    new_data = self.remote_conn.recv(MAX_BUFFER)
  File "/usr/local/lib/python3.6/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 "test2.py", line 35, in <module>
    net_connect.save_config()
  File "/usr/local/lib/python3.6/site-packages/netmiko/cisco/cisco_ios.py", line 34, in save_config
    cmd=cmd, confirm=confirm, confirm_response=confirm_response
  File "/usr/local/lib/python3.6/site-packages/netmiko/cisco_base_connection.py", line 203, in save_config
    self.enable()
  File "/usr/local/lib/python3.6/site-packages/netmiko/cisco_base_connection.py", line 18, in enable
    return super().enable(cmd=cmd, pattern=pattern, re_flags=re_flags)
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 1564, in enable
    if not self.check_enable_mode():
  File "/usr/local/lib/python3.6/site-packages/netmiko/cisco_base_connection.py", line 14, in check_enable_mode
    return super().check_enable_mode(check_string=check_string)
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 1544, in check_enable_mode
    output = self.read_until_prompt()
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 623, in read_until_prompt
    return self._read_channel_expect(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 561, in _read_channel_expect
    "Timed-out reading channel, data not available."
netmiko.ssh_exception.NetmikoTimeoutException: Timed-out reading channel, data not available.

### Relevant Python code
(Please try to essentialize your Python code to the minimum code needed to reproduce the issue)
(Paste the code between the quotes below)

from netmiko import (ConnectHandler,NetmikoTimeoutException,NetmikoAuthenticationException)
from getpass import getpass
from datetime import datetime
import itertools

Error = open("error.txt","a+")
Success = open("success.txt","a+")

Int_Devices = ['198.19.98.49', '198.19.98.56', '198.19.98.57', '198.19.98.64', '198.19.98.69', '198.19.98.72', '198.19.98.73', '198.19.98.75', '198.19.98.78', '198.19.98.80', '198.19.98.
84', '198.19.98.85', '198.19.98.86', '198.19.98.89', '198.19.98.91', '198.19.98.93', '198.19.98.94', '198.19.98.95', '198.19.98.96', '198.19.98.99', '198.19.98.100', '198.19.98.101', '19
8.19.98.105', '198.19.98.107', '198.19.98.117', '198.19.98.119', '198.19.98.139', '198.19.98.146', '198.19.98.148', '198.19.98.149', '198.19.98.150', '198.19.98.153', '198.19.98.154', '1
98.19.98.156', '198.19.98.159', '198.19.98.160', '198.19.98.161', '198.19.98.162', '198.19.98.164', '198.19.98.166', '198.19.98.167', '198.19.98.168', '198.19.98.169', '198.19.98.170', '
198.19.98.171', '198.19.98.172', '198.19.98.173', '198.19.98.174', '198.19.98.175', '198.19.98.176', '198.19.98.178', '198.19.98.179', '198.19.98.180', '198.19.98.181', '198.19.98.182', 
'198.19.98.183', '198.19.98.184', '198.19.98.185', '198.19.98.198', '198.19.98.223', '198.19.98.236', '198.19.98.245', '198.19.98.246', '198.19.99.15', '198.19.99.22', '198.19.99.25', '1
98.19.99.26', '198.19.99.36', '198.19.99.43']
Command = ['hostname ABC-SITE049-ROUTER', 'hostname ABC-SITE056-ROUTER', 'hostname ABC-SITE057-ROUTER', 'hostname ABC-SITE064-ROUTER', 'hostname ABC-SITE069-ROUT
ER', 
'hostname ABC-SITE072-ROUTER', 'hostname ABC-SITE073-ROUTER', 'hostname ABC-SITE075-ROUTER', 'hostname ABC-SITE078-ROUTER', 'hostname ABC-SITE080-ROUTER', 
'hostname ABC-SITE084-ROUTER', 'hostname ABC-SITE085-ROUTER', 'hostname ABC-SITE086-ROUTER','hostname ABC-SITE089-ROUTER', 'hostname ABC-SITE091-ROUTER', 
'hostname ABC-SITE093-ROUTER', 'hostname ABC-SITE094-ROUTER', 'hostname ABC-SITE095-ROUTER', 'hostname ABC-SITE096-ROUTER', 'hostname ABC-SITE099-ROUTER', 
'hostname ABC-SITE100-ROUTER', 'hostname ABC-SITE101-ROUTER', 'hostname ABC-SITE105-ROUTER', 'hostname ABC-SITE107-ROUTER', 'hostname ABC-SITE117-ROUTER', 
'hostname ABC-SITE119-ROUTER', 'hostname ABC-SITE139-ROUTER', 'hostname ABC-SITE146-ROUTER', 'hostname ABC-SITE148-ROUTER', 'hostname ABC-SITE149-ROUTER', 
'hostname ABC-SITE150-ROUTER', 'hostname ABC-SITE153-ROUTER', 'hostname ABC-SITE154-ROUTER', 'hostname ABC-SITE156-ROUTER', 'hostname ABC-SITE159-ROUTER', 
'hostname ABC-SITE160-ROUTER', 'hostname ABC-SITE161-ROUTER', 'hostname ABC-SITE162-ROUTER', 'hostname ABC-SITE164-ROUTER', 'hostname ABC-SITE166-ROUTER', 
'hostname ABC-SITE167-ROUTER', 'hostname ABC-SITE168-ROUTER', 'hostname ABC-SITE169-ROUTER', 'hostname ABC-SITE170-ROUTER', 'hostname ABC-SITE171-ROUTER', 
'hostname ABC-SITE172-ROUTER', 'hostname ABC-SITE173-ROUTER', 'hostname ABC-SITE174-ROUTER', 'hostname ABC-SITE175-ROUTER', 'hostname ABC-SITE176-ROUTER', 
'hostname ABC-SITE178-ROUTER', 'hostname ABC-SITE179-ROUTER', 'hostname ABC-SITE180-ROUTER', 'hostname ABC-SITE181-ROUTER', 'hostname ABC-SITE182-ROUTER', 
'hostname ABC-SITE183-ROUTER','hostname ABC-SITE184-ROUTER', 'hostname ABC-SITE185-ROUTER', 'hostname ABC-SITE198-ROUTER', 'hostname ABC-SITE223-ROUTER', 
'hostname ABC-SITE236-ROUTER','hostname ABC-SITE245-ROUTER', 'hostname ABC-SITE246-ROUTER', 'hostname ABC-SITE271-ROUTER', 'hostname ABC-SITE278-ROUTER', 
'hostname ABC-SITE281-ROUTER', 'hostname ABC-SITE282-ROUTER', 'hostname ABC-SITE292-ROUTER', 'hostname ABC-SITE299-ROUTER']
for (device,cmd) in zip(Int_Devices,Command):
        print("Connecging to device: " + device)
        try:
                net_connect = ConnectHandler(device_type = "cisco_ios" ,host = device ,username = "*******" , password ="*******",allow_auto_change=True)

        except NetmikoTimeoutException:
                output = ("The device is not reachable :" + device)
                Error.write("\n"  "\n")
                Error.write(str(datetime.now()))
                Error.write("\n"  "\n")
                Error.write(output)
                Error.write("\n"  "\n")
                continue
        except NetmikoAuthenticationException:
                output = ("There is Authentication issue in accessing the device: " +  device)
                Error.write("\n"  "\n")
                Error.write(str(datetime.now()))
                Error.write("\n"  "\n")
                Error.write(output)
                Error.write("\n"  "\n")
                continue
        net_connect.send_config_set(cmd, cmd_verify=False)
        print('host name changed')
        net_connect.save_config()
        print('save config working')
        config_done = net_connect.send_command("sh run | in hostname")
        print(config_done)
        net_connect.disconnect()