running interactive program from shell script

Jay Moore jaymo at cromagnon.cullmail.com
Sun Jan 30 20:24:38 PST 2005


On Sunday 30 January 2005 05:11 pm, Timothy Luoma 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?
>
> Well, you'll have to give a little more information about what you're
> trying to do, because "initiating a telnet session" to me means
> "starting telnet" but it looks (if I am correctly interpreting/guessing
> what you are trying to do below) like you are trying to create a shell
> script which will start a telnet session AND automatically log you in.

Precisely...

> > 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.
>
> Yes, if you want to automatically log yourself in via telnet, then you
> will need expect.  There's no way to do this via /bin/sh

A-a-a-r-r-r-g-h-h.  You are correct! But what I still don't understand is why 
I can start and maintain a telnet session from a shell script, but if I add a 
couple of lines to send the password the damn thing terminates. In other 
words:

#! /bin/sh
telnet -l user 192.168.0.2

will start an "interactive" telnet session which will run until I enter 
'exit'. 

BUT - if I embellish it just a bit (as in the script below) to echo the 
password and ls -la the remote directory it exits as soon as the script 
finishes. 

As the bank robber in 'Dirty Harry' said, "I gots to know."

As for what I'm trying to do:
I have a requirement to administer a number of remotely located embedded 
devices; these devices do not support ssh - only telnet. To avoid the obvious 
security issues, I am going to co-locate a "real" computer at each remote 
location. I will ssh into the "real" computer, and then telnet over a local 
network to the embedded device(s). I realize there are many ways of 
accomplishing this, but I'm kind of hung up on doing it "my way"  :)

>
> > #! /bin/sh
> >
> >     (sleep 3;
> >     echo "password";
> >     sleep 3;
> >     echo "ls -la";
> >     sleep 3;
> >     ) | telnet -l user 192.168.0.2
>
> Ok, so this says: "Wait 3 seconds, echo 'password' to stdout, wait 3
> seconds, then show the output of 'ls -la' then wait 3 seconds and then
> send the output to telnet [which will, as far as I know, completely
> ignore everything you send to it] and then telnet to 192.168.0.2 as
> user 'user'

No - not exactly. You have the sequence correct, but the results are that the 
password is sent to the remote telnet host, it does log me in, it performs 
the ls -la (displaying the result on my local console), and then exits! 

Puzzledly,
Jay


More information about the freebsd-questions mailing list