#!/usr/local/bin/kermit +

; Illustration of scripted Telnet login using C-Kermit 7.0 or K95 1.1.19.
;
; F. da Cruz, Columbia University Kermit Project, August 1999.
; Revised February 2000 for portability between K95 and C-Kermit.
;
; Optional command-line parameters:
;  1. Hostname or address
;  2. Username on host
;  3. Password on host
;
; Parameters not supplied are prompted for.
; Uses Telnet port (23) only since a login dialog is assumed.
; Write a different script for accessing non-Telnet ports, in which:
;  . The service port is an additional parameter
;  . "set host <name> /telnet" is replaced by "set host <name> <port>"
;  . The dialog section is removed or modifed appropriately.
; Omit the "/telnet" switch for ports that do not use Telnet protocol.
;
; WARNING: This is just an illustration.  It works with most Telnet 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 7.0 or K95 1.1.19 or later required.
    exit 1
}
if not equal "\v(program)" "C-Kermit" badversion
if LLT \v(version) 700196 badversion

; From here down we make free use of C-Kermit 7.0 / K95 1.1.19 features.

local kerbang				; Invoked as Kerbang script?
.kerbang = 0				; Assume no.
if eq "\%0" "\v(cmdfil)" .kerbang = 1	; This means we were.

define ERRQUIT {			; Macro to exit appropriately
    if def \%1 echo \%1			; with an error message.
    if \m(kerbang) exit 1		; If Kerbang script, exit all the way.
    stop 1				; Otherwise return to Kermit prompt.
}

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

while not def \%1 {			; If hostname/address not supplied
    ask \%1 { Host: }			; prompt for one until we get it.
    if > \fsplit(\%1,,,.) 1 {		; Allow only one "word" here.
        echo Just the address please.	; E.g. no TCP port number.
        undef \%1
    }
}
if not def \%2 {			; If username not supplied
    ask \%2 { User [\v(user)]: }	; Prompt for one, but default
    if not def \%2 assign \%2 \v(user)	; to local user ID.
}
set telnet environment user \%2		; Make sure correct userid is sent

; To force the remote host to issue a login (username) prompt, uncomment
; the following command.  This is necessary if your local and remote user
; IDs are not the same.
;
;;; set login userid

; This script assumes that authenticated logins are not being performed
; via Kerberos, SRP, or any other supported method.

set telopt start-tls refuse		; Do not use START_TLS option
set telopt authentication refuse	; Do not use AUTH option
set telopt encrypt refuse refuse	; Do not use ENCRYPT option

;;; log session session.log             ; Uncomment to log the session

echo Connecting to \%1 as user \%2...

; Change the following statement if login on non-Telnet port is desired.

set host \%1 23 /telnet			; Force Telnet protocol negotiations
if fail errquit {Can't open Telnet connection to \%1.}

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

while not defined \%3 {
    askq \%3 { Password for \%2 at \%1: }
}
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 <username>:",
; whereas most others prompt just "Password:".
;
minput 20 login: Username: Password: {Password for \%2:}
if fail errquit {Timed out waiting for initial prompt: \v(inwait) sec.}
if ( = \v(minput) 1 || = \v(minput) 2 ) {
    lineout \%2                        ; User ID required - send it.
    minput 10 Password: {Password for \%2:}
    if fail errquit {Timed out waiting for Password prompt: \v(inwait) sec.}
}
lineout \%3				; Send password
undef \%3				; Erase password from memory
if \m(kerbang) {			; Automatic exit on connection loss.
    set exit on-disconnect on
}

; 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 Kermit command prompt unless you include additional commands below.

End ; of Kermit sample Telnet script.
