不同型号 交换机批量备份脚本 expect

有华为2300、5700系列,H3C 3100、5120、5500系列交换,还有神州数码4500的交换机,需进行批量备份,,各交换机有两种密码,不是passwd1就是passwd2,利用linux shell和expect写了个脚本,交换机在不同网段,先进行批量ping扫描ip地址再进备份,备份时先进行保存再tftp上传。

安装expect见

#!/bin/bashTFTP=$1PASSWD1=$2PASSWD2=$3IP=$4if [ “$#” -ne 4 ];thenecho “for example:./backup.sh tftp_ip ‘passwd1’ ‘passwd2’ 10.255.251″fi if [ -f ./$IP.xx ];thenrm -f $IP.xxfiif [ -f ./$IP_error.txt ];thenrm -f $IP.errorfiecho “******************Scanning Start******************”for n in {2..254}dohost=$IP.$nping -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 $SWIPif [ “$?” -ne 0 ];thenecho “$SWIP” >>$IP.errorfidone<$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 1send “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” 博客,请务必保留此出处

福报够的人,从来就没听到过是非。

不同型号 交换机批量备份脚本 expect

相关文章:

你感兴趣的文章:

标签云: