Re: running interactive program from shell script

From: Alejandro Pulver (alejandro_at_varnet.biz)
Date: 02/03/05

  • Next message: John: "Re: running interactive program from shell script"
    Date: Thu, 3 Feb 2005 17:07:02 -0300
    To: jaymo@cromagnon.cullmail.com
    
    

    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
    _______________________________________________
    freebsd-questions@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-questions
    To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"


  • Next message: John: "Re: running interactive program from shell script"

    Relevant Pages

    • Re: running interactive program from shell script
      ... >> But it still seems that there should be a way to do this using a shell ... > # this shell script will also be running. ... > # To communicate telnet and TTY. ... > mkfifo $FIFO ...
      (freebsd-questions)
    • Re: Creating a tty in a C program
      ... My customer is trying to fork off ssh to ... ssh is complaining that there is no tty available so ... > then run their app (since telnet creates a new tty). ...
      (comp.unix.solaris)
    • Re: Creating a tty in a C program
      ... My customer is trying to fork off ssh to ... ssh is complaining that there is no tty available so ... > then run their app (since telnet creates a new tty). ...
      (comp.unix.programmer)
    • shell script - telnet port 25
      ... I am trying to write a shell script which keeps checking for smtp ... server with "telnet smtp.test.com 25" ... Unless i type quit it hangs there and can't grep 220. ...
      (comp.unix.shell)
    • shell script - telnet port 25
      ... I am trying to write a shell script which keeps checking for smtp ... server with "telnet smtp.test.com 25" ... Unless i type quit it hangs there and can't grep 220. ...
      (comp.unix.shell)