Operating System - HP-UX
1833758 Members
2694 Online
110063 Solutions
New Discussion

Telneting within a script

 
Philip Lunney
Occasional Advisor

Telneting within a script

I have a script to telnet from one server to another...looks something like this:

#!/bin/ksh
REMOTE=server2
(sleep 1
echo username
sleep 2
echo password) | telnet $REMOTE

It works between some servers but not all and when it fails it kicks you out with the message " Connection closed by foreign host."

You can telnet normally between the servers but for some reason it doesn't like my script. Anyone know of any settings that might be preventing this??

Thanks Barbara.
14 REPLIES 14
Deepak Extross
Honored Contributor

Re: Telneting within a script

Just a shot in the dark, but try increasing the sleep period. Maybe it takes a little longer for some machines to prompt for the username / password.
And consider using Expect. Quite a handy tool for such stuff.
Philip Lunney
Occasional Advisor

Re: Telneting within a script

Deepak

Thanks but I tried increasing the sleep but it doesn't even seem to get as far as the password, it returns the connection closed straight away! Any other ideas?

Also what is Expect??
U.SivaKumar_2
Honored Contributor

Re: Telneting within a script

hi,
i think the problem is with carriage return
try putting ; at the end of all your script
shell commands.

regards,
U.SivaKumar
Innovations are made when conventions are broken
Paula J Frazer-Campbell
Honored Contributor

Re: Telneting within a script

HI

Run it

ksh -x ./<script name>

and watch it run, it may give you and indication as to where the problem is.

Paula
If you can spell SysAdmin then you is one - anon
Deepak Extross
Honored Contributor

Re: Telneting within a script

Barbara,

See http://expect.nist.gov/

Does the account get locked after a few failed logins attempts, by any chance?
Volker Borowski
Honored Contributor

Re: Telneting within a script

Hi,

I found that this works with HPUX (at least 10.20 and 11.00) but i.e. not with a linux telnet.

Obviously it depends on the security thinking of the programmer. Now linux guys are hardliners in this case, so I found in linux programs that they flush the input stream before they ask for a password (to avoid, that you mistype something and the password appears in cleartext on the screen).

Such a program will never be able to be scripted in this way, because the entire pipe stream is flushed. HP-UX is more tolerant with this, but I do not know if this has been changed in 11.11 or so.

Hope this helps
Volker
Frank Slootweg
Honored Contributor

Re: Telneting within a script

The problem is (non) handshaking/timing. I.e. normally when you use telnet interactively, you *wait* for a certain prompt/reaction and *then* give the next command. In a script that does not happen, because the script does not wait. You can *try* to workaround that with sleeps, but, *if* it works at all, such scripts will fail at some time, often with obscure results. I have had such scripts which failed *years* after working 'correctly'.

The solution is the mentioned "expect" program, which was especially designed for problems like this.
Craig Rants
Honored Contributor

Re: Telneting within a script

I agree with Frank, expect is usually the way to go with telnet and ftp connections in scripts. I personally would look at setting up secure shell trusts, thus protecting your password from running in the clear and negating the use of expect.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
A. Clay Stephenson
Acclaimed Contributor

Re: Telneting within a script

I don't do this in the shell anymore at all. Instead I use the Perl Net::Telnet which is available from www.perl.org/CPAN.

Here is a simple example extracted from the man pages:



use Net::Telnet ();
my $username = "Clay";
my $passwd = "TOPSECRET";
my $remote_host = "bugs";
$t = new Net::Telnet (Timeout => 30,
Prompt => '/sh\$ $/');
$t->open($remote_host);
$t->login($username,$passwd);
@lines = $t->cmd("/usr/bin/ls -l /etc");
print @lines;

The beauty of this method is that it becomes trivially easy to add error checking.







If it ain't broke, I can fix that.
Mark Greene_1
Honored Contributor

Re: Telneting within a script

the problem is telnet is designed to want a tty on the originating end of the connection. You can try wrapping this around your script:

mknod pipe p
executable_file < pipe | telnet nnn.nnn.nnn.nnn > pipe 2>telnet.log
rm pipe

HTH
mark
the future will be a lot like now, only later
Ron Kinner
Honored Contributor

Re: Telneting within a script

This is from a script that I use to talk to a 3Com switch. I don't need the \r to talk to a Cisco router but the 3Com seems to require the \r after each line in order to work. You also may need a little extra sleep time at the beginning to make sure the connection is up before you try to logon. The first empty echo is required by the 3Com to wake it up.

(sleep 5
echo "\r"
sleep 1
echo "login\r"
sleep 1
echo "password\r"
sleep 2
echo "\r") |telnet $REMOTE

Ron
Ryan Moore
Advisor

Re: Telneting within a script

Try,

#!/bin/ksh
REMOTE=server2
(sleep 1
echo username
sleep 2
echo password
sleep 5) | telnet $REMOTE

Your subshell was ending and dropping the telnet connection.

Hope this helps,

rhino
Vincent Fleming
Honored Contributor

Re: Telneting within a script

I like my old shell trick... it doesn't require any sleeps at all:


telnet $server <
No matter where you go, there you are.
Ryan Moore
Advisor

Re: Telneting within a script

I tried this on Solaris 8 in bourne shell and Linux 7.0 with bash and bourne shell and this does not work for me.

What system and shell did you use to make this work?

Thanks,

rhino