| 1 |
#!/usr/bin/expect |
| 2 |
# |
| 3 |
# Reboot a machine connected to a Servertech Sentry power strip |
| 4 |
# |
| 5 |
# Copyright 2008 Google Inc., Ryan Kubiak <[email protected]> |
| 6 |
# Released under the GPL v2 |
| 7 |
|
| 8 |
set P "reboot-sentry" |
| 9 |
|
| 10 |
if {[llength $argv] < 3} { |
| 11 |
puts stderr "Usage: $P <ts host> <ts port> <outlet>" |
| 12 |
exit 1 |
| 13 |
} |
| 14 |
|
| 15 |
set user {admn} |
| 16 |
set pass {admn} |
| 17 |
set tshost [lindex $argv 0] |
| 18 |
set tsport [lindex $argv 1] |
| 19 |
set outlet [lindex $argv 2] |
| 20 |
set connected {0} |
| 21 |
set attempts {0} |
| 22 |
|
| 23 |
while {$connected == 0 && $attempts < 10} { |
| 24 |
spawn telnet $tshost $tsport |
| 25 |
sleep 5 |
| 26 |
send "\r" |
| 27 |
set timeout 10 |
| 28 |
set attempts [ expr $attempts +1 ] |
| 29 |
expect { |
| 30 |
#Connection closed |
| 31 |
"Connection closed by foreign host." { |
| 32 |
puts "Retrying attempt $attempts" |
| 33 |
set rand [expr round(rand() * 15 + 6)] |
| 34 |
puts "Waiting $rand before next attempt" |
| 35 |
sleep $rand |
| 36 |
} |
| 37 |
#Already logged in |
| 38 |
"Switched CDU:" { |
| 39 |
set connected {1} |
| 40 |
send "reboot $outlet\r" |
| 41 |
expect "Command successful" |
| 42 |
send "logout\r" |
| 43 |
puts "Machine successfully rebooted." |
| 44 |
exit 0 |
| 45 |
} |
| 46 |
#Login |
| 47 |
"Username" { |
| 48 |
set connected {1} |
| 49 |
send "$user\r" |
| 50 |
expect "Password:" |
| 51 |
send "$pass\r" |
| 52 |
expect "Switched CD:" |
| 53 |
send "reboot $outlet\r" |
| 54 |
expect "Command successful" |
| 55 |
send "logout\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 10 connection attempts." |
| 66 |
exit 1 |