Cisco设备自动执行和备份的脚本

在日常工作中我们经常要对 Cisco 的网络设备的配置进行备份,或者和 suricata 联动的时候要执行操作。

方法其实很简单,调用 python 的相应模块即可。

准备工作如下:

首选需要在/export/servers/python363装好 python 3.6, pip install netmiko

其次,在路由器上可以配置en的密码

然后依次执行备份就可以了。

 1#!/export/servers/python363/bin/python3.6
 2from netmiko import Netmiko
 3import time
 4tw_bgp = {
 5        "device_type": "cisco_ios",
 6        "host": "tw-bgp",
 7        "ip": "192.168.1.10",
 8        "username": "noc",
 9        "use_keys": True,
10        "secret" : "xxxxxxxx",
11        "key_file": "/root/.ssh/id_jump_rsa_new",
12}
13tw_r1_e1 = {
14        "device_type": "cisco_ios",
15        "host": "tw-r1-e1",
16        "ip": "192.168.1.11",
17        "username": "noc",
18        "use_keys": True,
19        "key_file": "/root/.ssh/id_jump_rsa_new",
20}
21tw_r1_e2 = {
22        "device_type": "cisco_ios",
23        "host": "tw-r1-e2",
24        "ip": "192.168.1.12",
25        "username": "noc",
26        "use_keys": True,
27        "key_file": "/root/.ssh/id_jump_rsa_new",
28}
29tw_r2_e1 = {
30        "device_type": "cisco_ios",
31        "host": "tw-r2-e1",
32        "ip": "192.168.1.13",
33        "username": "noc",
34        "use_keys": True,
35        "key_file": "/root/.ssh/id_jump_rsa_new",
36}
37tw_r2_e2 = {
38        "device_type": "cisco_ios",
39        "host": "tw-r2-e2",
40        "ip": "192.168.1.14",
41        "username": "noc",
42        "use_keys": True,
43        "key_file": "/root/.ssh/id_jump_rsa_new",
44}
45
46devices=[tw_bgp, tw_r1_e1, tw_r1_e2, tw_r2_e1, tw_r2_e2]
47
48for dev in devices:
49    name = dev["ip"]
50    connection = Netmiko(**dev)
51    connection.enable()
52    out = connection.send_command("show running-config")
53    calender = time.strftime("%Y%m%d")
54    file_name = '{}-{}.txt'.format(dev["host"],calender)
55    file = open(file_name ,"w")
56    file.write(out)
57    file.close()
58    connection.disconnect()
59    print("BACKUP for %s done" %dev["host"])

用户态的NFS Server
Infrastructure as Code中packer的使用
comments powered by Disqus