Original Question:
>Hello sun managers,
>I have a need to redirect a telnet session from a file or a script. I have trie>d to redirect a file into telnet with the appropriate login and password inform>ation. It appears to me that the end of the file is reached to soon and the con>nection is closed. I have also tried a script using the 'here document' struct>ure to redirect input to telnet. It also did not work.
>
>Example Output: (meteor is the host to telnet to)
>
>Trying 192.103.9.5 ...
>Connected to meteor.
>Escape character is '^]'.
>Connection closed by foreign host.
Thank you all that responded!
The responds can be broken up into 3 categories:
1) People told me to get ahold of the public domain package 'expect' that can be downloaded via anonymous ftp from ftp.cme.nist.gov as pub/expect/expect.shar.Z
2) Use a script with the sleep command in it and redirect it into the telnet program. Example:
#! /bin/sh
(echo "open foo.bar.com"
sleep 1
echo "ls" -> or what ever you want to do
sleep 1
echo "quit"
sleep 1) | telnet
3) Or write your own program that reads in an ASCII file with special pause command lines that can be redirected into telnet. The program is listed:
cat ascpause.c
/*SD0*/
/* ---------------------------------------------------------------------*/
/* Source Control Options: @(#)%M% %I% %G% %U% */
/* */
/* Source File: ascpause.c */
/* Written By: Paul Blankenbaker */
/* Date Written: 8/31/92 */
/* */
/* Copyright (c)1992 - LightNET Inc. */
/* */
/* Description: Simple program to read an ASCII file, and then */
/* print the contents of the ASCII file to the */
/* screen (not including 'special' lines such */
/* as comments and pauses). */
/* */
/* This source file contains the following functions: */
/* int main(int argc, char *argv[]) */
/* */
/* Below is area to log changes made to this source file: */
/* */
/* Modification History: */
/* */
/* Date Initials Modifications to source file */
/* */
/* -------------------------------------------------------------------- */
/*ED0*/
/* ------------------------------------------------------------ */
/* Includes */
/* ------------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ------------------------------------------------------------ */
/* Defines */
/* ------------------------------------------------------------ */
#define COMMENTLINE '#'
#define COMMANDLINE '.'
#define BUFSIZE 300 /* up to 300 bytes per line */
/* ------------------------------------------------------------ */
/* Code */
/* ------------------------------------------------------------ */
/*SD1*/
/* ---------------------------------------------------------------------*/
/* FUNCTION: int main(int argc, char *argv[]) */
/* */
/* PROTO IN: n/a */
/* */
/* DESCRIPTION: Program to read an ASCII file, and perform the */
/* following based on each line in the file: */
/* */
/* # Treat line as a comment line (ignored) */
/* .p val Pause for number of seconds specified */
/* .DATA print DATA (must not begin with p) - used for */
/* quoting lines that start with '.' or '#' */
/* DATA print DATA (must not begin with '.' or '#') */
/* */
/* This program is intended to be used to pipe data to */
/* another program, so that the data stream may be paused */
/* as sent the other program (a telnet script for example) */
/* */
/* PARAMETERS: (none) */
/* */
/* RETURNS: 0 If successfully processed file */
/* */
/* CREATED: 8/31/92 */
/* */
/* SIDE EFFECTS:none */
/* */
/* EXAMPLES: ascpause < telnet.script | telnet */
/* */
/* Where the telnet.script file looks like the following: */
/* -------------------------------------------------------------------- */
/*
# Simple script file to be used for a telnet connection
# First give a short pause and then open connection to desired host
..pause 1
open HOST
# Wait 5 seconds for login response back from host
..pause 5
# Login as USER ID
USERID
# wait 3/10ths of a second before sending password
..pause .3
PASSWORD
# wait 3 seconds for login processing
..pause 3
# execute an ls command on the remote system, and then logout
ls
logout
# wait for the logout to be accepted, and then send escape sequence & quit
..pause .5
^]
quit
*/
/*ED1*/
int main(int argc, char *argv[]) {
char buf[BUFSIZE]; /* buffer for reading in data lines */
char *asctime,*left; /* pointer to ascii pause time (secs) */
double secs; /* number of seconds to sleep */
int line=0; /* current line in file */
/* while more data */
while (fgets(buf,BUFSIZE,stdin)) {
line++; /* bump line number count */
switch (buf[0]) {
case COMMENTLINE : /* '#' - Comment line */
break; /* ignore */
case COMMANDLINE : /* '.' - Command line */
if (buf[1] == 'p') { /* '.p[ause]' command */
strtok(buf," \t\n\v"); /* tokenize the string */
/* if duration field present */
if (asctime = strtok(NULL," \t\n\v")) {
/* sleep the time specified by field */
secs = strtod(asctime, &left);
if (left != asctime) {
usleep((int) (secs * 1000000.0));
}
else { /* invalid pause time specified */
fprintf(stderr,"Line %5d: ***ERROR*** invalid pause time\n",
line);
}
}
else {
fprintf(stderr,"Line %5d: ***ERROR*** no pause time specified\n",
line);
}
}
else { /* otherwise, quote rest of line */
fprintf(stdout,"%s",&buf[1]);
fflush(stdout);
}
break;
default : /* default - just echo line */
fprintf(stdout,"%s",buf);
fflush(stdout);
}
}
return 0; /* exit with return code of 0 */
}
/* ------------------------------------------------------------ */
/* */
/* ------------------------------------------------------------ */
This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:06:48 CDT