From owner-freebsd-questions@FreeBSD.ORG Thu Feb 3 20:06:39 2005 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8A6F816A4CE for ; Thu, 3 Feb 2005 20:06:39 +0000 (GMT) Received: from relay.pair.com (relay00.pair.com [209.68.1.20]) by mx1.FreeBSD.org (Postfix) with SMTP id A63B743D2D for ; Thu, 3 Feb 2005 20:06:38 +0000 (GMT) (envelope-from alejandro@varnet.biz) Received: (qmail 15152 invoked from network); 3 Feb 2005 20:06:37 -0000 Received: from unknown (HELO ale.varnet.bsd) (unknown) by unknown with SMTP; 3 Feb 2005 20:06:37 -0000 X-pair-Authenticated: 200.115.214.206 Date: Thu, 3 Feb 2005 17:07:02 -0300 From: Alejandro Pulver To: jaymo@cromagnon.cullmail.com 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> X-Mailer: Sylpheed-Claws 0.9.12b (GTK+ 1.2.10; i386-portbld-freebsd5.3) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit cc: freebsd-questions Subject: Re: running interactive program from shell script X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Feb 2005 20:06:39 -0000 On Thu, 3 Feb 2005 05:02:28 -0600 Jay Moore 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