Debian Codegraph @

drivers/zseries-console

raw
1 #!/usr/bin/expect
2 # Copyright 2000, 2001, 2004 by Paul Mattes.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose and without fee is hereby granted,
5 # provided that the above copyright notice appear in all copies and that
6 # both that copyright notice and this permission notice appear in
7 # supporting documentation.
8 #
9 # s3270 is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the file LICENSE for more details.
12 #
13 # Based on cms_cmd.expect licenced as above, modifications generating
14 # zseries-console:
15 # (C) Copyright IBM Corp. 2004, 2005, 2006
16 # and released under the GNU Public License V2
17 #
18
19 # Read in the glue functions.
20 set lib [file dirname $argv0]
21 source "$lib/x3270_glue.expect"
22
23 set P "console-zseries"
24
25 if { [llength $argv] != 4 } {
26 puts stderr "Usage: $P <VM Userid> <VM Password> zseries <VM System>"
27 exit 1
28 }
29
30 set username [lindex $argv 0]
31 set password [lindex $argv 1]
32 set hostname [lindex $argv 3]
33 set command "ipllnx"
34
35 # Procedure to wait for a READ prompt from CMS or CP.
36 proc waitread {} {
37 Snap Save
38 while {[Snap Ascii [expr [Snap Rows]-1] [expr [Snap Cols]-17] 4]
39 != "Read"} {
40 Snap Wait Output
41 }
42 }
43
44 # Procedure to check for the CMS "Ready" prompt.
45 # Returns its row number, or -1 for "MORE..." state, and leaves a screen with
46 # data to read in the Snap buffer.
47 proc cmd_done {} {
48 global verbose
49 Snap Save
50 while {1} {
51 if {[Snap Ascii [expr [Snap Rows]-1] [expr [Snap Cols]-20] 7]
52 == "More..."} {
53 if {$verbose} {puts "More..."}
54 return -1
55 }
56
57 set i [expr [Snap Rows]-2]
58 while {$i >= 0} {
59 set text [Snap Ascii $i 0 [Snap Cols]]
60 switch -regexp -- $text {
61 "Ready; T=.*" {return $i}
62 "Ready\(.*\); T=.*" {
63 error [Snap Ascii [expr $i-1] 0 \
64 [Snap Cols]]
65 }
66 "^ *\$" {}
67 "00: Please choose*" { return $i }
68 default {
69 if {$verbose} {
70 #puts "Incomplete $i '[string trimright $text]'"
71 }
72 set mylength [string length [string trimright $text]]
73 if { $mylength > 5 } {
74 if {[Snap Ascii $i [expr $mylength-6] 6] == "login:"} {
75 puts "Hohooooooooo login prompt found"
76 return $i
77 }
78 }
79
80 set i 0
81 }
82 }
83 incr i -1
84 }
85 Snap Wait Output
86 }
87 }
88
89 # Execute a command, return the output.
90 proc cms_cmd {text} {
91 global verbose
92
93 # Clear the screen.
94 Clear
95
96 # Send the command.
97 String "$text\n"
98
99 # 'first' is the row where the first line of output will appear. For
100 # the first screenful it's 1; after that it's 0.
101 set first 1
102
103 # r is the result.
104 set r {}
105
106 while {1} {
107 # Wait for a screenful.
108 set d [cmd_done]
109
110 # Dump out what's there.
111 set i $first
112 #set first 0
113 if {$d < 0} {set last [expr [Snap Rows]-2]} {set last $d}
114
115 while {$i <= $last} {
116 set r [linsert $r end [string trimright [Snap Ascii $i 0 [Snap Cols]]]]
117 incr i
118 }
119 if {$d >= 0} {
120 for {set n 0} {$n < [llength $r]} {incr n} {
121 puts [lindex $r $n]
122 }
123 break
124 }
125
126 # Clear the screen and go around again.
127 Clear
128 }
129 return $r
130 }
131
132 ## Calculates how many lines are there in the s3270 screen buffer
133 proc find_last_line {} {
134 global debug
135 global verbose
136 Snap Save
137
138 if {$debug == 1} {
139 puts "v---"
140 foreach l [Snap Ascii] {
141 puts $l
142 }
143 puts "^---"
144 }
145
146 if {[Snap Ascii [expr [Snap Rows]-1] [expr [Snap Cols]-20] 7]
147 == "More..."} {
148 ## To denote the console buffer is full
149 return -1
150 }
151
152 set i [expr [Snap Rows]-3]
153
154 while {$i >= 0} {
155 set text [Snap Ascii $i 0 [Snap Cols]]
156 switch -regexp -- $text {
157 "^ *\$" {
158 }
159 default {
160 return $i
161 }
162 }
163 incr i -1
164 }
165 ## To denote the console buffer is empty
166 return -2
167 }
168
169 # Print the s3270 screen buffer
170 proc print_cons_buf {} {
171 ##puts "<<output>>"
172 set output 0
173 while {1} {
174 # Grab the screen and clear it out.
175 Snap Save
176 Clear
177
178 set screen [Snap Ascii 0 0 [expr [Snap Rows]-3] [Snap Cols]]
179
180 # Find how many lines are populated and output those.
181 for {set last [llength $screen]} {$last >= 0} {incr last -1} {
182 switch -regexp -- [lindex $screen $last] {
183 "^ *\$" {
184 }
185 default {
186 break
187 }
188 }
189 }
190 ##puts "last<$last>"
191 if {$last < 0} {
192 break
193 }
194 for {set line 0} {$line <= $last} {incr line} {
195 set output 1
196 puts -nonewline "\n[string trimright [lindex $screen $line]]"
197 }
198 }
199 ##puts "output<$output>"
200 return $output
201 }
202
203 # Set 'verbose' to 1 to get debug output from the glue functions.
204 set verbose 1
205 set debug 0
206
207 proc note {m} {
208 global P
209 puts "$P: $m"
210 }
211 proc warn {m} {
212 global P
213 puts "$P: WARNING: $m"
214 }
215 proc winge {m} {
216 global P
217 puts "$P: MACHINE ERROR: reboot failed - $m"
218 }
219
220 note "Logging into the hosting VM system to restart the VM guest"
221
222 # Start s3270
223 Start
224 # Setverbose 1
225
226 # Connect to the host and wait for an input field.
227 Connect $hostname
228 Wait InputField
229
230 # Log in and wait for CP READ or VM READ mode.
231 note "logging in ... sending username"
232 String "$username\n"
233
234 # Wait for the input field, to proceed further. If timed out screen scrape
235 # the s3270 (error) message.
236 if {[catch {set ret_val [Wait 10 InputField]} err]} {
237 print_cons_buf
238 note "password never prompted"
239 exit 2
240 }
241
242 # Sometimes the input field is emitted before the output from the VMserver.
243 # Hence, after reading the input field wait for the output.
244 if {[catch {set ret_val [Wait 10 Output]} err]} {
245 print_cons_buf
246 note "No output seen even after the reading the InputFiled"
247 exit 2
248 }
249
250 if { [Ascii 2 0 19 ] == "Enter your password" } {
251 print_cons_buf
252 note "pasword prompted ..."
253 note "sending password ..."
254 String "$password\n"
255 } elseif { [Ascii 1 11 7] == "Already"} {
256 # If we can't log on, we're hosed.
257 Enter
258 print_cons_buf
259 warn "Already logged in. logging in here....."
260 String "logon $username here\n"
261 # Wait for the input field to proceed further
262 if {[catch {set ret_val [Wait 10 InputField]} err]} {
263 print_cons_buf
264 note "password never prompted"
265 exit 2
266 }
267 if { [Ascii 2 0 19 ] == "Enter your password"} {
268 print_cons_buf
269 note "sending password ..."
270 String "$password\n"
271 }
272 } elseif { [Ascii 1 20 19] == "not in CP directory" } {
273 print_cons_buf
274 note "'$username' is not a valid username"
275 exit 1
276 } else {
277 # Other unknown errors (firewall??)
278 print_cons_buf
279 warn "Unknown Error: Check the console log"
280 exit 1
281 }
282
283 if { [Ascii 1 11 23] == "PASSWORD NOT AUTHORIZED" } {
284 while {1} {
285 note "authorisation failure: PASSWORD NOT AUTHORIZED: holding ..."
286 after 5000
287 }
288 exit 1
289 } elseif { [Ascii 1 11 16] == "PASSWORD EXPIRED" } {
290 while {1} {
291 note "authorisation failure: PASSWORD EXPIRED: holding ..."
292 after 5000
293 }
294 note "Password expired"
295 exit 1
296 } {
297 note "Password authorized"
298 }
299 print_cons_buf
300 waitread
301
302 note "Connected to VM"
303
304 # If we're in CP mode, which means we disconnected last time, boot CMS.
305 if {[Ascii [expr [Rows]-1] [expr [Cols]-20] 2] == "Cp"} {
306 note "Starting CMS"
307 Clear
308 String "i cms\n"
309 String "no"
310 waitread
311 }
312
313 Snap save
314 set n [expr [Snap Rows]-2]
315 set first 1
316 while {$n >= $first} {
317 set lptext [Snap Ascii $first 0 [Snap Cols]]
318 set lplength [string length [string trimright $lptext]]
319 puts "$lptext "
320 incr first
321 }
322
323 # Enter an empty command to get a CMS prompt. If we don't do this, there will
324 # be a Ready prompt as the first line of output below.
325 Enter
326 cmd_done
327
328 note "Executing command $command"
329 cms_cmd $command
330 Clear
331
332 String "#cp term more 50 10\n"
333
334 set timeouts {1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 5}
335 set otimeout 0
336 set double 1
337 set cmdline {}
338 while {1} {
339 set ctimeout $otimeout
340 interact {
341 "" {
342 puts -nonewline "\b \b"
343 set cmdline [string range $cmdline 0 end-1]
344 flush stdout
345 }
346 "" {
347 puts -nonewline "\b \b"
348 set cmdline [string range $cmdline 0 end-1]
349 flush stdout
350 }
351 "\r" {
352 ## When enter(alone) is pressesed, s3270 goes from
353 ## "Running" to "Vm Read" mode. So, for actual one
354 ## enter key press, two 'Enter'[a s3270 function] is
355 ## required
356 String $cmdline
357 set cmdline {}
358 Enter
359 if {$double == 1} {
360 Enter
361 }
362 set double 1
363
364 print_cons_buf
365 if {$ctimeout != 0} {
366 set otimeout 0
367 return
368 }
369 }
370 -re "(.)" {
371 set char $interact_out(0,string)
372 puts -nonewline "$char"
373 set cmdline "$cmdline$char"
374 flush stdout
375 set double 0
376
377 if {$ctimeout != 0} {
378 set otimeout 0
379 return
380 }
381 }
382 timeout [lindex $timeouts $otimeout] {
383 ## Read the console buffer and print it
384 ##puts "<<timeout>>"
385 if {[print_cons_buf]} {
386 set otimeout 0
387 } elseif {$otimeout < 15} {
388 incr otimeout
389 }
390
391 #puts "TIMEOUT: $otimeout [lindex $timeouts $otimeout]"
392 return
393 }
394 }
395 }