#!/bin/sh - # # expect-shell functions # # Simple script to call a telnet server # # NOTE: these functions expect that pty and waitfor is on the current path # # File Descriptors used # >&4 Send to the telnet server # <&5 Recieve from the telnet server # >&6 terminal output (for debugging) # ><&8 The file opened for upload and download # PROGNAME=`/bin/basename $0` # base name of program ORIGDIR=`pwd` # original directory PAPBIN=`/bin/dirname $0` # directory of script cd $PAPBIN # cd to script directory PAPBIN=`pwd` # set correctly, full path to script DEBUG= # turn debugging output on/off Server="machine.network" # the server to call Port=6356 # on this port Prompt='telnet>' # telnet prompt to look for tmpfiles="" # the temporary file to remove on trap abort # ---------------------------------------------------------- Read() { # just read and ignore the response (used to read telnet echo's) exec <&5; read I [ "$DEBUG" ] && echo "--> $I" >&6 } ReadIgnore() { exec <&5; read I } CheckRead() { # Read and check that we read the given argument exec <&5; read I [ "$DEBUG" ] && echo "--> $I" >&6 case "$I" in $1) return 0 ;; *) echo "$name : \`\`$I''" | report; return 1 ;; esac } ReadFile() { # Download to the given filename # NOTE: EOF is a `.' on a line by itself exec <&5 8>"$1"; : > "$1" while :; do exec <&5; read I [ "$DEBUG" ] && echo "==> $I" >&6 [ "$I" = '.' ] && break echo >8 "$I" done exec 8>&- } Send() { # Send a string to the server [ "$DEBUG" ] && echo "<-" "$@" >&6 echo >&4 "$@" } SendRead() { # Send and ignore the return echo send "$@"; ReadIgnore } SendFile() { # Upload the given filename to the server # NOTE: EOF is a `.' on a line by itself exec 8<"$1" while exec <&8; read I; do [ "$DEBUG" ] && echo "<=" "$I" >&6 echo >&4 "$I" ReadIgnore done [ "$DEBUG" ] && echo "<= ." >&6 echo >&4 "." ReadIgnore exec 8<&- } WaitFor() { # wait for the given string to be received if [ "$DEBUG" ]; then waitfor "$1" <&5 2>&1 | sed 's/^/ <- /' >&6 else waitfor "$1" <&5 2>/dev/null fi } QuitServer() { # Close the telnet server connection sleep 1 # wait for things to settle echo >&4 "^]" # break back to telnet prompt WaitFor "$Prompt" Send "quit" Read } OpenServer() { # Open the telnet connection to server echo "Contacting $Server on $Port..." pty -d0E telnet recv & exec 4>send 5&- 5<&-; exit 1; } CheckRead "Connected*" || { QuitServer; exec 4>&- 5<&-; exit 1; } CheckRead "Escape*" || { QuitServer; exec 4>&- 5<&-; exit 1; } echo "Connected!" } # ---- Prepare the above functions --- # the abort trap trap ' exec 4>&- 5<&- 9<&- rm -f $tmpfiles if [ "$DEBUG" ] ; then echo >&6 "TRAP: aborting" exec 6>&- fi exit 1 ' 1 2 3 15 # create the named pipes required tmpfiles="$tmpfiles send recv" mknod send p mknod recv p # ---------------------------------------------------------- OpenServer exec <&5; read I; # read the servers first response (if it does) set - $I # and examine it SendRead "Command \"$ADMIN_NAME\"" # a command CheckRead "OK*" || { QuitTelnet; exec 4>&- 5<&-; exit; } # the return ...etc... exec 4>&- 5<&- 9<&- rm -f $tmpfiles if [ "$DEBUG" ] ; then echo >&6 "Program finished" exec 6>&- fi exit 0