有華為2300、5700系列,H3C 3100、5120、5500系列交換,還有神州數位4500的交換器,需進行批量備份,各交換器有兩種密碼,不是passwd1就是passwd2,利用linux shell和expect寫了個指令碼,交換器在不同網段,先進行批量ping掃描ip地址再進備份,備份時先進行儲存再tftp上傳。
安裝expect見http://hiid365.blog.51cto.com/3142060/1350800
#!/bin/bashTFTP=$1PASSWD1=$2PASSWD2=$3IP=$4if [ "$#" -ne 4 ];then echo "for example:./backup.sh tftp_ip 'passwd1' 'passwd2' 10.255.251"fi if [ -f ./$IP.xx ];then rm -f $IP.xxfiif [ -f ./$IP_error.txt ];then rm -f $IP.errorfiecho "******************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.xxdoneecho "******************The scan is complete******************"sleep 3echo "******************Start backup******************"echo "You can click Ctrl+C skip when backup failed!!!"sleep 5while read SWIP;do ./back.exp $TFTP $PASSWD1 $PASSWD2 $SWIP if [ "$?" -ne 0 ];then echo "$SWIP" >>$IP.error fidone<$IP.xxecho "*******************Backup Complete*********************"echo "==========The following IP backup failed==========" cat $IP.errorecho "=================================================="
expect代碼關鍵:
#!/usr/sbin/expectset timeout 35set TFTP [lindex $argv 0]set PASSWD1 [ lindex $argv 1]set PASSWD2 [ lindex $argv 2]set IP [lindex $argv 3]set done 0spawn telnet $IPexpect { "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" interactexpect eof}
在終端運行
#./back.sh tftp伺服器ip地址 'passwd1' 'passwd2' 需要備份的網段(如192.168.1)
例外一種同種型號交換器批量備份的shell指令碼
#!/bin/bashTFTP=$1PASSWD=$2while 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 $swipdone<$3
終端執行
#./back.sh tftp_ip地址 ‘密碼’ ip備份表
本文出自 “hiid365” 部落格,請務必保留此出處http://hiid365.blog.51cto.com/3142060/1357279