2013年3月5日 星期二

使用 Expect 備份網路設備設定檔 for HP


支援 HP ProCurve Switch 2610系列、2510系列

#!/usr/bin/expect -f

if { $argc != 4 } {
        send_user "usage: hp.exp device_ip username password server_ip \n"
        exit
}

set timeout 1
set TERM ANSI
set date [exec date +%Y%m%d]

set DEVICE [lindex $argv 0]
set USERNAME [lindex $argv 1]
set PASSWD [lindex $argv 2]
set SERVER [lindex $argv 3]


spawn telnet
expect "telnet> "
send "open $DEVICE\r"
sleep 1
expect continue { send "\r" ; exp_continue }
sleep 1
expect "Username:"
send "$USERNAME\r"
sleep 1
expect "Password:"
send "$PASSWD\r"



sleep 1
send "\r\r"
expect "#"
send "copy running-config tftp $SERVER $DEVICE-$date\r"
sleep 1
expect "#"
send "exit\r"
sleep 1
expect ">"
send "exit\r"
sleep 1
expect "Do you want to log out"
send "y\r"
interact



沒有留言: