Skip to content

Instantly share code, notes, and snippets.

@awaxa
Last active December 18, 2019 07:16
Show Gist options
  • Save awaxa/d3c3e1bb9902f21010ab to your computer and use it in GitHub Desktop.
Save awaxa/d3c3e1bb9902f21010ab to your computer and use it in GitHub Desktop.
apc pdu outlet expect script
#!/usr/bin/expect
set timeout 3
if {[llength $argv] == 0} {
send_user "Usage: outlet [1-8] [on|off] \n"
exit 1
}
set number [lindex $argv 0]
set onoff [lindex $argv 1]
set offstring "off"
set state "1"
if { [string compare $onoff $offstring] == 0 } {
set state "2"
}
spawn telnet pdu
expect "User Name :" { send "apc\r" }
expect "Password :" { send "apc\r" }
# device manager
expect "> " { send "1\r" }
# outlet management
expect "> " { send "2\r" }
# outlet control
expect "> " { send "1\r" }
# outlet number
expect "> " { send "$number\r" }
# control outlet
expect "> " { send "1\r" }
# state
expect "> " { send "$state\r" }
# control outlet
expect "cancel : " { send "YES\r" }
expect "..." { send "\r" }
expect "> " { send \033 }
expect "> " { send \033 }
expect "> " { send \033 }
expect "> " { send \033 }
expect "> " { send \033 }
expect "> " { send "4\r" }
@nickmoody
Copy link

Thank you for sharing this script, you saved me a lot of time :-)

@theodric
Copy link

Hella good.

@lpere
Copy link

lpere commented Dec 14, 2017

I think it is simpler to log in with "PASS -c" password for turn on and off the outlets. I made a simplified version, but I may have messed up the argument order:

#!/usr/bin/expect

set timeout 3

if {[llength $argv] == 0} {
send_user "Usage: outlet [1-8] [on|off] \n"
exit 1
}

set onoff [lindex $argv 0]
set number [lindex $argv 1]
set offstring "off"
set state "1"
if { [string compare $onoff $offstring] == 0 } {
set state "2"
}

spawn telnet 192.168.1.30

expect "User Name :" { send "apc\r" }
expect "Password :" { send "apc -c\r" }

expect "APC> " { send "$onoff $number\r" }
expect "APC> " { send "\r" }

Thanks for the script, it was a great help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment