#!/usr/local/bin/kermit

; Illustration of scripted Telnet login using C-Kermit 6.0
; Compare with the version for C-Kermit 7.0.
; (The main difference is the ease in passing parameters from the command line)
;
; Optional command-line parameters (must be preceded by "=" sign).
;  1. Hostname or address
;  2. Username on host
;  3. Password on host
;
; Example:
;  autotelnet6 = xyzcorp.com fred secret
;
; Parameters not supplied are prompted for.
; Uses Telnet port only since a login dialog is assumed.
; Write a different script for accessing non-Telnet ports.
;
; WARNING: This is just an illustration.  It works with most servers,
; but is not general enough to work with all.  Prompts might be different,
; some terminal-related escape sequences might need to be exchanged, etc.
; See Chapter 19 of "Using C-Kermit" for details.
;
define badversion echo Sorry - C-Kermit 6.0 or later required., exit
if not equal "\v(program)" "C-Kermit" badversion
if LLT "\v(version)" "600192" badversion

local \%i \%h \%u \%p logging          ; Local variables

define logging 0                       ; Change to 1 to log the session

define fatal { if def \%1 echo \%1, exit 1 }

set network type tcp/ip
if fail fatal {Sorry, this version of C-Kermit does not support TCP/IP.}

; In C-Kermit versions prior to 7.0, we have to extract the arguments from
; the command line.
;
for \%i 1 \v(args) 1 {
    xif equal "\&@[\%i]" "=" {
        incr \%i
        if = \v(args) \%i break
        asg \%h \&@[\%i]
        incr \%i    
        if = \v(args) \%i break
        asg \%u \&@[\%i]
        incr \%i
        if = \v(args) \%i break
        asg \%p \&@[\%i]
        break
    }
}
while not def \%h {                    ; If hostname/address not supplied
    ask \%h { Host: }                  ; prompt for one until we get it.
    xif > 0 \index(\32,\%h,1) 1 {      ; Allow only one "word" here.
        echo Just the address please.  ; E.g. no TCP port number.
        undef \%h
    }
}
xif not def \%u {                      ; If username not supplied
    ask \%u { User: }                  ; Prompt for one, but default
    if not def \%u assign \%u \v(user) ; to local user ID.
}

echo Connecting to \%h as user \%u...

set host \%h 23                        ; Connect on Telnet port
if fail fatal {Can't open Telnet connection to \%h.}

if \m(logging) log session             ; Make session.log file

; Prompt for password if necessary only after connection is made
; (because there's no point in asking for it if the connection failed).

while not defined \%p {
    askq \%p { Password for \%u at \%h: }
}
set input echo off                     ; Don't echo scripted interactions.

; Note that some Telnet servers get your user ID automatically in Telnet
; negotiations and so only prompt for Password:, so look for both at once.
; Also allow username prompt to be "login:" (UNIX) or "Username:" (VMS).
; Also allow for the fact that some servers prompt "Password for blah:",
; whereas most others prompt just "Password:".

minput 20 login: Username: Password: {Password for \%u:}
if fail fatal {Timed out waiting for initial prompt}
xif < \v(minput) 3 {
    output \%u\13                      ; User ID required - send it.
    minput 10 Password: {Password for \%u:}
    if fail fatal {Timed out waiting for Password prompt}
}
output \%p\13                          ; Send password
undef \%p                              ; Erase password from memory
set exit on-disconnect on              ; Exit automatically if connection lost.

; The CONNECT command sends you online for an interactive session.  Instead
; of CONNECT, you can substitute additional scripting for automation of any
; interactions you would do by hand: use INPUT, OUTPUT, IF FAIL (or IF
; SUCCESS), and other scripting commands for this.  In a common example,
; you can start a Kermit server on the remote end and then transfer and/or
; manage remote files from this script.

connect

; At this point, escaping back while the connection is open will give you
; the C-Kermit> command prompt unless you include additional commands below.

End ; of C-Kermit sample Telnet script.
