有华为2300、5700系列,H3C 3100、5120、5500系列交换,还有神州数码4500的交换机,需进行批量备份,各交换机有两种密码,不是passwd1就是passwd2,利用linux shell和expect写了个脚本,交换机在不同网段,先进行批量ping扫描ip地址再进备份,备份时先进行保存再tftp上传。
安装expect见http://hiid365.blog.51cto.com/3142060/1350800
#!/bin/bash
TFTP=$1
PASSWD1=$2
PASSWD2=$3
IP=$4
if [ "$#" -ne 4 ];then
echo "for example:./backup.sh tftp_ip ‘passwd1‘ ‘passwd2‘ 10.255.251"
fi
if [ -f ./$IP.xx ];then
rm -f $IP.xx
fi
if [ -f ./$IP_error.txt ];then
rm -f $IP.error
fi
echo "******************Scanning Start******************"
for n in {2..254}
do
host=$IP.$n
ping -W 1 -c 1 $host &> /dev/null && echo "$host is up!"&& echo "$host">>$IP.xx
done
echo "******************The scan is complete******************"
sleep 3
echo "******************Start backup******************"
echo "You can click Ctrl+C skip when backup failed!!!"
sleep 5
while read SWIP;do
./back.exp $TFTP $PASSWD1 $PASSWD2 $SWIP
if [ "$?" -ne 0 ];then
echo "$SWIP" >>$IP.error
fi
done<$IP.xx
echo "*******************Backup Complete*********************"
echo "==========The following IP backup failed=========="
cat $IP.error
echo "=================================================="expect代码关键:
#!/usr/sbin/expect
set timeout 35
set TFTP [lindex $argv 0]
set PASSWD1 [ lindex $argv 1]
set PASSWD2 [ lindex $argv 2]
set IP [lindex $argv 3]
set done 0
spawn telnet $IP
expect {
"Password:" {
send "$PASSWD1\r"
expect ">" {send "display version\r"}
}
"login:" {
set done 1
send "admin\r"
expect "Password:" {send "$PASSWD1\r"}
expect "#" {send "write\r"}
expect "Y/N" {send "y\r"}
expect "successful" {send "copy startup.cfg tftp://$TFTP/$IP.cfg\r"}
expect "Y/N" {send "y\r"}
expect "close" {send "quit\r"}
}
}
if {$done==1} {
exit 0
} else {
expect {
"Error:" {send "$PASSWD2\r";expect ">" {send "display version\r"}}
"Wrong" {send "$PASSWD2\r";expect ">" {send "display version\r"}}
">" {send "display version\r"}
}
expect {
"S2300" {
send "save\r"
expect "Y/N" {send "y\r"}
expect "successfully" {send "tftp $TFTP put vrpcfg.zip $IP.zip\r"}
expect {
"successfully." {send "quit\r"}
"Error:" {exit 1}
}
}
"S5700" {
send "save\r"
expect "Y/N" {send "y\r"}
expect "successfully" {send "tftp $TFTP put vrpcfg.zip $IP.zip\r"}
expect {
"successfully." {send "quit\r"}
"Error:" {exit 1}
}
}
"S5120" {
send "save\r"
expect "Y/N" {send "y\r"}
expect "key):" {send "\r"}
expect "Y/N" {send "y\r"}
expect "successfully" {send "tftp $TFTP put startup.cfg $IP.cfg\r"}
expect {
"successfully." {send "quit\r"}
"Error:" {exit 1}
}
}
"S5500" {
send "save\r"
expect "Y/N" {send "y\r"}
expect "key):" {send "\r"}
expect "Y/N" {send "y\r"}
expect "successfully." {send "tftp $TFTP put startup.cfg $IP.cfg\r"}
expect {
"successfully." {send "quit\r"}
"Error:" {exit 1}
}
}
"S3100" {
send "save\r"
expect "Y/N" {send "y\r"}
expect "key):" {send "\r"}
expect "successfully." {send "tftp $TFTP put config.cfg $IP.cfg\r"}
expect {
"successfully." {send "quit\r"}
"Unable" {exit 1}
}
}
}
#expect "
#expect "Y/N" interact
expect eof
}在终端运行
#./back.sh tftp服务器ip地址 ‘passwd1‘ ‘passwd2‘ 需要备份的网段(如192.168.1)
例外一种同种型号交换机批量备份的shell脚本
#!/bin/bash
TFTP=$1
PASSWD=$2
while read swip;do
(
echo $PASSWD;
echo "save";
echo "y";
sleep 18;
echo "tftp $TFTP put vrpcfg.zip $swip.zip";
sleep 5;
echo "quit";
sleep 1;
)| telnet $swip
done<$3终端执行#./back.sh tftp_ip地址 ‘密码’ ip备份表
本文出自 “hiid365” 博客,请务必保留此出处http://hiid365.blog.51cto.com/3142060/1357279
原文:http://hiid365.blog.51cto.com/3142060/1357279