| 1 |
#!/usr/bin/expect |
| 2 |
# |
| 3 |
# Reboot a machine connected to a Cyclades PM IPDU |
| 4 |
# |
| 5 |
# Copyright 2008 Google Inc., Scott Zawalski <[email protected]> |
| 6 |
# Based heavily off of reboot-sentry by Ryan Kubiak |
| 7 |
# Released under the GPL v2 |
| 8 |
|
| 9 |
set P "reboot-cyclades" |
| 10 |
|
| 11 |
if {[llength $argv] < 5} { |
| 12 |
puts stderr "Usage: $P <ts host> <ts port> <user> <pass> <outlet>" |
| 13 |
exit 1 |
| 14 |
} |
| 15 |
|
| 16 |
#Max number of attempts before stopping |
| 17 |
set max_attempts {5} |
| 18 |
set tshost [lindex $argv 0] |
| 19 |
set tsport [lindex $argv 1] |
| 20 |
set user [lindex $argv 2] |
| 21 |
set pass [lindex $argv 3] |
| 22 |
set outlet [lindex $argv 4] |
| 23 |
set connected {0} |
| 24 |
set attempts {0} |
| 25 |
|
| 26 |
while {$connected == 0 && $attempts < $max_attempts} { |
| 27 |
spawn telnet $tshost $tsport |
| 28 |
sleep 5 |
| 29 |
send "\r" |
| 30 |
set timeout 10 |
| 31 |
set attempts [expr $attempts +1 ] |
| 32 |
expect { |
| 33 |
#Connection closed |
| 34 |
"Connection closed by foreign host." { |
| 35 |
puts "Retrying attempt $attempts" |
| 36 |
} |
| 37 |
#Already logged in |
| 38 |
"pm>" { |
| 39 |
send "cycle $outlet\r" |
| 40 |
expect "Outlet turned off" |
| 41 |
expect "Outlet turned on" |
| 42 |
send "exit\r" |
| 43 |
puts "Machine successfully rebooted." |
| 44 |
exit 0 |
| 45 |
} |
| 46 |
#Login |
| 47 |
"Username" { |
| 48 |
send "$user\r" |
| 49 |
expect "Password:" |
| 50 |
send "$pass\r" |
| 51 |
expect "pm>" |
| 52 |
send "cycle $outlet\r" |
| 53 |
expect "Outlet turned off" |
| 54 |
expect "Outlet turned on" |
| 55 |
send "exit\r" |
| 56 |
puts "Machine successfully rebooted." |
| 57 |
exit 0 |
| 58 |
} |
| 59 |
timeout { |
| 60 |
puts "Timed out connecting." |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
puts "Unable to connect after $attempts connection attempts." |
| 66 |
exit 1 |