Automate a 3270 session from a script: s3270, Wait and screen scraping
Drive a TN3270 session from a script with s3270: send actions on standard input, wait for the host, read the screen as text, and check ok or error.
To automate a 3270 application, a script has to do what a person does: wait for a screen, type into the right field, press an AID key such as Enter or PF3, and read the reply. The x3270 family has an emulator built for this, s3270, with no window at all. A script starts it and talks to it over standard input and output.
The protocol
The script writes one line of actions, ending with a newline. s3270 answers with:
- any output, each line starting with
data:; - a prompt line of twelve fields describing the emulator: keyboard (
Uunlocked,Llocked,Eoperator error), formatted or not, protected field or not, connection, mode, model, rows, columns, cursor row and column (both counted from 0), window ID and the time the command took; - a last line that is
okorerror.
Always check that last line. An action fails if its arguments are wrong, if the emulator is in the wrong mode, or if the host disconnects.
The actions you use most
Connect(host:port)andDisconnect(). Without a port, Connect uses port 23.String("text")types text at the cursor, as if typed. In 3270 mode the text is converted to EBCDIC with the host code page.Enter(),PF(3),Clear()and the other AID keys. AID actions such asPF()block until the host unlocks the keyboard (theaidWaitresource controls this), so the next action sees the new screen.Wait(...)pauses until a condition is true, for exampleWait(inputField)for an input field,Wait(30,stringAt,1,2,"READY")for text at row 1, column 2 within 30 seconds, orWait(disconnect).Ascii1()returns the whole screen as text.Ascii1(row,col,length)returns part of it. Its rows and columns start at 1, unlike the prompt line.
A logon script is a short list of these: connect, wait for an input field, type the user ID, press Enter, wait for the password field, and so on.
Other ways in
-scriptportand-socketmake an emulator accept the same protocol on a TCP port or a Unix socket.-httpdoffers a REST interface over HTTP.- Inside x3270, the
Script()action starts a child program that controls the session. - On Windows, products that offer EHLLAPI let a program drive an open emulator session instead. See What HLLAPI and EHLLAPI are, and when you need them.
Things that make scripts reliable
- Wait for something specific on the screen, not for a fixed number of seconds.
- Check the field you are typing into is the one you expect: the cursor row and column are in every prompt line.
- Handle screens that only appear sometimes, such as a message the host shows before the menu.
Related
- What HLLAPI and EHLLAPI are, and when you need them
- AID keys
- Fields and attributes
- App guide: Scripts and macros