File: ibm-bc.exp

package info (click to toggle)
conman 0.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 1,424 kB
  • sloc: ansic: 11,173; sh: 4,072; exp: 2,132; makefile: 153; perl: 122
file content (311 lines) | stat: -rwxr-xr-x 9,028 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/expect -f
###############################################################################
# $Id: ibm-bc.exp 1003 2011-01-26 18:26:06Z chris.m.dunlap $
###############################################################################
# Written by Chris Dunlap <cdunlap@llnl.gov>.
# Copyright (C) 2007-2011 Lawrence Livermore National Security, LLC.
# Copyright (C) 2001-2007 The Regents of the University of California.
# UCRL-CODE-2002-009.
#
# This file is part of ConMan: The Console Manager.
# For details, see <http://conman.googlecode.com/>.
###############################################################################
# This script connects to a console on a given blade in an IBM BladeCenter
# via Serial Over LAN (SOL) using the telnet protocol.
#
# This script can be specified in "conman.conf" in the following manner:
#
#   console name="zot" dev="/path/to/ibm-bc.exp HOST BLADE USER PSWD"
#
# HOST  is the hostname of the blade server.
# BLADE is the blade number associated with the console.
# USER  is the username being authenticated.
# PSWD  is the corresponding password.
#
# Since this command-line will persist in the process listing for the duration
# of the connection, passing sensitive information like PSWD in this manner is
# not recommended.  Instead, consider using either a command-line argument
# default or the password database (see below).
###############################################################################

##
# Set "exp_internal" to 1 to print diagnostics describing internal operations.
#   This is helpful in diagnosing pattern-match failures.
##
  exp_internal 0

##
# Set "log_user" to 1 to show the underlying dialogue establishing a connection
#   to the console.
##
  log_user 0

##
# The "timeout" specifies the number of seconds before the connection attempt
#   times-out and terminates the connection.
##
  set timeout 10

##
# If "bcmm_timeout" is set greater than or equal to 0, the telnet inactivity
#   timeout of the BladeCenter management module will be set to this value.
# This setting specifies the number of seconds of inactivity before the
#   interface session times-out; setting it to 0 disables the timeout.
##
  set bcmm_timeout 0

##
# If "idle_timeout" is set greater than 0, a "space-backspace" sequence will
#   be sent every ${idle_timeout} seconds after no output activity has been
#   detected in order to keep the connection alive.
# This setting should ideally be the same value as "bcmm_timeout".
##
  set idle_timeout 0

##
# If "session_override" is enabled, an existing connection to this console
#   session will be terminated and the new connection will be established.
#   Subsequent attempts to steal this console session will be thwarted.
# Otherwise, an existing connection to this console session will cause the new
#   connection to fail with "console session already in use".  If the console
#   session is not in use and the connection succeeds, the console session may
#   subsequently be stolen thereby causing this connection to terminate with
#   "console session stolen".
# Beware of dueling banjos if a given console session is overridden by more
#   than one process, as these processes will continuously steal the console
#   session from each other.
##
  set session_override 1

##
# The "password_db" specifies the location of the password database.
#   This avoids exposing sensitive information on the command-line without
#   needing to modify this script.
# Whitespace and lines beginning with '#' are ignored.  The file format is:
#   <host-regex> : <user> : <pswd>
##
  set password_db "/etc/conman.pswd"

##
# Command-line argument defaults can be specified here.  This avoids exposing
#   sensitive information on the command-line.
##
# set user "USERID"
# set pswd "PASSW0RD"

###############################################################################

set env(PATH) "/usr/bin:/bin"

proc get_password {host user index} {
  global password_db
  set db_pswd {}
  if {! [info exists password_db]} {
    return
  }
  if {[catch {open $password_db} input]} {
    return
  }
  while {[gets $input line] != -1} {
    if {[regexp {^[ \t]*#} $line]} {
      continue
    }
    set record [split $line ":"]
    set db_host [string trim [lindex $record 0]]
    if {[catch {regexp "^$db_host$" $host} got_host_match]} {
      continue
    }
    if {! $got_host_match && [string length $db_host]} {
      continue
    }
    set db_user [string trim [lindex $record 1]]
    if {[string compare $db_user $user]} {
      continue
    }
    set db_pswd [string trim [lindex $record $index]]
    break
  }
  close $input
  return $db_pswd
}

if {! $argc} {
  set prog [lindex [split $argv0 "/"] end]
  send_user "Usage: $prog <host> <blade> <user> <pswd>\r\n"
  exit 1
}
if {$argc > 0} {
  set host [lindex $argv 0]
}
if {$argc > 1} {
  set blade [lindex $argv 1]
}
if {$argc > 2} {
  set user [lindex $argv 2]
}
if {$argc > 3} {
  set pswd [lindex $argv 3]
}
set bcmm_bay 1
set cmd_sent 0
set connected 0
if {! [info exists host]} {
  send_user "Error: Unspecified hostname.\r\n"
  exit 1
}
if {! [info exists blade]} {
  send_user "Error: Unspecified blade number.\r\n"
  exit 1
}
if {! [info exists user]} {
  send_user "Error: Unspecified username.\r\n"
  exit 1
}
if {! [info exists pswd]} {
  set pswd [get_password $host $user 2]
  if {! [string length $pswd]} {
    send_user "Error: Unspecified password.\r\n"
    exit 1
  }
}
if {! [info exists bcmm_timeout] || ($bcmm_timeout < 0)} {
  set bcmm_timeout -1
}
if {! [info exists idle_timeout] || ($idle_timeout <= 0)} {
  set idle_timeout -1
}
set cmd "console -T system:blade\[$blade]"
if {[info exists session_override] && ($session_override > 0)} {
  append cmd " -o"
}
if {[catch "spawn telnet $host" spawn_result]} {
  send_user "Error: $spawn_result.\r\n"
  exit 1
}
expect {
  -gl "\u001b\\\[2J" {
    if {$connected == 0} {
      exp_continue -continue_timer
    }
  }
  -gl "Invalid login" {
    send_user "Error: Permission denied.\r\n"
    exit 1
  }
  -gl "Command not recognized" {
    send_user "Error: Command not recognized.\r\n"
    exit 1
  }
  -gl "Invalid target path" {
    send_user "Error: Invalid blade name.\r\n"
    exit 1
  }
  -gl "The target bay is out of range" {
    send_user "Error: Invalid blade number.\r\n"
    exit 1
  }
  -gl "SOL session is already active" {
    send_user "Error: Console session already in use.\r\n"
    exit 1
  }
  eof {
    send_user "Error: Connection closed by remote host.\r\n"
    exit 1
  }
  timeout {
    if {$connected == 0} {
      send_user "Error: Timed-out.\r\n"
      exit 1
    }
  }
  -nocase -gl "username: \$" {
    if {$connected == 0} {
      send "$user\r"
    }
    exp_continue -continue_timer
  }
  -nocase -gl "password: \$" {
    if {$connected == 0} {
      send "$pswd\r"
    }
    exp_continue -continue_timer
  }
  -nocase -gl "OK\r\n" {
    if {$cmd_sent != 0} {
      incr bcmm_bay
      set cmd_sent 0
    }
    exp_continue -continue_timer
  }
  -nocase -gl "The target bay is empty.\r\n" {
    if {$bcmm_bay == 0} {
      send_user "Error: No blade in specified slot.\r\n"
      exit 1
    } elseif {$cmd_sent != 0} {
      set bcmm_bay 0
      set bcmm_timeout -1
      set cmd_sent 0
    }
    exp_continue -continue_timer
  }
  -nocase -gl "Command cannot be issued to this target." {
    if {$cmd_sent != 0} {
      set bcmm_timeout -1
      set cmd_sent 0
    }
    exp_continue -continue_timer
  }
  -nocase -gl "User does not have the authority to issue this command.\r\n" {
    if {$cmd_sent != 0} {
      set bcmm_timeout -1
      set cmd_sent 0
    }
    exp_continue -continue_timer
  }
  -nocase -gl "system> \$" {
    if {($bcmm_timeout >= 0) && ($cmd_sent == 0)} {
      send "telnetcfg -T system:mm\[$bcmm_bay] -t $bcmm_timeout\r"
      incr cmd_sent
    } elseif {($connected == 0) && ($cmd_sent == 0)} {
      send "$cmd\r"
      incr connected
    } else {
      send_user "Error: Unrecognized response.\r\n"
      exit 1
    }
    exp_continue -continue_timer
  }
  -re "\[^\r]*\r+\n" {
    exp_continue -continue_timer
  }
}
send_user "Connection established via telnet (pid $spawn_result).\r\n"

set timeout 2
interact {
  # Replace "&B" with serial-break.
  "&B" {
    send "\035send brk\r\n"
    expect "telnet> send brk"
  }
  # Match subsequent patterns against spawned process, not user's keystrokes.
  -o
  # Send "space-backspace" sequence after ${idle_timeout} seconds of inactivity
  #   in order to keep the connection alive.
  timeout $idle_timeout {
    send " \177"
  }
  # Disable "ESC (" sequence for stopping the console session and returning to
  #   the BladeCenter management module prompt.  If "session_override" is set,
  #   this will also prevent the console session from being stolen.
  -re "\r\nsystem> \$" {
    send "$cmd\r"
    expect {
      -gl "\r\nSOL session is already active\r\nsystem> \$" {
        send_user "\r\nConsole session stolen.\r\n"
        exit 1
      }
      -gl "\u001b\\\[2J"
    }
  }
}