简体   繁体   English

使用python脚本保存cisco交换机/路由器的启动配置?

[英]Save startup-configuration from switchs/routers cisco with python script?

I Would like to backup my switchs/routers configuration cisco automatically.我想自动备份我的交换机/路由器配置 cisco。

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 ?但它不起作用......你能告诉我怎么做吗?

You were sending them as "config commands".您将它们作为“配置命令”发送。 In IOS, copying to a TFTP server must be done from Privileged EXEC mode.在 IOS 中,必须从 Privileged EXEC 模式复制到 TFTP 服务器。 If executed from Global Config, the command will not work unless it is preceded by "do" as in do copy start tftp://... .如果从全局配置执行,该命令将不起作用,除非它前面有“do”,如do copy start tftp://...

Are you trying to back up the startup config to a TFTP server?您是否尝试将启动配置备份到 TFTP 服务器? Interesting choice to name the config "test.bin" btw.顺便说一句,将配置命名为“test.bin”的有趣选择。

You can do this in 2 ways:您可以通过两种方式执行此操作:

  1. Backup over TFTP to a device, like you are doing像您一样通过 TFTP 备份到设备
  2. Backup to a file by capturing the output of a "show run" command通过捕获“show run”命令的输出备份到文件

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.第二个选项很酷:即使您的设备在访问 TFTP 服务器时遇到问题,您仍然可以备份配置。

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所以你必须准备好对这两个问题都用“Enter”来回答

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如果您还有其他问题,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM