C-Kermit / Kermit 95 Scripts Library

New York City
Most recent update: Sat Jan 18 17:15:29 2003

CONTENTS


INTRODUCTION

The command and script language of C-Kermit and Kermit 95 is described in Using C-Kermit, 2nd Edition, as supplemented by the C-Kermit 7.0 Supplement and the C-Kermit 8.0 Supplement.

Most of these scripts are intended to illustrate new features of C-Kermit 7.0 (released 8 Feb 2000), C-Kermit 8.0 (released 12 Dec 2001), K95 1.1.20 (March 2000) and K95 2.0 (June 2002).

The scripts are listed below. Those marked with (*) are "kerbang" scripts which, in UNIX, can be used exactly like shell scripts if you give them execute permission:

  chmod +x scriptname

Command-line arguments are accepted in the expected manner, e.g.:

  autotelnet xyz.com myuserid

This makes the the command-line arguments available to the script in the variables \%0 (script name), \%1 (first argument), \%2 (second argument), etc.

The first line of each kerbang script looks like:

  #!/usr/local/bin/wermit +

(but without the indentation). This indicates the pathname of the C-Kermit executable that is to execute the script; change this line as needed. The trailing plus sign is required if command-line arguments are to be passed to the script (and doesn't hurt if they are not). The "kerbang" feature requires C-Kermit 7.0 or later. For more about kerbang scripts, see the C-Kermit 7.0 Supplement Section on this topic.

On non-UNIX platforms, these scripts are executed by:

  1. Giving a TAKE filename command to Kermit. In C-Kermit 7.0 and later, the filename can be followed by arguments, which are assigned to to the varables \%1, \%2, ..., \%9.

  2. Including the script filename as the first command-line argument to the Kermit program, followed by a plus sign, followed by the arguments. In VMS and Windows, the plus sign seems to cause trouble with the shell, so in that case you can substitute an equal sign, but put it after the script file name instead of before it:

      kermit script.ksc = arg1 arg2 arg3 ...  VMS 
      k95 script.ksc = arg1 arg2 arg3 ...     Windows
    

    This assigns arg1 to \%1, arg2 to \%2, and so on.

  3. In Windows 95/98/ME/NT/2000/XP, by filetype association (when the script filename has the suffix ".ksc"), but in this case Windows does not provide a mechanism to pass arguments to the script.

Outside of UNIX, the "kerbang" line has no effect, since it is a comment to Kermit. In VMS, any references to "environment variables" can be satisfied by logical names or DCL symbols.

These scripts are for illustrative purposes only and carry no warranty, express or implied.


TUTORIAL

The Kermit scripting language is a programming language similar to Perl, but with different syntax (because the Kermit language predates Perl and most other scripting languages). The Kermit language is portable across UNIX (Linux, AIX, HP-UX, Solaris, FreeBSD, IRIX, SINIX, QNX, SCO, Tru64, and every other known UNIX variation), VMS, Stratus VOS, Data General AOS/VS, Windows 95/98/ME/NT/2000/XP, OS/2, Plan 9, OS-9/68000, the Commodore Amiga, and other platforms, and works uniformly on serial connections (direct or dialed) and network connections. Thus learning the language is a good investment of your time since it can be applied to almost any communications problem. The Kermit script language is documented in the book Using C-Kermit.

The Kermit scripting language is easy to learn if you already use Kermit, since it is the same as Kermit's command language. A Kermit script program is simply a series of Kermit commands collected into a file or a macro. To execute the script, you tell Kermit to TAKE the file or DO the macro. Or in UNIX you can also execute it as if it was a shell script, as described at the top of this page. In either case you can pass parameters to the script in the command that invokes it.

When using Kermit "manually", i.e. interacting with the host directly, you typically make a connection (TELNET, DIAL, etc), and then interact with the other computer directly, switching back and forth between the Kermit command screen and the terminal screen. The command to switch from the command screen to the terminal screen is CONNECT (C is a sufficient abbreviation). Returning from the terminal screen to the command screen requires a special "escape sequence" such as Ctrl-\C, Ctrl-]C, or Alt-x (Alt-x is used in Kermit 95 and MS-DOS Kermit). Note that Kermit's TELNET command is a shortcut for SET HOST followed by CONNECT; that is, TELNET includes an implied CONNECT command.

When automating a session, you do not switch back and forth between "screens"; you do not CONNECT or escape back. In a script, everything is done in command mode. There is no terminal screen in a script. Instead of CONNECT (or TELNET), use the following commands, which tell Kermit to do what you would do "by hand":

SET HOST [ switches ] hostname-or-address [ switches ]
Open a network connection but remain in command mode, i.e. without entering the Terminal screen or CONNECT mode.

INPUT timeout string
Wait up to timeout seconds for the given string to arrive from the other computer. If it arrives, this command succeeds; otherwise the command fails. Example: INPUT 10 login: The INPUT command can accept not only simple strings but also patterns. An alternative form, MINPUT, accepts a list of match strings and/or patterns.

SET INPUT ECHO ON
Normally you don't see scripted dialogs on your screen. Use this command to let you see the what Kermit and the host are saying to each other. This doesn't affect the operation of the script, only what you can see.

IF FAILURE command
If the preceding command (SET HOST, INPUT, or any other command) failed, execute the given command.
Example: IF FAIL EXIT 1 No login prompt

IF SUCCESS command
If the preceding command succeeded, execute the given command.

STOP number string
Stop the script and return to the Kermit prompt. The number is a success code: 0 for success, nonzero for failure; the command that invoked the current command file (TAKE) or macro (DO or "implied DO") can be tested for success or failure based on this code. If a message is given, it is printed.

END number string
Like STOP, but pops the command stack just one level instead of all the way back to the top. Use this for returning early from a macro or command file to its caller. Synonym: POP.

EXIT number string
Stop the script and exit from Kermit. The number is Kermit's exit status code, normally 0 for success, nonzero for failure. If a message is given, it is printed.

OUTPUT string
Send the given string to the other computer. Control characters may be included in the string using \ddd notation (where the d's are digits, and ddd represents the numeric code for the control character.
Example: OUTPUT olga\13

LINEOUT string
(C-Kermit 7.0 and later; Kermit 95 1.1.20 and later) Since it is so common to output a line with a carriage return on the end, this command does it for you, so you don't have to remember to include \13 on the end. lineout foo is equivalent to output foo\13.

INPUT takes the place of your eyes, OUTPUT takes the place of your fingers, and IF takes the place of your brain.

The rest is regular programming: FOR, WHILE, SWITCH, GOTO, variables, arrays, functions, block structure, nesting, scoping, and the rest, listed HERE and documented in the manual (just as any other programming language is documented in its own manual).

Here is a very simple example of making a Telnet connection to UNIX and logging in:

  set host foo.bar.baz.com           ; Make the connection
  if fail stop 1 Connection failed   ; Check that it was made
  input 20 login:                    ; Wait 20 seconds for login: prompt
  if fail stop 1 No login prompt     ; Check that it came
  output myuserid\13                 ; or "lineout myuserid"
  input 5 Password:                  ; Wait 5 seconds for Password: prompt
  if fail stop 1 No Password prompt  ; Check that it came  
  output mypassword\13               ; or "lineout mypassword"

This illustrates how your actions in the terminal screen are simulated by INPUT (eyes), OUTPUT (fingers), and IF (brain). It can be elaborated to any desired degree: to use variables instead of constants for host, username, or password; to prompt for the password so you don't have to store it in a file; to attempt some sort of recovery action if a command fails instead of just stopping, and so on. And of course you can add more steps -- have it transfer a file, send email, whatever you want.

The syntax of the Kermit programming language should be familiar to anyone who uses other scripting languages such as Perl or the UNIX shell. It is a string substitution language, therefore an "escape character" (backslash) is used to indicate string substitution. Since many kinds of items can be substituted, the backslash is followed by a second character to indicate which kind of substitution is to be made: a scalar variable, an array element, a function result, a special character, and so forth. Examples:

  \%a           A scalar user-defined variable, evaluated recursively
  \m(name)      A scalar user-defined variable, evaluated one level deep
  \v(name)      A built-in variable (such as \v(time), "show var" for a list)
  \&a[1]        An array element, evaluated recursively
  \fname(args A function invocation ("show func" for a list)
  \x0F          A character whose code is the given hexadecimal number (0-255)
  \123          A character whose code is the given decimal number (0-255)
  \\            A literal backslash.

This should give you an idea how to read the scripts in the library, and how to write a simple script or adapt one of them to your needs. For a brief description of a particular Kermit command or function, use Kermit's HELP command. For a description of a built-in function, type "help function xxx" at the prompt, where xxx is the function name. For a thorough treatment, please consult the manual.

Finally, remember:

[ Top ] [ Contents ] [ C-Kermit ] [ Kermit 95 ] [ Kermit Home ]


C-Kermit Initialization Files

Internet Scripts:

Modem Scripts:

Pager Scripts:

Screen-Formatting Scripts:

Screen-Scraping Scripts:

File-Transfer Scripts:

File-Management Scripts:

Number-Crunching Scripts:

Date-Time Arithmetic:

Object-Oriented Programming:

(And other creative programming techniques.) This section by Dat Thuc Nguyen.

Script-Language Torture Tests:


Links:


C-Kermit/K95 Script Library / Columbia University / [email protected] / 18 January 2003