running interactive program from shell script

Timothy Luoma lists at
Sun Jan 30 20:57:11 PST 2005

>> 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!

Actually... I'm not... I just tried this from my machine, and to my 
utter surprise, it does work, at least from the commandline.

By which I mean it will work to login via telnet, and it will execute 
those commands.

> 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
> 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."

I can't fathom why it even works at all, so I certainly don't know why 
it doesn't work differently.

> 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.

ah.  Well, then the only thing I can think to suggest is seeing if they 
support rsh or any such.

> I realize there are many ways of accomplishing this, but I'm kind of 
> hung up on doing it "my way"  :)

I'm familiar w/ that fixation, but it may take less time to just bite 
the bullet and setup 'expect'

>>> #! /bin/sh
>>>     (sleep 3;
>>>     echo "password";
>>>     sleep 3;
>>>     echo "ls -la";
>>>     sleep 3;
>>>     ) | telnet -l user
>> 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 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!

Yes... you're right... this is very peculiar to me.... then again, I 
haven't used telnet in years.

My initial thought was to try and change 'ls -la' to 'sh -i' or 
something like that, to give you an interactive shell, but you're right 
again that it simply exits as soon as it is finished.

Again, in the time it's taken to think about all this, you probably 
could have installed expect and gotten it to work :-)


More information about the freebsd-questions mailing list