Cisco设备配置文件定期备份

本文涉及的产品
云防火墙,500元 1000GB
简介:

Cisco设备配置文件定期备份

一、需求和应对之策

  公司IDC机房几台交换机和防火墙设备,因业务需要,经常在交换机上或防火墙上修改配置文件,虽说Cisco设备稳定性很好,但做好备份,可以防万一。

  网上有很多关于Cisco设备定期备份的文章,个人根据公司实际情况写了个备份脚本。

  个人采用自动交互expect获取Cisco设备的配置文件,让后通过FTP上传到FTP服务器。

  该脚本是放在Centos6.5服务器上,每周六晚上23:00执行。

二、线上脚本

1.脚本所在目录介绍

1
2
3
4
[root@localhost cisco_bak] # pwd/usr/local/scripts/cisco_bak
[root@localhost cisco_bak] # ls
cfg  cisco_bak.sh  ip_asa.txt  ip_switch.txt  telnet_asa.exp  telnet_switch.exp
up_cfg.sh

2.文件及目录介绍

  cfg是存放备份的Cisco设备的配置文件。

  telnet_switch.exp是通过Expect获取Cisco交换机的配置文件的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost cisco_bak] # cat telnet_switch.exp 
#!/usr/bin/expect
#This script is get switch cfg.
set  timeout 60
set  ip [lindex $argv 0]
set  password [lindex $argv 1]
spawn  /usr/kerberos/bin/telnet  $ip
expect  "Password:"
send  "$password\r"
expect  ">"
send  "enable\r"
expect  "Password:"
send  "$password\r"
expect  "#"
send  "show running-config\r"
while  {1} {
     sleep  1
     expect {
     "*More--"  {send  " " }
     "*#"  { break }
     }
}
send  "exit\r"
expect eof

  telnet_asa.exp是通过Expect获取Cisco防火墙的配置文件的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost cisco_bak] # cat telnet_asa.exp 
#!/usr/bin/expect
#This script is get switch cfg.
set  timeout 60
set  ip [lindex $argv 0]
set  password [lindex $argv 1]
spawn  /usr/kerberos/bin/telnet  $ip
expect  "*assword:"
send  "$password\r"
expect  ">"
send  "enable\r"
expect  "Password:"
send  "$password\r"
expect  "#"
send  "show running-config\r"
while  {1} {
     sleep  1
     expect {
     "*More --->"  {send  " " }
     "*#"  { break }
     }
}
send  "exit\r"
expect eof

  ip_switch.txt存放交换机的IP和密码。(我修改了,不能用公司的真实IP和密码。)

1
2
3
[root@localhost cisco_bak] # cat ip_switch.txt 
10.10.10.1   123
10.10.20.1   123

  ip_asa.txt存放防火墙的IP和密码。(我修改了,不能用公司的真实IP和密码。)

1
2
  [root@localhost cisco_bak] # cat ip_asa.txt 10.10.10.254    123
10.10.20.254    123

  cat up_cfg.sh是将备份的配置文件上传FTP备份。(也可以通过其他途径备份到存储服务器。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost cisco_bak] # cat up_cfg.sh 
#!/bin/bash
#This script is upload cfg to Remote Computer.
TODAY=` /bin/date  +%F`
#Local Path
L_PATH= /usr/local/scripts/cisco_bak
#date path
Date_PATH=` /bin/date  +%Y%m%d%H%M%S`
cd  ${L_PATH}
#$1 Ip,$2 User,$3 Passwd,$4 Remote_Path
/usr/kerberos/bin/ftp  -i -n - v  << !
open  $1
user $2  $3
bin
passive
cd  $4
lcd  ${L_PATH} /cfg
mput *${TODAY}.cfg
bye
!

  cisco_bak.sh是主程序,它将telnet_asa.exp 、telnet_switch.exp和up_cfg.sh整合到一个脚本中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost cisco_bak] # cat cisco_bak.sh 
#!/bin/bash
#
TODAY=` date  +%F`
PATH= /usr/local/scripts/cisco_bak
[ -d ${PATH} /cfg/  ] ||  /bin/mkdir  -p ${PATH} /cfg/
cd  ${PATH}
##ip.txt is stored in the user name and password.
#switch_bak
while  read  ip
do 
#IP_NAME is IP 
IP_NAME=` echo  "$ip"  /bin/awk  '{print $1}' `
/usr/bin/expect  telnet_switch.exp $ip > ${PATH} /cfg/switch_ ${IP_NAME}_${TODAY}.cfg
done  < ip_switch.txt
#asa_bak
while  read  ip
do 
#IP_NAME is IP 
#IP_NAME=`echo "$ip" | /bin/awk '{print $1}'`
/usr/bin/expect  telnet_asa.exp  $ip > ${PATH} /cfg/asa_ ${IP_NAME}_${TODAY}.cfg
done  < ip_asa.txt
##upload cfg to ftp
##/bin/bash ${PATH}/up_cfg.sh FTP_IP FTP_USER FTP_PASSWD FTP_PATH
/bin/bash  ${PATH} /up_cfg .sh 10.10.10.200 abc 123  /home/abc/Cisco_Devices

3.在linux上定期执行

1
2
[root@localhost cisco_bak] # crontab -l
00 23 * * 6  /bin/bash  /usr/local/scripts/cisco_bak/cisco_bak .sh  >>  /dev/null  2>&1

三、总结:

1.telnet_switch.exp和telnet_asa.exp很像,但在while循环中有区别,"*More--" {send " "}和"*More --->" {send " "}是不一样的。

2.有的防火墙需要用户名和密码才能登陆,可稍作修改telnet_asa.exp,传入三个参数:ip、name和passwd

3.可扩展:在备份失败时放送邮件通知管理员。







      本文转自独弹古调  51CTO博客,原文链接:http://blog.51cto.com/hunkz/1759138,如需转载请自行联系原作者




相关文章
|
Linux
centos 8 换阿里源
centos 8 换阿里源
3771 0
|
安全 Java 应用服务中间件
使用OkHttp工具时Authorization请求头丢失问题
记一次联调三方接口时&quot;Authorization&quot;请求头丢失问题, 使用工具OkHttp
使用OkHttp工具时Authorization请求头丢失问题
|
人工智能 Java 大数据
网站性能测试指标(QPS,TPS,吞吐量,响应时间)详解
QQ用得起来越少了,现在就加入300+技术微信群,下方公众号回复"微信群"即可加入。 常用的网站性能测试指标有:吞吐量、并发数、响应时间、性能计数器等。
4790 0
|
5月前
|
人工智能 前端开发 机器人
10个优质独立开发者社区
以下是我整理的10个优质独立开发者社区,都是自己平时经常逛或参与过的,分类整理方便不同需求的开发者参考
1281 7
|
Linux 编译器 C语言
Linux内核对GCC版本的检测
Linux内核对GCC版本的检测
|
缓存 开发工具 git
Docker 从构建开始导出一个镜像
docker build [OPTIONS] PATH | URL | -
536 1
|
Prometheus Cloud Native 网络安全
Prometheus+Grafana+Alertmanager部署教程(超详细)
Prometheus+Grafana+Alertmanager部署教程(超详细)
3072 0
Prometheus+Grafana+Alertmanager部署教程(超详细)
|
存储 安全 测试技术
Postman工具介绍
【6月更文挑战第1天】Postman是一款由Postman公司开发的API开发协作软件,广泛应用于API设计、构建、测试和安全管理。
|
存储 弹性计算 数据管理
阿里云对象存储OSS怎么收费?存储包流量包还是按量?
阿里云对象存储OSS收费有两种计费模式,即包年包月和按量付费,包年包月是指购买存储包、流量包来抵扣OSS产生的存储费核流量费,OSS标准(LRS)存储包100GB优惠价33元、500GB存储包半年162元、OSS存储包40GB一年9元,OSS流量包100G 49元/月
8866 2
阿里云对象存储OSS怎么收费?存储包流量包还是按量?
|
运维 监控 安全

热门文章

最新文章