| 1 |
#!/usr/bin/expect -- |
| 2 |
# |
| 3 |
# blade -- console and management control for IBM blade servers. |
| 4 |
# |
| 5 |
# Obtain consoles for and hard reset support for blades in |
| 6 |
# IBM blade centres. Blades are identified by their blade centre ids. |
| 7 |
# |
| 8 |
# usage: |
| 9 |
# blade reboot|console <blade id> <user> <password> <connect command> ... |
| 10 |
# |
| 11 |
# example: |
| 12 |
# blade reboot blade[4] FOO BAR telnet 1.2.3.4 |
| 13 |
# blade console blade[4] FOO BAR telnet 1.2.3.4 |
| 14 |
# |
| 15 |
# (C) Copyright IBM Corp. 2004, 2005, 2006 |
| 16 |
# Author: Andy Whitcroft <[email protected]> |
| 17 |
# |
| 18 |
# The Console Multiplexor is released under the GNU Public License V2 |
| 19 |
# |
| 20 |
set P "blade" |
| 21 |
|
| 22 |
if { [llength $argv] < 5 } { |
| 23 |
puts stderr "Usage: $P <command> <blade> <login> <password> <connect command ...>" |
| 24 |
exit 1 |
| 25 |
} |
| 26 |
|
| 27 |
log_user 0 |
| 28 |
|
| 29 |
set cmd [lindex $argv 0] |
| 30 |
set system [lindex $argv 1] |
| 31 |
set username [lindex $argv 2] |
| 32 |
set password [lindex $argv 3] |
| 33 |
set command [lrange $argv 4 end] |
| 34 |
|
| 35 |
# Validate the command. |
| 36 |
switch -- $cmd { |
| 37 |
console {} |
| 38 |
reboot {} |
| 39 |
default { |
| 40 |
puts stderr "$P: $cmd: unrecognised command"; |
| 41 |
exit 1 |
| 42 |
} |
| 43 |
} |
| 44 |
# Ensure the blade name is fully qualified. This ensures that |
| 45 |
# the console prompt detection as reliable as possible. Add the system |
| 46 |
# prefix if it was not specified. |
| 47 |
switch -re -- "$system" { |
| 48 |
system:.* { } |
| 49 |
.* { set system "system:$system" } |
| 50 |
} |
| 51 |
|
| 52 |
#log_file -a "$logfile" |
| 53 |
|
| 54 |
set elapsed_time 0 |
| 55 |
set timeout 30 |
| 56 |
|
| 57 |
proc note {m} { |
| 58 |
global P |
| 59 |
puts "$P: $m" |
| 60 |
} |
| 61 |
proc warn {m} { |
| 62 |
global P |
| 63 |
puts "$P: WARNING: $m" |
| 64 |
} |
| 65 |
proc winge {m} { |
| 66 |
global P |
| 67 |
puts "$P: ERROR: $m" |
| 68 |
} |
| 69 |
|
| 70 |
note "Logging into blade centre with command \"$command\" to restart it"; |
| 71 |
|
| 72 |
# CONNECT: connect to the remote console. |
| 73 |
eval spawn $command |
| 74 |
expect { |
| 75 |
default { |
| 76 |
winge "login prompt not issued" |
| 77 |
exit 2 |
| 78 |
} |
| 79 |
"Connection closed by foreign host." { |
| 80 |
winge "Telnet connection closed." |
| 81 |
exit 1 |
| 82 |
} |
| 83 |
"Unable to connect to remote host:" { |
| 84 |
winge "Connection to remote console failed" |
| 85 |
exit 2 |
| 86 |
} |
| 87 |
"username:" { |
| 88 |
#note "saw login prompt" |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
# AUTHENTICATE: send username and password at the relevant prompts |
| 93 |
note "sending login ..." |
| 94 |
send -- "$username\r" |
| 95 |
expect { |
| 96 |
default { |
| 97 |
winge "password prompt not issued" |
| 98 |
exit 2 |
| 99 |
} |
| 100 |
"password:" { |
| 101 |
#note "password prompt found" |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
note "sending password ..." |
| 106 |
send -- "$password\r" |
| 107 |
expect { |
| 108 |
default { |
| 109 |
winge "command prompt not issued" |
| 110 |
exit 2 |
| 111 |
} |
| 112 |
"Invalid login!" { |
| 113 |
winge "login/password incorrect ... aborting" |
| 114 |
exit 1 |
| 115 |
} |
| 116 |
-- "system>" { |
| 117 |
#note "command prompt found" |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
# SYSTEM: first validate the system specifier, if it does not |
| 122 |
# exist get a listing and print that out whilst we are connected. |
| 123 |
note "selecting system '$system' ..." |
| 124 |
send "env -T $system\r" |
| 125 |
set found 0 |
| 126 |
set prompt "$system>" |
| 127 |
expect { |
| 128 |
default { |
| 129 |
winge "command prompt not issued" |
| 130 |
exit 2 |
| 131 |
} |
| 132 |
OK { |
| 133 |
set found 1 |
| 134 |
exp_continue |
| 135 |
} |
| 136 |
-- "system>" { |
| 137 |
#note "command prompt found" |
| 138 |
} |
| 139 |
-ex $prompt { |
| 140 |
#note "command prompt found" |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
# The system the user specified was not found, give them a hand by |
| 145 |
# getting a list of systems. |
| 146 |
if {$found == 0} { |
| 147 |
note "Defined systems:" |
| 148 |
send "list -l 2\r" |
| 149 |
expect { |
| 150 |
default { |
| 151 |
winge "command prompt not issued" |
| 152 |
exit 2 |
| 153 |
} |
| 154 |
-- "system>" { |
| 155 |
#note "command prompt found" |
| 156 |
} |
| 157 |
"\n$" { |
| 158 |
puts -nonewline "$expect_out(buffer)" |
| 159 |
exp_continue |
| 160 |
} |
| 161 |
} |
| 162 |
note "complete ... exiting" |
| 163 |
send "exit\r" |
| 164 |
exit 1 |
| 165 |
} |
| 166 |
|
| 167 |
# CONSOLE: open a console channel |
| 168 |
if {[string compare $cmd "console"] == 0} { |
| 169 |
set ts_start 1 |
| 170 |
set ts_cur 1 |
| 171 |
set retry 0; |
| 172 |
note "connecting to console ..." |
| 173 |
|
| 174 |
# Unless console session lasts for more than two seconds, retry every |
| 175 |
# thirty seconds for up to a day. This prevents repeated login events |
| 176 |
# which fill logs and cause loss of useful event log information. |
| 177 |
while {($ts_cur - $ts_start < 2) && ($retry < 2880)} { |
| 178 |
# 30 second delay between attempts, except for the first one |
| 179 |
if {$ts_start != 1} { |
| 180 |
note "connection failed, retrying after 30 second delay" |
| 181 |
after 30000 |
| 182 |
} |
| 183 |
set ts_start [timestamp]; |
| 184 |
send "console -o\r" |
| 185 |
interact { |
| 186 |
-o -ex $prompt { |
| 187 |
return |
| 188 |
} |
| 189 |
} |
| 190 |
set ts_cur [timestamp]; |
| 191 |
incr retry |
| 192 |
} |
| 193 |
winge "console lost" |
| 194 |
exit 0 |
| 195 |
} |
| 196 |
|
| 197 |
# Function to wait until the issued power command has taken effect. |
| 198 |
proc powerWait {s} { |
| 199 |
global prompt |
| 200 |
set ok 0 |
| 201 |
for {set retry 1} {$ok == 0 && $retry < 30} {incr retry} { |
| 202 |
note "checking for power state $s ..." |
| 203 |
after 2000 |
| 204 |
send "power -state\r" |
| 205 |
expect { |
| 206 |
default { |
| 207 |
winge "command prompt not issued" |
| 208 |
exit 2 |
| 209 |
} |
| 210 |
-ex $s { |
| 211 |
set ok 1 |
| 212 |
exp_continue |
| 213 |
} |
| 214 |
-ex "$prompt" { |
| 215 |
#note "command prompt found" |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
if {$ok == 0} { |
| 220 |
winge "system did not enter power $s state ... aborting" |
| 221 |
exit 2 |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
# DOWN: shut the system down ... hard. Expect to OK before the |
| 226 |
# prompt in the case of success. |
| 227 |
note "powering cycling the system ..." |
| 228 |
send "power -cycle\r" |
| 229 |
set fail 1 |
| 230 |
expect { |
| 231 |
default { |
| 232 |
winge "command prompt not issued" |
| 233 |
exit 2 |
| 234 |
} |
| 235 |
"OK" { |
| 236 |
set fail 0 |
| 237 |
exp_continue |
| 238 |
} |
| 239 |
-ex "$prompt" { |
| 240 |
#note "command prompt found" |
| 241 |
} |
| 242 |
} |
| 243 |
if {$fail == 1} { |
| 244 |
winge "power cycle failed ... system not rebooted" |
| 245 |
exit 2 |
| 246 |
} |
| 247 |
powerWait On |
| 248 |
|
| 249 |
note "complete ... exiting" |
| 250 |
send "exit" |
| 251 |
exit 0 |