Operating System - HP-UX
1833013 Members
2717 Online
110048 Solutions
New Discussion

script to telnet remote machines

 
SOLVED
Go to solution
Karthik S S
Honored Contributor

script to telnet remote machines

Hi,

I need to write a script which does a telnet to the remotes systems specified and run some commands there. With in the script I must be able to supply the user name and password,

I tried something like this but it didnt work,

for i in "sys1 sys2 sys3"
do
telnet $i <root
password
`run some commands`
exit
EOF
done


Is there any way out?. I can't make use of rsh as the remote system does not support it.

Pl. help,

Thanks
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
11 REPLIES 11
Ollie R
Respected Contributor

Re: script to telnet remote machines

Hi Karthik,

You won't be able to do the telnet like this as the password can never be passed in this fashion in HP-UX.

A product called "expect" will let do that kind of thing but I don't know the product myself.

My personal suggestions are either to create a trust between the 2 systems so you can use "remsh" (not the most secure option) or install SSH on your systems and use "ssh" to run the commands (much more secure).

Ollie.
To err is human but to not award points is unforgivable
Michael Tully
Honored Contributor

Re: script to telnet remote machines

I'm not sure why you would do this. The way you have the script set up, you may as well have a .rhosts/hosts.equiv file. Why not run them on the local system and then ftp the log file afterwards.
Anyone for a Mutiny ?
Sridhar Bhaskarla
Honored Contributor
Solution

Re: script to telnet remote machines

Hi Karthik,

There are two major issues with your approach. Logging in directly as root and storing it's password in a text file. I suggest you use ssh for automated jobs.

If you are willing to take the risk, you can use 'expect' to achieve what you want. But you would need to understand it a bit before you can start on it.

Look at the following thread. I gave a simple example (in real world, you would be keeping a lot of error checking) on how expect can make interactive sessions non-interactive. There are examples available with expect that can help you understand it better.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x54e2c6af36b7d5118ff10090279cd0f9,00.html

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Karthik S S
Honored Contributor

Re: script to telnet remote machines

Thank you sridhar .. expect is the right thing I am looking for ...

Ollie/michael:
I need this because I need to take battery status every week from some 20 odd Sun T3 arrays which does not support any "r" commands. And I wanted to automate this using cron.

Thanks
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: script to telnet remote machines

Hi,

But how do I pass multiple hosts to expect for doing some common tasks?? If I call the expect script from within a shell script it doesnt take the environmental variables...

Thanks
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Michael Tully
Honored Contributor

Re: script to telnet remote machines

I run a variety of scripts on different systems, more or less looking for the same information. We don't use 'r' commands.

Why not run the same script on each server and just e-mail/ftp the results. Having the same script running from cron contains all the ENV variables we require and it is totally secure. That's why I asked why don't you ftp the results and then collate them on one system and post the results to yourself.
Anyone for a Mutiny ?
Nicolas Dumeige
Esteemed Contributor

Re: script to telnet remote machines

You can do pretty much what you wanted to do in the first place, of course with less reliability than expect but just using sh.
Take a look here :
http://steve-parker.org/sh/expct.shtml

Cheers
All different, all Unix
Chris Vail
Honored Contributor

Re: script to telnet remote machines

I'm with Sridhar here, and urge you to use scp and ssh instead of telnet, ftp, and the 'r' utilities (rsh {remsh}) and rcp). Telnet and ftp cannot easily be scripted: you can do a work-around with expect, but it IS a work-around and not a real solution to the problem.

Install secure shell and use scp and ssh. These are secure, they're easily scripted, and they're free. The only drawback is that they're a little slow on the initial connection while they swap public/private keys. But otherwise they're a dream to use, and they fundamentally solve your problem. Here's a quick-and-dirty example.

for HOST in HOST1 HOST2 HOST3 HOST4
do
ssh $HOST "Run command">>/tmp/txtfile
done
cat /tmp/txtfile|mailx -s "textfile" me@somewhere.com

Chris
Karthik S S
Honored Contributor

Re: script to telnet remote machines

Hi All,

As I have already told you I am using expect for collecting the battery status on Sun StorEdge T3 work group arrays which only supports telnet and ftp. Hence I cant use ssh on it ....

Thanks
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Denver Osborn
Honored Contributor

Re: script to telnet remote machines

Since it only supports telnet and ftp... and you've already seen that telnet doesn't seem to be a solution, use ftp instead. This can be put into a script.

SRVR="sys1 sys2 sys3"
for i in $SRVR
do
/bin/ftp -n $i | tee /tmp/$i-ftp.log <user username password
ascii
cd /path
get file_1
get file_2
..more ftp commands..etc..
quit
EOF
done

This may work for you. I wouldn't do this for root as you woudn't want roots passw being sent in clear text. I'd suggest creating a user account that you could lock down for this purpose.

My scripting skills are mediocre at best, so make your changes and test to suit your needs.


Hope this helps,
-denver
Tom Danzig
Honored Contributor

Re: script to telnet remote machines

Adding a "sleep 2" between each command will help. I've done this on telnet scripts before with good success. If it's not there, the feed of info to telnet will be too quick to allow it to respond properly.