running interactive program from shell script
Alejandro Pulver
alejandro at varnet.biz
Thu Feb 3 12:06:39 PST 2005
On Thu, 3 Feb 2005 05:02:28 -0600
Jay Moore <jaymo at 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
More information about the freebsd-questions
mailing list