Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netmiko.ssh_exception.NetMikoTimeoutException #1151

Closed
ghost opened this issue Mar 27, 2019 · 6 comments
Closed

netmiko.ssh_exception.NetMikoTimeoutException #1151

ghost opened this issue Mar 27, 2019 · 6 comments

Comments

@ghost
Copy link

ghost commented Mar 27, 2019

I HAVE A SIMPLE SCRIPT USING NETMIKO MODULE, THAT FROM A FILE WITH CONFIGS, TRY TO CONFIGURE A CISCO SWITCH. THE PROBLEM IS THAT IT ONLY WORKS SOMETIMES IN A RANDOM WAY, LIKE I RUN THE SCRIPT ONE TIME , IT RAISES THIS ERROR, THEN I RUN AGAIN AND IT WILL CONFIGURE THE SWITCH. i ALSO DID A GOOGLE SEARCH AND THE BEST I COULD FIND WAS THIS : #212

the ERROR : File "/usr/local/lib/python3.5/dist-packages/paramiko/channel.py", line 699, in recv
out = self.in_buffer.read(nbytes, self.timeout)
File "/usr/local/lib/python3.5/dist-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.5/dist-packages/netmiko/base_connection.py", line 519, in _read_channel_expect
new_data = self.remote_conn.recv(MAX_BUFFER)
File "/usr/local/lib/python3.5/dist-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 "compara_Configura.py", line 73, in
output = net_connect.send_config_from_file(ficheiro)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1502, in send_config_from_file
return self.send_config_set(cfg_file, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1567, in send_config_set
output += self.exit_config_mode()
File "/usr/local/lib/python3.5/dist-packages/netmiko/cisco_base_connection.py", line 56, in exit_config_mode
exit_config=exit_config, pattern=pattern
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1478, in exit_config_mode
if self.check_config_mode():
File "/usr/local/lib/python3.5/dist-packages/netmiko/cisco/cisco_ios.py", line 32, in check_config_mode
check_string=check_string, pattern=pattern
File "/usr/local/lib/python3.5/dist-packages/netmiko/cisco_base_connection.py", line 38, in check_config_mode
check_string=check_string, pattern=pattern
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1448, in check_config_mode
output = self.read_until_pattern(pattern=pattern)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 594, in read_until_pattern
return self._read_channel_expect(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 528, in _read_channel_expect
"Timed-out reading channel, data not available."
netmiko.ssh_exception.NetMikoTimeoutException: Timed-out reading channel, data not available.

the sCRIPT :

!/usr/bin/env python3

-- encoding: utf-8 --

"""
This script read the running config from a network device , and read previous known running config
from a file , and it compares it . if both are equal it does not do nothing .
if they are diferents try to configure the switch
"""
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
from show_run import show_run

iosv_l2 = {
'device_type': 'cisco_ios',
'ip' : '192.168.134.81',
'username' : 'nuno',
'password' : 'cisco'

'blocking_timeout':16,

'timeout': 60,

'session_timeout':60

}

Call fucntion to get running config of cisco network device and store in

a list " device_running_config"

device_running_config = show_run(iosv_l2)
for line in device_running_config:
line.splitlines()
print( device_running_config)

open a configuration file from the equipment that we know that is correct

ficheiro = open("backup_s1","r")
linhas = ficheiro.read().splitlines()
ficheiro.close()
print("################################################################")
print(linhas)
print("################################################################")

Compare 2 running configs - if they are equal do nothing . if they are

diferent config the network device from a file

file with the configuration commands of the network device

ficheiro = 's1_config'
if linhas == device_running_config:

print( "###########################################################")
print ("They are equal - do nothing")
print ("############################################################")

else:
print( "###########################################################")
print ("they are diferent \n Preparing to config")
print ("############################################################")
try:
net_connect = ConnectHandler(**iosv_l2)
except (AuthenticationException):
print ('Authentication failure: ' + ip_address_of_device)

except (NetMikoTimeoutException):
    print ('Timeout to device: ' + ip_address_of_device)
    #continue
except (EOFError):
    print ('End of file while attempting device ' + ip_address_of_device)
    #continue
except (SSHException):
    print ('SSH Issue. Are you sure SSH is enabled? ' + ip_address_of_device)
    #continue
except Exception as unknown_error:

output = net_connect.send_config_from_file(ficheiro)
print (output)

Thanks you all !

@carlmontanari
Copy link
Contributor

Are you changing the hostname by any chance? Also can you enable logging and post that here, see this link: https://github.com/ktbyers/netmiko/blob/develop/COMMON_ISSUES.md#enable-netmiko-logging-of-all-reads-and-writes-of-the-communications-channel

@ghost
Copy link
Author

ghost commented Mar 31, 2019

Im running python on a network automation machine inside GNS3.
I found a file /usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py that as the method refered. Its diferent from the example in the forum .
Nevertheless a put the line :

debug = True

def read_until_pattern(self, *args, **kwargs):
"""Read channel until pattern detected. Return ALL data available."""
return self._read_channel_expect(*args, **kwargs)

I run again de script to configure equipment and :

Preparing to config
############################################################
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/channel.py", line 699, in recv
out = self.in_buffer.read(nbytes, self.timeout)
File "/usr/local/lib/python3.5/dist-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.5/dist-packages/netmiko/base_connection.py", line 519, in _read_channel_expect
new_data = self.remote_conn.recv(MAX_BUFFER)
File "/usr/local/lib/python3.5/dist-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 "compara_Configura.py", line 74, in
output = net_connect.send_config_from_file(ficheiro)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1504, in send_config_from_file
return self.send_config_set(cfg_file, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1569, in send_config_set
output += self.exit_config_mode()
File "/usr/local/lib/python3.5/dist-packages/netmiko/cisco_base_connection.py", line 56, in exit_config_mode
exit_config=exit_config, pattern=pattern
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1480, in exit_config_mode
if self.check_config_mode():
File "/usr/local/lib/python3.5/dist-packages/netmiko/cisco/cisco_ios.py", line 32, in check_config_mode
check_string=check_string, pattern=pattern
File "/usr/local/lib/python3.5/dist-packages/netmiko/cisco_base_connection.py", line 38, in check_config_mode
check_string=check_string, pattern=pattern
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1450, in check_config_mode
output = self.read_until_pattern(pattern=pattern)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 595, in read_until_pattern
return self._read_channel_expect(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 528, in _read_channel_expect
"Timed-out reading channel, data not available."
netmiko.ssh_exception.NetMikoTimeoutException: Timed-out reading channel, data not available.

Run the script on a second try and it goes fine!

@ktbyers
Copy link
Owner

ktbyers commented Mar 31, 2019

@nonioAtoz Try setting global_delay_factor=4 in your device dictionary (that you pass to Netmiko):

iosv_l2 = {
'device_type': 'cisco_ios',
'ip' : '192.168.134.81',
'username' : 'nuno',
'password' : 'cisco',
'global_delay_factor': 4,
}

Does that help?

@ghost
Copy link
Author

ghost commented Apr 2, 2019

It did nt work ,
'global_delay_factor': 4, nor 'global_delay_factor': 8 , nor 'global_delay_factor': 16.

but i ithink i have found the problem ( im also new to network devices and protocols and make a lot of mistakes):
I was changing the interface of the swicth ( changing encapsulation from isl to dot1q) that is connected to the Linux network automation tool .
i removeed that configuration from the commands list and now its working .
Thanks s

carambar

@ktbyers
Copy link
Owner

ktbyers commented Apr 2, 2019

@nonioAtoz Okay, so I am assuming that means that your change was disconnecting the Linux automation server where Netmiko was running--thus causing the connection to break?

@ktbyers ktbyers closed this as completed Apr 2, 2019
@ghost
Copy link
Author

ghost commented Apr 2, 2019

i was perfoming this commands on switch
interface range ethernet 0/0 - 2
switchport trunk encapsulation dot1q
switchport mode trunk
switchport trunk native vlan 1

and this ports were configured and the the connection breaks .
I have to run the script again so that all the configurations and this ports and all others to be configured.
since i change the range ethernet to 0/1-2 , problema solved

I also run the same problem when reseting switch configuration to defaults .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants