ktbyers / netmiko

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

NetMikoTimeoutException: Timed-out . #473

Closed tafiela closed 7 years ago

tafiela commented 7 years ago

Hi,

I am new to programming and want to thank Kirk Byers for this awesome helpful module. Everything has been working great until I pushed it a little bit, here is my code:

' import time import telnetlib import getpass import sys import os from netmiko import ConnectHandler

username = raw_input('Please Enter your username: ') password= getpass.getpass()

host = {'172.16.1.133': 'west_switch'}

os.chdir('Network-workplace/Learning/Testing/Python_Network_scripts/Telnet_folder/')

hostname=1

for key, value in host.items():

print "\n"
print "*"*30
print "Conifguration starting for : {}".format(key)
print "*"*30

connect = telnetlib.Telnet(key)
print "\nConnected to {}\n".format(key)
connect.read_until('Username: ')
connect.write('cisco\n')

connect.read_until('Password: ')
connect.write('cisco\n')

connect.write('config t\n')
print "1"
connect.write('hostname asset{}\n'.format(hostname))
print "2"
connect.write('username mohammadouran priv 15 sec 0 cisco\n')
print "3"
connect.write('ip domain-name virl.virl\n')
print "4"

connect.write('crypto key  generate rsa\n')
print "5"

connect.read_until('How many bits in the modulus [512]: ')
print "6"
connect.write('1024\n')
time.sleep(2)
print "7"
connect.write('ip ssh ver 2\n')
print "8"
connect.write('line vty 0 4\n')
print "9"
connect.write('transport input telnet ssh\n')
print "10"
time.sleep(3)
print "Closing Telnet session to {}\n".format(key)
connect.close()

print "{} is ready for SSH connections".format(key)

print "\nPreparing for SSH session ...\n"
time.sleep(2)

print "\nOpening config file for {} ".format(key)
f=open(value, 'rb')
f.seek(0)

print "\nEstabilishing SSH connection"
ssh_connect = ConnectHandler(device_type = 'cisco_ios', ip=key, username=username, password= password)
time.sleep(2)

print "\nSending Configuration from file ... "
ssh_connect.send_config_set(f.read())

print "\nSending Show run command to file ... "
output = ssh_connect.send_command("do show run\n")
time.sleep(3)

print "\nsaving output ... "
output_file = open('{}_configurationg_output'.format(key), 'w')

print "\nclosing files ..."
f.close()
output_file.close()

print "\nSaving configuration ... "
ssh_connect.send_command('do wr me\n')
time.sleep (3)

print "\nclosing SSH connection "
ssh_connect.close()

print "="*30
print "Conifguration complete for : {}".format(key)
print "="*30

hostname+=1

exit(0) '

#Here is whats in the config file west_router:

config t hostname west-router

inter lo 12 desc config_via_netmiko

here is the error im getting :

...

Opening config file for 172.16.1.133

Estabilishing SSH connection

Sending Configuration from file ... Traceback (most recent call last): File "TelnetTest.py", line 79, in ssh_connect.send_config_set(f.read()) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 956, in send_config_set output += self.exit_config_mode() File "/Library/Python/2.7/site-packages/netmiko/cisco_base_connection.py", line 50, in exit_config_mode pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 906, in exit_config_mode if self.check_config_mode(): File "/Library/Python/2.7/site-packages/netmiko/cisco_base_connection.py", line 32, in check_config_mode pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 887, in check_config_mode output = self.read_until_pattern(pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 327, in read_until_pattern return self._read_channel_expect(*args, **kwargs) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 278, in _read_channel_expect raise NetMikoTimeoutException("Timed-out reading channel, data not available.") netmiko.ssh_exception.NetMikoTimeoutException: Timed-out reading channel, data not available.

==============

As you can see i get to "Sending Configuration from file ... " and thats when it gives me the erro, whats funny, is the configuration in the file gets commited completly, i tried that with much longer config file and the configuration does get commited, it seems like it doesnt know how to get out of the config file back to the script. any thoughts or tips please? :)

p.s for some reason i couldnt get the whole script in the code view.

ktbyers commented 7 years ago

Use the send_config_from_file(filename) method to send configuration commands from files.

That method will automatically handle opening/closing the file.

tafiela commented 7 years ago

Hi Kirk,

Thanks for replying.

still the same issue, here is what i used : ssh_connect.send_config_from_file(value)

same error, here is the error:

File "Network_Configuration.py", line 103, in <module> ssh_connect.send_config_from_file(value) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 925, in send_config_from_file return self.send_config_set(cfg_file, **kwargs) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 956, in send_config_set output += self.exit_config_mode() File "/Library/Python/2.7/site-packages/netmiko/cisco_base_connection.py", line 50, in exit_config_mode pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 906, in exit_config_mode if self.check_config_mode(): File "/Library/Python/2.7/site-packages/netmiko/cisco_base_connection.py", line 32, in check_config_mode pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 887, in check_config_mode output = self.read_until_pattern(pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 327, in read_until_pattern return self._read_channel_expect(*args, **kwargs) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 278, in _read_channel_expect raise NetMikoTimeoutException("Timed-out reading channel, data not available.") netmiko.ssh_exception.NetMikoTimeoutException: Timed-out reading channel, data not available.

ktbyers commented 7 years ago

@tafiela You probably should turn on logging and see if you can get more details on what if fails.

import logging
logging.basicConfig(filename='test.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

What are the configuration commands you are doing?

tafiela commented 7 years ago

I simplified my script to make it easier to pinpoint the issue, however getting the same exact error. I am able to mitigate the error using an exception handling with the try-except

here is my script :

`import time import getpass import os import logging from netmiko import ConnectHandler

logging.basicConfig(filename='test.log', level=logging.DEBUG) logger = logging.getLogger("netmiko")

password=getpass.getpass()

os.chdir('~/Python_Network_scripts/Configuration_Completed/')

host_IP= {'device_type':'cisco_ios', 'ip':'172.16.1.10', 'username':'cisco', 'password' : password}

connect = ConnectHandler(**host_IP) time.sleep(2) connect.send_config_from_file('west_router') time.sleep(3) connect.send_command('do wr me\n')`

here is the configuration file called : _westrouter

config t hostname west-router inter loo 10 descr python_script_config ip addre 3.3.3.133 255.255.255.0 router eigrp 10 no auto summary network 10.10.10.0 0.0.0.255 inter gig 0/2 description EIGRP-peering-interface ip add 10.10.10.3 255.255.255.0 ip hello-interval eigrp 10 3 ip hold-time eigrp 10 10 no shut

Here is the error:

File "SSH_OSPF_Config.py", line 23, in <module> connect.send_config_from_file('west_router') File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 925, in send_config_from_file return self.send_config_set(cfg_file, **kwargs) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 956, in send_config_set output += self.exit_config_mode() File "/Library/Python/2.7/site-packages/netmiko/cisco_base_connection.py", line 50, in exit_config_mode pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 906, in exit_config_mode if self.check_config_mode(): File "/Library/Python/2.7/site-packages/netmiko/cisco_base_connection.py", line 32, in check_config_mode pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 887, in check_config_mode output = self.read_until_pattern(pattern=pattern) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 327, in read_until_pattern return self._read_channel_expect(*args, **kwargs) File "/Library/Python/2.7/site-packages/netmiko/base_connection.py", line 278, in _read_channel_expect raise NetMikoTimeoutException("Timed-out reading channel, data not available.") netmiko.ssh_exception.NetMikoTimeoutException: Timed-out reading channel, data not available.

here is the error log :

`DEBUG:paramiko.transport:starting thread (client mode): 0x1067c110L DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.1.2 DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-Cisco-1.25 INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25) DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-cbc', u'3des-cbc', u'aes192-cbc', u'aes256-cbc'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-cbc', u'3des-cbc', u'aes192-cbc', u'aes256-cbc'] client mac:[u'hmac-sha1', u'hmac-sha1-96'] server mac:[u'hmac-sha1', u'hmac-sha1-96'] client compress:[u'none'] server compress:[u'none'] client lang:[u''] server lang:[u''] kex follows?False DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group1-sha1 DEBUG:paramiko.transport:Cipher agreed: aes128-ctr DEBUG:paramiko.transport:MAC agreed: hmac-sha1-96 DEBUG:paramiko.transport:Compression agreed: none DEBUG:paramiko.transport:kex engine KexGroup1 specified hash_algo DEBUG:paramiko.transport:Switch to new keys ... DEBUG:paramiko.transport:Adding ssh-rsa host key for 172.16.1.10: 06d3f7fc1b2abcea3418364f0ef955d1 DEBUG:paramiko.transport:userauth is OK INFO:paramiko.transport:Auth banner:


DEBUG:netmiko:read_channel: iosv-1# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: terminal length 0

DEBUG:netmiko:_read_channel_expect read_data: t DEBUG:netmiko:_read_channel_expect read_data: erminal length 0 iosv-1# DEBUG:netmiko:write_channel: terminal width 511

DEBUG:netmiko:_read_channel_expect read_data: te DEBUG:netmiko:_read_channel_expect read_data: rminal width 511 iosv-1# DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel:

DEBUG:netmiko:_read_channel_expect read_data:

DEBUG:netmiko:_read_channel_expect read_data: iosv-1# DEBUG:netmiko:write_channel: config term

DEBUG:netmiko:_read_channel_expect read_data: c DEBUG:netmiko:_read_channel_expect read_data: onfig term Enter configuration commands, one per line. End with CNTL/Z. iosv-1(config)# DEBUG:netmiko:write_channel:

DEBUG:netmiko:_read_channel_expect read_data: iosv-1(config)# DEBUG:netmiko:write_channel: config t

DEBUG:netmiko:write_channel: hostname west-router

DEBUG:netmiko:write_channel: inter loo 10

DEBUG:netmiko:write_channel: descr python_script_config

DEBUG:netmiko:write_channel: ip addre 3.3.3.133 255.255.255.0

DEBUG:netmiko:write_channel: router eigrp 10

DEBUG:netmiko:write_channel: no auto summary

DEBUG:netmiko:write_channel: network 10.10.10.0 0.0.0.255

DEBUG:netmiko:write_channel: inter gig 0/2

DEBUG:netmiko:write_channel: description EIGRP-peering-interface

DEBUG:netmiko:write_channel: ip add 10.10.10.3 255.255.255.0

DEBUG:netmiko:write_channel: ip hello-interval eigrp 10 3

DEBUG:netmiko:write_channel: ip hold-time eigrp 10 10

DEBUG:netmiko:write_channel: no shut

DEBUG:netmiko:write_channel:

DEBUG:netmiko:read_channel: config t ^ % Invalid input detected at '^' marker.

iosv-1(config)#hostname west-router west-router(config)#inter loo 10 west-router(config-if)#descr python_script_config west-router(config-if)#ip addre 3.3.3.133 255.255.255.0 west-router(config-if)#router eigrp 10 west-router(config-router)#no auto summary ^ % Invalid input detected at '^' marker.

west-router(config-router)#network 10.10.10.0 0.0.0.255 west-router(config-router)#inter gig 0/2 west-router(config-if)#description EIGRP-peering-interface west-router(config-if)#ip add 10.10.10.3 255.255.255.0 west-router(config-if)#ip hello-interval eigrp 10 3 west-router(config-if)#ip hold-time eigrp 10 10 west-router(config-if)#no shut west-router(config-if)# west-router(config-if)# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel:

DEBUG:netmiko:_read_channel_expect read_data:

DEBUG:netmiko:_read_channel_expect read_data: west-router(config-if)# DEBUG:paramiko.transport:EOF in transport thread

`

ktbyers commented 7 years ago

Changing the hostname is problematic; you will probably need to handle that separately (as it changes the router prompt).

You also don't need config t (as it does that automatically).

So try it without the hostname change and see if it works.

tafiela commented 7 years ago

Amazing, thank you, sir!

Solved!

191801737 commented 7 years ago

@ktbyers how to change the hostname in cisco switch i want to use the "send_config_set" change the hostname by netmiko (1.4.2)

        config_commands = ['hostname cisco2960',
                           'end',
                           'write memory']
        output = net_connect.send_config_set(config_commands)

NetMikoTimeoutException: Timed-out reading channel

superloopnetwork commented 6 years ago

Is changing the hostname not supported then, @ktbyers ? Or is there a work around?

ktbyers commented 6 years ago

Yes, it should work fine. I just tested:

config_commands = ['hostname cisco2960',]
output = net_connect.send_config_set(config_commands)

The above is invalid, you can't have end and write memory in your config commands. Netmiko automatically does the end and write memory is not a config command.

superloopnetwork commented 6 years ago

You me mentioned above that changing the hostname is problematic.. I face the same issue too when I change the hostname.. It times out.. All other config changes works perfectly though..

ktbyers commented 6 years ago

Changing a hostname should work now...the earlier issue was from over a year ago. Create a new issue and post your exception message.

Regards, Kirk