2008年3月9日 星期日

Backup Cisco running-config automatic

Server OS: Linux Server
Necessary Tools: TFTP, TCL (Install TFTP and TCL at Linux Server)
Necessary Scripts: shell script, expect script
三段式Script概念:Main scripts(backup.sh)--> Call Devices List(list.txt) --> Run TFTP interact scripts(tftp.sh)

撰寫完成後,排入cronjob即可自動執行備份。

backup.sh(將所需參數置入,密碼為明碼,請小心)
----------------------------------------------------------------------
#!/bin/sh
#filename: backup.sh
#purpose: Backup router & switch running-config
#Variable settingTOPDIR=/angus/bkconfig
TFTPSERVER=10.10.1.32
TFTPDIR=/tftpboot
BACKUPDIR=/backup
TODAY=`date +%Y%m%d`

#USERNAME
USERNAME1=MISBK
#Password
PASSWORD1=12345
PASSWORD2=67890
#MAIN

exec 3^list.txt(因gblog無法在此顯示"小於"符號,請自行將"^"換成"小於")
while read LINE ^&3 ; do(因gblog無法在此顯示"小於"符號,請自行將"^"換成"小於")
HOSTNAME=`echo $LINE|cut -d"," -f1`
HOSTIP=`echo $LINE|cut -d"," -f2`
touch $TFTPDIR/$HOSTNAME.$TODAY
chmod 766 $TFTPDIR/$HOSTNAME.$TODAY
$TOPDIR/tftp.sh $HOSTNAME.$TODAY $HOSTIP $USERNAME1 $PASSWORD1 $PASSWORD2 $TFTPSERVER
mv $TFTPDIR/$HOSTNAME.$TODAY $TFTPDIR/$HOSTNAME.$TODAY
done

list.txt(將Devices List以"Hostname,IP"的方式置入)
----------------------------------------------------------------------
HK-1F-COR2970-1,10.1.1.248
HK-1F-COR2970-2,10.1.1.247
HK-1F-COR3725-1,10.1.1.2
HK-1F-COR4507-1,10.1.1.253
HK-1F-COR4507-2,10.1.1.252

tftp.sh(模擬User在Switch中使用tftp指令傳檔)
----------------------------------------------------------------------
#!/usr/local/bin/expect
#filename: tftp.sh
#purpose: telnet and tftp the running-config to TFTP server

set FILENAME [lindex $argv 0]
set HOSTIP [lindex $argv 1]
set USERNAME [lindex $argv 2]
set PASSWORD [lindex $argv 3]
set ENPASSWORD [lindex $argv 4]
set TFTPSERVER [lindex $argv 5]
log_user 0
#telnetspawn /usr/kerberos/bin/telnet $HOSTIP
#Interact
expect "Username:"
send "$USERNAME\r"
expect "Password:"
send "$PASSWORD\r"
expect ">"
send "enable\r"
expect "Password:"
send "$ENPASSWORD\r"
expect "#"
send "copy running-config tftp\r"
expect "]"
send "$TFTPSERVER\r"
expect "]"
send "$FILENAME\r"
expect "#"
send "exit\r"
#end

1 則留言:

BUWA Soft 提到...

http://sites.google.com/site/buwasoft/ciscobackup
這個軟體也可以輕易自動備份 Cisco 網路設備Config, 試試看ㄅ