您的位置:首页 > 理论基础 > 计算机网络

网络设备(思科,华为交换机、路由器)自动备份配置脚本

2018-03-04 15:02 1291 查看
1、网络设备(含华为路由器、交换机、思科交换机)每天对配置进行一次备份。备份完配置后,发送邮件
2、假如你的设备华为设备管理地址为:10.1.1.1-40 , 思科设备为 10.2.1.1-40 ,需要分别对其进行备份。可以建一个Huawei.list 、Cisco.list 分别对设备管理ip进行存储。同样,该脚本模式也适用于其它设备。



3、操作
2> 脚本直接放在一台可与其互通的跳板机上,做一个计划任务每天一备。
1>搭建好tftp 或者 ftp 服务器,本例用tftp服务器。
3>脚本如下:

#! /bin/bash

tftp_server=10.10.10.10    #tftp-server 服务器,用于存储备份文件
tftp_dir=/var/lib/tftpboot     #根目录
back_dir=`date +"%Y%m%d"`     #以时间名称备份
mkdir /var/lib/tftpboot/$back_dir

echo "tftp_server_ip 10.10.10 , backup_dir /var/lib/tftpboot/" >> $PWD/result

echo "#### $back_dir huawei-sw backup begin ####" >> $PWD/result

for ip in `cat $PWD/huawei.list`    #华为的list
do
ping -c 2 $ip &>2 >/dev/null

/usr/bin/expect <<-EOF     #调用expect
set timeout 5
spawn telnet $ip
expect "*Username:"
send "admin\r"
expect "*Password:"
send "* * * * * \r"
expect "*>"
send "tftp $tftp_server put vrpcfg.zip $ip.zip\r"
set timeout 10
expect " *second(s)."
send "quit\r"
EOF
sleep 1s
echo "$ip" >> $PWD/result
done

mv /var/lib/tftpboot/*.zip /var/lib/tftpboot/$back_dir      #t移动

echo "#### $back_dir huawei-sw backup end ####" >> $PWD/result

echo "#### $back_dir cisco-sw backup begin  ####" >> $PWD/result

for cip in `cat $PWD/cisco.list`         #思科list

do
ping -c 2 $cip &>2 >/dev/null

/usr/bin/expect <<-EOF
set timeout 5
spawn telnet $cip
expect "*Username:"
send "admin\r"
expect "*Password:"
send "xxxxxxx\r"
expect "*>"
send "en\r"
expect "*Password:"
send "xxxxxxxx\r"

send "copy startup-config tftp:\r"
send "$tftp_server\r"
send "$cip\r"
set timeout 10
send "exit\r"
expect EOF
EOF
echo "$cip" >> $PWD/result
done

mv /var/lib/tftpboot/10.* /var/lib/tftpboot/$back_dir
echo "#### $back_dir cisco-sw backup end  ####" >> $PWD/result
tail -42  result | mailx -v -s "xxxxx-idc sw backup ended | from maboai mail" xxxxxx@163.com,xxxxxx@163.com      #  备份完后发送邮件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息