Operating System - HP-UX
1833851 Members
2245 Online
110063 Solutions
New Discussion

Re: how to log in to different servers via a script?

 
wim merckx
Occasional Contributor

how to log in to different servers via a script?

Hello,

I'm currently writing a script that should log into different servers and execute some unix commands. I know how to do this (logging in I mean)with ftp, but since I need to execute unix commands, this is of no use.

So, the general structure of my script should be something like this:

1) give username & passwords (this has to be only once, since we have one general password for all servers)

2) login to first server from a file or something (for $servername in $serverlist do ...)

3) execute some (a lot) unix commands

4) log in to next server from the serverlist

5) execute commands

etc etc ...

Can somebody please help me since this would make my daily routine much more easier :-)

Cheers

Wim

8 REPLIES 8
Ken Hubnik_2
Honored Contributor

Re: how to log in to different servers via a script?

You need to use the rlogin commands. You can use remsh to execute what you need to run on each server. Look at the man pages for rlogin and remsh.
Pete Randall
Outstanding Contributor

Re: how to log in to different servers via a script?

I would be leary of putting usernames and passwords in a script. Aside from the security issues, you also introduce maintenance issues when users/passwords change. You could look into setting up /etc/hosts.equiv and/or ~/.rhosts to enable the "r" commands, then simply do a "remsh hostname command".

Pete

Pete
Darrell Allen
Honored Contributor

Re: how to log in to different servers via a script?

Hi Wim,

You could use remsh or ssh to run the commands on the remote server. The basic process could be:

create a script of the commands you want to run on the remote systems
rcp the script to the remote systems
remsh (execute) the script on the remote systems

You can do the same procedure using ssh.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
John Dvorchak
Honored Contributor

Re: how to log in to different servers via a script?

Here is a script that I wrote that parses a file /usr/local/etc/rrun.conf for system names. Then executes any command given on the command line i.e.:

rrun 'bdf |grep "[890][0-9]% "'

or

rrun 'uptime'

#!/usr/bin/ksh
USAGE="usage:rrun 'command' < i.e, uname -a or who -r >"

# Get command to run from command line

COMMAND=$*

# Check to see if it is blank

if [[ $COMMAND = "" ]]
then
print $USAGE
exit 1
fi

# Check for rm, mv, shutdown and other nasty commands and shut it down
# Enter your list of restricted commands below

if [[ "$COMMAND" = +(*rm*||*mv*||*reboot*||*shutdown*||*init*) ]]
then
echo
echo "******************************************"
echo
echo "Your command: $COMMAND"
echo "Will not allow rm, mv, init, reboot or shutdown!"
echo
echo "******************************************"

exit 1
fi

# If the command is not blank and not nasty then do it

for i in `cat /usr/local/etc/rrun.conf` #Name of file containing system names
do
print "************************ $i ***************************"
# print "Running the following command on $i:$COMMAND"
print $COMMAND
remsh $i -n $COMMAND
# print "***********************************************************"
print
done
If it has wheels or a skirt, you can't afford it.
Balaji N
Honored Contributor

Re: how to log in to different servers via a script?

Hi,

Expect is your friend. Expect helps you automate interactive shell sessions.

I have been using a expect script to login to a server. The passwords i have are autogenerated by a script and are shared among users. So, i have a script which reads the passwords from a file and logs in to the server and spawns a shell for me.

If u need, u instead of spawning a shell, u could give the set of commands and using another script call this one for n no of times, one for each host.

A snippet of the script is attached.

HTH
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Mihails Nikitins
Super Advisor

Re: how to log in to different servers via a script?

Hi,

1. r-commands are simple but they are always security issue. Please think about your security requirements.

2. EXPECT really works, but if you know PERL, just use special modules for telnet, ftp, ssh and whatever you want. (http://www.cpan.org)
I think that it's enaugh to know shell and PERL langauages to do any administrative tasks.

3. Some alternative solutions for your task (sure with their own drawbacks)

- running scripts locally and retrieving results by ftp
- running scripts locally and placing results on NFS share
- storing scripts on NFS share, running scripts from NFS share (with possible parameters), storing results on NFS share.

Just my 2 cents.

BR,
Mihail
KISS - Keep It Simple Stupid
Anonymous
Not applicable

Re: how to log in to different servers via a script?

well, being aware of that this is not what you've asked for...
you may want to have a look at Service Control Mgr (free download) in case you have to look after [ $N -ge 10 ] boxes.

"Service Control Manager makes
managing a large number of systems much easier. A nice feature of SCM is
being able to do everything from either the command line, a GUI interface,
or a browser window. Service Control Manager is similar to SUDO, but it can
be leverage across many systems. Any user can be given access to selected
root-privileged commands without being given the root password. " (itrc training ad)

The so called tools are pretty cool- you can eg push the creation of a local user with a defined password (kind of nis without the nw load;)

to get started see
http://www.software.hp.com/products/SCMGR/index.html
http://www.software.hp.com/products/SCMGR/downloads.html
ServiceControl Manager is a software product that allows you to manage
groups of HP-UX systems from a central server. It includes integrated
products that provide centralized management for installation,
configuration, inventory and other administrative tasks. $0.0 Free
Download
http://www.software.hp.com/products/SCMGR/info.html
Release Notes 2.5 (v. A.02.05)
README [ 1020.txt ] [ 1100.txt ] [ 11i.txt ]
Technical Reference 2.5 (v. A.02.05)
Installation Guide 2.5 (v. A.02.05)
Browsable Help System
--whitepaper on
http://www.hp.com/products1/unix/management/infolibrary/index.html#configuration :
http://www.hp.com/products1/unix/management/infolibrary/scmwp.pdf (33pages)
Tony Lowrie
Advisor

Re: how to log in to different servers via a script?

In its simplist form :-)
----------------------------------
#!/usr/bin/sh
command=`ll`
sysadm=/usr/local/bin


for server in server1 server2 server3
do
remsh $server "$command $sysadm"
done
---------------------------------
should execute ll /usr/local/bin : on server 1,2,3.

Hope this helps.
Life is like MTB - Ups and Downs - and Adrenalin