From owner-freebsd-questions@FreeBSD.ORG Thu Feb 3 11:04:04 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 BB4F416A4CE for ; Thu, 3 Feb 2005 11:04:04 +0000 (GMT) Received: from cromagnon.cullmail.com (cromagnon.cullmail.com [67.33.58.202]) by mx1.FreeBSD.org (Postfix) with ESMTP id D960643D1F for ; Thu, 3 Feb 2005 11:04:03 +0000 (GMT) (envelope-from jamoore@cromagnon.cullmail.com) Received: from cromagnon.cullmail.com (localhost.cullmail.com [127.0.0.1]) j13B2TxY023169; Thu, 3 Feb 2005 05:02:29 -0600 (CST) (envelope-from jamoore@cromagnon.cullmail.com) Received: from localhost (localhost [[UNIX: localhost]]) by cromagnon.cullmail.com (8.12.10/8.12.10/Submit) id j13B2S2A023168; Thu, 3 Feb 2005 05:02:29 -0600 (CST) (envelope-from jamoore) From: Jay Moore To: "Loren M. Lang" Date: Thu, 3 Feb 2005 05:02:28 -0600 User-Agent: KMail/1.6.1 References: <200501300533.51350.jaymo@cromagnon.cullmail.com> <20050201122226.GM8619@alzatex.com> In-Reply-To: <20050201122226.GM8619@alzatex.com> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200502030502.28281.jaymo@cromagnon.cullmail.com> cc: FreeBSD Mailing List Subject: Re: running interactive program from shell script X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: jaymo@cromagnon.cullmail.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Feb 2005 11:04:04 -0000 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 > > When any command is run by default, it's standard input comes from the > same place as it's parent process. When you run this shell script from > your prompt, it inherits a standard input coming from your keyboard and > any command it runs inherit the same input as the shell. The same goes > for a processes standard output which it to your screen. Now when you > run a pipe (|), thing are a little different, the standard input and > output get redirected a little bit. The following command sets up a > pipe between two programs: > > leftProgram | rightProgram > > leftProgram's standard input is still from the keyboard, but it's output > gets redirected to the standard input of rightProgram and rightPrograms > standard output goes to the screen as usual. Everything rightProgram > reads on standard input comes only from leftProgram's standard output, > it's no longer reading from the keyboard. When leftProgram's done > sending output and exits, rightProgram sees and End Of File (EOF) and, > in general, will exit since there's nothing more to read. In your > example, you set up a sub-shell which runs 5 command, 3 sleeps and 2 > echos, once the sub-shell exits, telnet just sees and EOF and exits. > The control charater ^D means EOF, on a blank terminal that you don't > mind logging out of, try hitting Control-D and see what happens. It > will logout usually, that's what happened to telnet. 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