接前篇:
一个企业中交换机肯定有很多个,如果手工执行第一篇文章中的脚本得花很多时间,这肯定还是没达到标题要求
原本思路:直接是打算直接在exp中修改,加个读取文件并循环执行的功能,但水平有限,一直没调试成功,后来就干脆另外写了个bash shell,再其中调用第一篇中的脚本,这样就简单很多了,
具体脚本代码如下
cat baksw.sh
- #!/usr/bin/env bash
- #file name: baksw.sh
- #Usages: auto backup switch configure
- #Version: v1.1
- #By Create Edward.zhou
- #Date: 2012-07-14
- cd /backup/shell
- date=`date +%Y%m%d%I%M`
- swbakdir=/backup/cisco
- for eachline in `cat sw.txt`;do
- ./baksw-1.2.exp ****** ***** $eachline 192.168.103.71 2>&1 > /dev/null
- done
- ./baksw-1.2.exp ****** ***** 10.1.11.237 192.168.103.71 2>&1 >/dev/null
- ./baksw-1.2.exp ****** ***** 10.1.11.238 192.168.103.71 2>&1 >/dev/null
- if [ ! -d $swbakdir ]; then
- mkdir -p $swbakdir
- fi
- tar -cjPf $swbakdir/switch-$date.tar.bz2 /tftproot
- rm -fr /tftproot/*
cat baksw-1.2.exp
- #!/usr/bin/expect -f
- #file name: baksw.exp
- #Usages: auto backup switch configure
- #Version: v1.2
- #By Create Edward.zhou
- #Date: 2012-07-14
- set timeout -1
- if { $argc !=4 } {
- puts "Usage $argv0 passwd enablepasswd switchhost tftpserver"
- exit 1
- }
- set passwd [lindex $argv 0]
- set enapwd [lindex $argv 1]
- set swhost [lindex $argv 2]
- set tftpsr [lindex $argv 3]
- spawn telnet $swhost
- expect "Password:"
- send "$passwd\r"
- expect "*>"
- send "enable\r"
- expect "Password:"
- send "$enapwd\r"
- expect "*#"
- send "copy startup-config tftp:\r"
- expect "*]?"
- send "$tftpsr\r"
- expect "*]?"
- send "\r"
- send "exit\r"
- interact
其中 sw.txt 文件中就是需要备份的交换机的IP管理地址,每行一个IP地址即可
阅读(2707) | 评论(0) | 转发(0) |