网络设备配置的备份

这个其实是网络工程师的工作,有以下两种方法:

一、用ssh备份Cisco设备的脚本 需要事先在/root/.ssh/config配置好直接登录,并且在Cisco设备里设置好权限级别,可以执行show run

1#!/bin/sh
2sshcmd="ssh -o LogLevel=quiet"
3$sshcmd $1 "show run" > /root/backup/$1-$(date '+%Y%m%d').txt

二、备份Cisco设备的Python脚本,这个可控性更高: 首选需要在/export/servers/python363装好python, pip install netmiko 其次,在路由器上可以配置en的密码

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

Lvm卷的clone方法
Rsyslog的一些特殊用法
comments powered by Disqus