Reputation: 1
I Would like to backup my switchs/routers configuration cisco automatically.
I try to create this script :
#!/usr/bin/env python3
#-*- conding: utf-8 -*-
from netmiko import ConnectHandler
cisco_test = {
'device_type': 'cisco_ios',
'host': 'xxx.xxx.xxx.xxx',
'username': 'xxxxxx',
'password': 'xxxxxxxxxx',
'secret': 'xxxxxxx',
}
net_connect = ConnectHandler(**cisco_test)
net_connect.enable()
config_commands = ['copy start tftp://xxx.xxx.xxx.xxx/test.bin']
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.exit_enable_mode()
But it doesn't work... Could you show me how to do that ?
Upvotes: 0
Views: 3556
Reputation: 1
You were sending them as "config commands". In IOS, copying to a TFTP server must be done from Privileged EXEC mode. If executed from Global Config, the command will not work unless it is preceded by "do" as in do copy start tftp://...
.
Are you trying to back up the startup config to a TFTP server? Interesting choice to name the config "test.bin" btw.
You can do this in 2 ways:
Cool thing about the second option: even if you're device is having problems reaching a TFTP server, you can still back up the config.
METHOD 1
You will need to not only send the copy command, but you'll need to respond to the prompts you'll receive:
CISCO2921-K9#copy start tftp://10.122.151.118/cisco2921-k9-backup
Address or name of remote host [10.122.151.118]?
Destination filename [cisco2921-k9-backup]?
!!
1759 bytes copied in 0.064 secs (27484 bytes/sec)
CISCO2921-K9#
So you must be prepared to respond with "Enter" for both questions
This is an example for what a working script will look like:
from netmiko import ConnectHandler
# enter the IP for your TFTP server here
TFTP_SERVER = "10.1.1.1"
# to add a device, define its connection details below, then add its name
# to the list of "my_devices"
device1 = {
'device_type': 'cisco_ios',
'host': '10.1.1.1',
'username': 'admin',
'password': 'cisco123',
'secret': 'cisco123',
}
device2 = {
'device_type': 'cisco_xr',
'host': '10.1.1.2',
'username': 'admin',
'password': 'cisco123',
'secret': 'cisco123',
}
# make sure you add every device above to this list
my_devices = [device1, device2]
# This is where the action happens. Connect, backup, respond to prompts
# Feel free to change the date on the backup file name below,
# everything else should stay the same
i = 0
for device in my_devices:
i += 1
name = f"device{str(i)}"
net_connect = ConnectHandler(**device)
net_connect.enable()
copy_command = f"copy start tftp://{TFTP_SERVER}/{name}-backup-02-26-2020"
output = net_connect.send_command_timing(copy_command)
if "Address or name" in output:
output += net_connect.send_command_timing("\n")
if "Destination filename" in output:
output += net_connect.send_command_timing("\n")
net_connect.disconnect
print(output)
I hope this was helpful. Let me know if you have any more questions
Upvotes: 0