天天看点

用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