From owner-freebsd-questions@FreeBSD.ORG Thu Feb 3 20:15:51 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 4929A16A4CE for ; Thu, 3 Feb 2005 20:15:51 +0000 (GMT) Received: from dexter.starfire.mn.org (starfire.skypoint.net [66.93.17.236]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4AA1443D5A for ; Thu, 3 Feb 2005 20:15:50 +0000 (GMT) (envelope-from john@dexter.starfire.mn.org) Received: (from john@localhost) by dexter.starfire.mn.org (8.11.3/8.11.3) id j13KFlc32991; Thu, 3 Feb 2005 14:15:47 -0600 (CST) (envelope-from john) Date: Thu, 3 Feb 2005 14:15:47 -0600 From: John To: Alejandro Pulver Message-ID: <20050203141547.A32973@starfire.mn.org> References: <200501300533.51350.jaymo@cromagnon.cullmail.com> <20050201122226.GM8619@alzatex.com> <200502030502.28281.jaymo@cromagnon.cullmail.com> <20050203170702.55874ded@ale.varnet.bsd> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <20050203170702.55874ded@ale.varnet.bsd>; from alejandro@varnet.biz on Thu, Feb 03, 2005 at 05:07:02PM -0300 cc: jaymo@cromagnon.cullmail.com 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:15:51 -0000 On Thu, Feb 03, 2005 at 05:07:02PM -0300, Alejandro Pulver wrote: > 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 It can be done with dead-reckoning and so forth, but I find "expect" to be really really great for this sort of thing, and recommend it highly if you have to do automated interactions with telnet or ftp sessions. -- John Lind john@starfire.MN.ORG