1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}
package require Expect
# Description: Simple fragment to begin a telnet daemon
# For more information, see Chapter 17 of "Exploring Expect"
# Author: Don Libes, NIST
set IAC "\xff"
set DONT "\xfe"
set DO "\xfd"
set WONT "\xfc"
set WILL "\xfb"
set SB "\xfa" ;# subnegotation begin
set SE "\xf0" ;# subnegotation end
set TTYPE "\x18"
set SGA "\x03"
set ECHO "\x01"
set SEND "\x01"
send "$IAC$WILL$ECHO"
send "$IAC$WILL$SGA"
send "$IAC$DO$TTYPE"
remove_nulls 0
expect_before {
-re "^$IAC$DO$ECHO" {
# treat as acknowledgement and ignore
exp_continue
}
-re "^$IAC$DO$SGA" {
# treat as acknowledgement and ignore
exp_continue
}
-re "^$IAC$DO\(.)" {
# refuse anything else
send_user "$IAC$WONT$expect_out(1,string)"
exp_continue
}
-re "^$IAC$WILL$TTYPE" {
# respond to acknowledgement
send_user "$IAC$SB$TTYPE$SEND$IAC$SE"
exp_continue
}
-re "^$IAC$WILL$SGA" {
send_user "$IAC$DO$SGA"
exp_continue
}
-re "^$IAC$WILL\(.)" {
# refuse anything else
send_user "$IAC$DONT$expect_out(1,string)"
exp_continue
}
-re "^$IAC$SB$TTYPE" {
expect_user null
expect_user -re "(.*)$IAC$SE"
set env(TERM) [string tolower $expect_out(1,string)]
# no continue!
}
-re "^$IAC$WONT$TTYPE" {
# treat as acknowledgement and ignore
set env(TERM) vt100
# no continue!
}
}
# do negotations up to terminal type
# expect
##############################
# your code goes after this point here
# spawn something ;# typically spawn something
# expect ... ;# typically do some expects, sends, etc.
# send ...
# expect ...
# send ...
# expect_before ;# remove all protocol nonsense
# let user interact
# interact -re "\r" {send "\r"; expect_user \n {} null}
|