#!/bin/sh # \ exec expect -f "$0" ${1+"$@"} ###################################################################### # power-toggle December 2005 # Horms horms@verge.net.au # # power-toggle # Change the power states on an APC Rack PDU APP # Copyright (C) 2005 Horms # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA # 02111-1307 USA # ###################################################################### proc out {} { global tmpfile log_file log_user 1 set fl [open $tmpfile r] puts "=== Begin Error Log ===" puts [read $fl] puts "==== End Error Log ====" close $fl file delete $tmpfile puts "ERROR: Unexpected data\n" exit 1 } proc usage {} { global argv0 puts "Usage: $argv0 HOST USERNAME PASSWORD on|off|reset|status" puts " e.g. $argv0 172.17.100.1 9 9 on" exit 1 } proc cmd {hostname username password action} { while (1) { spawn telnet $hostname expect { "User Name :" { break } "usage: telnet" { out } timeout { out } eof { } } puts "waiting for another session to end..." sleep 1 } if {[catch {send "$username\n\r"}] != 0} { send_log "ERROR: Could not open connection\n" out } expect { "Password : " { } default { out } } send "$password\n\r" expect { -re "Control Console.*\n> " {} "User Name : " { out } default { out } } send "1\n\r" expect { -re "Device Manager.*\n> " {} default { out } } send "2\n\r" expect { -re "Outlet Control.*\n> " {} default { out } } send "1\n\r" expect { -re "State *: (\[A-Z\]*).*\n> " {} default { out } } set state $expect_out(1,string) switch $action { flip { if { $state == "OFF" } { puts "Turning on..." send "1\n\r" } else { puts "Turning off..." send "2\n\r" } } on { if { $state == "ON" } { puts "Already on..." return $state } puts "Turning on..." send "1\n\r" } off { if { $state == "OFF" } { puts "Already off..." return $state } puts "Turning off..." send "2\n\r" } status { return $state } } expect { -re " Immediate.*\n.*to cancel : " {} default {out} } send "YES\n\r" expect { "Press to continue..." {} default {out} } send "\n\r" expect { -re "State *: (\[A-Z\]*).*\n> " {} default {out} } close return $expect_out(1,string) } if {$argc != 4 } { usage } set hostname [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] set action [lindex $argv 3] trap out {SIGINT SIGTERM} set tmpfile [exec mktemp] log_user 0 log_file -a $tmpfile set state "UNKNOWN" switch $action { on { cmd $hostname $username $password on } off { cmd $hostname $username $password off } reset { set state [ cmd $hostname $username $password flip ] if { $state == "OFF" } { puts "Sleeping for 10s..." sleep 10 cmd $hostname $username $password on } } status { set state [ cmd $hostname $username $password status ] puts "STATE: $state" if { $state != "ON" } { file delete $tmpfile exit 1 } } default { puts "ERROR: invalid action: $action" usage } } file delete $tmpfile exit 0