Date: Thu, 3 Feb 2005 17:07:02 -0300 From: Alejandro Pulver <alejandro@varnet.biz> To: jaymo@cromagnon.cullmail.com Cc: freebsd-questions <freebsd-questions@freebsd.org> Subject: Re: running interactive program from shell script Message-ID: <20050203170702.55874ded@ale.varnet.bsd> In-Reply-To: <200502030502.28281.jaymo@cromagnon.cullmail.com> References: <200501300533.51350.jaymo@cromagnon.cullmail.com> <20050201122226.GM8619@alzatex.com> <200502030502.28281.jaymo@cromagnon.cullmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 3 Feb 2005 05:02:28 -0600 Jay Moore <jaymo@cromagnon.cullmail.com> wrote: > On Tuesday 01 February 2005 06:22 am, Loren M. Lang wrote: > > > > I need a shell script that initiates a telnet session to another host. I > > > have come up with the following, but unfortunately it terminates when the > > > script is finished. What I wanted was for the telnet session to remain > > > "alive" and interactive until manually terminated. > > > > > > Is there a way to accomplish this in a shell script? > > > > > > I've been told that I'll have to use "expect" or similar to accomplish > > > this, but it seems to me that I should be able to do this using just > > > Bourne shell commands. > > > > > > #! /bin/sh > > > > > > (sleep 3; > > > echo "password"; > > > sleep 3; > > > echo "ls -la"; > > > sleep 3; > > > ) | telnet -l user 192.168.0.2 > > [ explanation of pipes snipped ] > > I believe you are correct - thanks. Understanding why this is happening has > lifted a huge, uncomfortable burden :) > > But it still seems that there should be a way to do this using a shell > script... I will have to think about this some more. > > Best Rgds, > Jay Hello: I have tried the following and it worked for me (I am not sure about the correctness of redirecting input/output to/from a terminal device). This is the script (with comments included): ----- BEGIN ----- #!/bin/sh # Date: Thu, 3 Feb 2005 # Shell script to start a connection to another host using telnet and # keep the connection "alive". While the telnet session is running, # this shell script will also be running. # It uses redirection operators (pointing to the current TTY to avoid # blocking 'stdin'), and a FIFO (pipe) to communicate the reader # program (cat) with the telnet program. # To exit you have to end the telnet process ('quit' command) and # then input an ENTER or ^D (EOF) character to 'cat' (so it ends). # Example values are prefixed with "example-" (change them to real ones). FIFO="tmp-fifo" HOST="example-host" USER="example-user" PASS="example-pass" PORT="" # leave empty for default (23) TTY=`tty` # To communicate telnet and TTY. mkfifo $FIFO # Start telnet, reading from the FIFO and outputting everything to # the current TTY. Wait 3 seconds, log in, wait 3 seconds and run # cat, that reads from the TTY and outputs to the FIFO (that is # read by telnet). telnet -l $USER $HOST $PORT < $FIFO 2>&1 > $TTY & sleep 3; echo $PASS > $FIFO; sleep 3; cat > $FIFO < $TTY # Clean up (delete FIFO). rm $FIFO # Exit. exit 0 ----- END ------ Best Regards, Ale
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050203170702.55874ded>