天天看點

用expect實作apc互動式循環啟動和關閉

公司花幾千元買了一個apc,但是apc隻能實作單次開關機動作,不能實作循環動作。是以有需要通過其它方式實作自動重新開機;

下面是一個簡單的實作腳本,主要是先登陸telnet,然後對apc進行操作,實際中apc每步都會給出提示,根據動作前的編号進行選擇即可實作,是以選擇使用expect

本來想用python寫一個windows下的登陸程式,結果python在windows下暫不支援expect,失望呀!

#!/usr/bin/expect -f


set i 1 

spawn echo "######running to telnet apc######"

spawn telnet

expect " telnet>"

send "o 168.192.0.1\r"

expect "User Name :"

send "apc\r"

expect "Password :"

send "apc\r"

expect ">"

send "1\r"

expect ">"

send "2\r"

expect ">"

send "1\r"

expect ">"

send "9\r"

expect ">"

send "1\r"

while {$i<5} {            #循環次數

    expect ">"

    send "2\r"

    expect "Enter 'YES' to continue or <ENTER> to cancel :"

    send "yes\r"

    sleep 15 

    expect " Press <ENTER> to continue..."

    send "\r"

    expect ">"

    send "1\r"

    expect "Enter 'YES' to continue or <ENTER> to cancel :"

    send "yes\r"

    sleep 15 

    expect " Press <ENTER> to continue..."

    send "\r"

    incr i #自加

 }



set timeout -1 

expect eof



exit