建立执行脚本
vi /home/Network_Config_Backup/start.sh
#!/bin/bash #使用该脚本前需要安装tftp,xinetd,expect #取时间戳 BACKUP_DATA=`date +%Y%m%d` #创建时间戳的备份目录 mkdir -p /home/Network_Config_Backup/$BACKUP_DATA #TFTP服务根目录授权 chmod 777 /home/Network_Config_Backup/$BACKUP_DATA log=/data/log/start_${BACKUP_DATA}.log #修改TFTP根目录 sed -i "s/Network_Config_Backup\/......../Network_Config_Backup\/$BACKUP_DATA/" /etc/xinetd.d/tftp echo echo echo echo ------------------------------------------------------------ echo echo 该脚本适用于Cisco、Huawei、H3C 网络设备使用 echo Cisco使用tftp方式将配置文件上传到服务器 echo Huawei、H3C使用窗口记录方式获取配置信息 echo echo ------------------------------------------------------------ echo echo 备份文件存放位置: /home/Network_Config_Backup/$BACKUP_DATA echo echo echo systemctl restart xinetd sleep 4 #账号密码及IP地址存放在user.passwd中,通过awk获取所有IP并存放在数组IPADDR(大写) DEVICENAME=(`cat /home/Network_Config_Backup/user.passwd | awk '{print $1}'`) #for循环,取出所有设备名称devicename(小写) for devicename in ${DEVICENAME[@]} do #输出备份开始时间戳 echo "`date +%H:%M:%S` 开始备份$devicename" #根据设备名称devicename取出相应交换机的IP地址、用户、密码 IPADDR=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $3}'`) echo ${IPADDR} USERNAME=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $4}'`) echo ${USERNAME} PASSWORD=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $5}'`) echo ${PASSWORD} TABLE=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $2}'`) echo ${TABLE} devicename=${devicename}_${BACKUP_DATA} if [ $TABLE = Cisco ] then /usr/bin/expect >> /home/Network_Config_Backup/$BACKUP_DATA/log << EOF set timeout 10 spawn telnet $IPADDR expect "*sername:" send "$USERNAME\r" expect "*assword:" send "$PASSWORD\r" expect { "*>" { send "enable\r" expect "*assword:" send "$PASSWORD\r" expect "*#" send "copy running-config tftp:\r" expect "*remote host*" send "tftp服务器IP\r" expect "*filename*" send "$devicename\n" expect "*!*" send "\n" } "*#" { send "copy running-config tftp:\r" expect "*remote host*" send "tftp服务器IP\r"
expect "*filename*" send "$devicename\n" expect "*!*" send "\n" } } EOF elif [ $TABLE = Huawei -o H3C ] then /usr/bin/expect >> /dev/null << EOF set timeout 100 log_file /data2/SW/$devicename spawn ssh ${USERNAME}@$IPADDR expect "*assword:" send "$PASSWORD\r" expect "*>" send "display current-configuration\r" while (1) { expect { "*--- More ----" { send " " } "return" { break } } } EOF fi sleep 3 #判断备份目录中是否存在$devicename,如果存在则检查文件内是否存在字符串end或者return。如果文件不存在视为备份失败,如果不存在end或者return视为备份失败 if [ -f "/data2/SW/$devicename" ];then grep -E "^end$|^return$" /data2/SW/$devicename if [ $? -eq 0 ];then echo "$devicename $IPADDR 备份成功" echo "$devicename $IPADDR 备份成功" >> /home/Network_Config_Backup/$BACKUP_DATA/log echo "-------------------------------" else echo "$devicename $IPADDR 备份失败" echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/log echo "-------------------------------" fi else echo "$devicename $IPADDR 备份失败" echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/log echo "-------------------------------" fi done
chmod 777 /home/Network_Config_Backup/start.sh
新建user.passwd存放设备信息
vi /home/Network_Config_Backup/user.passwd
*设备名 品牌 IP地址 用户名 密码*
Huawei-TEST-SW01 Huawei 192.168.1.55 admin admin12345 Cisco-TEST-SW01 Cisco 192.168.1.66 admin admin12345 |
cd /home/Network_Config_Backup
sh start.sh //执行脚本即可自动备份
修改tftp文件储存位置
vi /etc/xinetd.d/tftp service tftp { socket_type = dgram protocol = udp wait = yes user = root server = /usr/sbin/in.tftpd server_args = -s /data2/SW -c disable = no per_source = 11 cps = 100 2 flags = IPv4 }
以上出自Cisco、华为、H3C交换机配置文件自动备份_fsk2684的博客-CSDN博客
由于环境不同可更改部分内容例如存储位置
远程连接方式【telnet,ssh】
#!/bin/bash#使用该脚本前需要安装tftp,xinetd,expect#取时间戳BACKUP_DATA=`date +%Y%m%d`#创建时间戳的备份目录mkdir -p /home/Network_Config_Backup/$BACKUP_DATA
#TFTP服务根目录授权chmod 777 /home/Network_Config_Backup/$BACKUP_DATA
log=/data/log/start_${BACKUP_DATA}.log#修改TFTP根目录sed -i "s/Network_Config_Backup\/......../Network_Config_Backup\/$BACKUP_DATA/" /etc/xinetd.d/tftpechoechoechoecho ------------------------------------------------------------echo echo 该脚本适用于Cisco、Huawei、H3C 网络设备使用 echo Cisco使用tftp方式将配置文件上传到服务器 echo Huawei、H3C使用窗口记录方式获取配置信息 echo echo ------------------------------------------------------------echoecho 备份文件存放位置: /home/Network_Config_Backup/$BACKUP_DATAechoechoechosystemctl restart xinetdsleep 4
#账号密码及IP地址存放在user.passwd中,通过awk获取所有IP并存放在数组IPADDR(大写)DEVICENAME=(`cat /home/Network_Config_Backup/user.passwd | awk '{print $1}'`)
#for循环,取出所有设备名称devicename(小写)for devicename in ${DEVICENAME[@]} do#输出备份开始时间戳echo "`date +%H:%M:%S` 开始备份$devicename"
#根据设备名称devicename取出相应交换机的IP地址、用户、密码IPADDR=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $3}'`)echo ${IPADDR}USERNAME=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $4}'`)echo ${USERNAME}PASSWORD=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $5}'`)echo ${PASSWORD}TABLE=(`cat /home/Network_Config_Backup/user.passwd | grep "$devicename" | awk '{print $2}'`)echo ${TABLE}devicename=${devicename}_${BACKUP_DATA}if [ $TABLE = Cisco ]then/usr/bin/expect >> /home/Network_Config_Backup/$BACKUP_DATA/log << EOFset timeout 10spawn telnet $IPADDRexpect "*sername:"send "$USERNAME\r"expect "*assword:"send "$PASSWORD\r"expect {"*>" {send "enable\r"expect "*assword:"send "$PASSWORD\r"expect "*#"send "copy running-config tftp:\r"expect "*remote host*"send "10.20.5.5\r"expect "*filename*"send "$devicename\n"expect "*!*"send "\n"}"*#" {send "copy running-config tftp:\r"expect "*remote host*"send "10.20.5.5\r"expect "*filename*"send "$devicename\n"expect "*!*"send "\n"}}EOFelif [ $TABLE = Huawei -o H3C ]then/usr/bin/expect >> /dev/null << EOFset timeout 100log_file /data2/SW/$devicenamespawn ssh ${USERNAME}@$IPADDRexpect "*assword:"send "$PASSWORD\r"expect "*>"send "display current-configuration\r"while (1) {expect {"*--- More ----" { send " " }"return" { break }}}EOFfisleep 3
#判断备份目录中是否存在$devicename,如果存在则检查文件内是否存在字符串end或者return。如果文件不存在视为备份失败,如果不存在end或者return视为备份失败if [ -f "/data2/SW/$devicename" ];thengrep -E "^end$|^return$" /data2/SW/$devicenameif [ $? -eq 0 ];thenecho "$devicename $IPADDR 备份成功"echo "$devicename $IPADDR 备份成功" >> /home/Network_Config_Backup/$BACKUP_DATA/logecho "-------------------------------"elseecho "$devicename $IPADDR 备份失败"echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/logecho "-------------------------------"fielseecho "$devicename $IPADDR 备份失败"echo "$devicename $IPADDR 备份失败" >> /home/Network_Config_Backup/$BACKUP_DATA/logecho "-------------------------------"fi
done 标签:脚本,devicename,Network,Backup,备份,echo,home,定时,Config From: https://www.cnblogs.com/ctdd/p/16903181.html