Closed tafiela closed 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.
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.
@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?
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
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:
iosv-1# DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel:
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
`
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.
Amazing, thank you, sir!
Solved!
@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
Is changing the hostname not supported then, @ktbyers ? Or is there a work around?
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.
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..
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
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():
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.