nixCraft Poll

Topics

Execute Commands on Multiple Linux or UNIX Servers

Posted by Vivek Gite [Last updated: December 17, 2005]

This is Part I in a series on Execut Commands on Multiple Linux or UNIX Servers Simultaneously. The full series is Part I, Part II, and Part III. Some time it is necessary to execute commands on Multiple Linux or UNIX Servers, for example you would like to find out who is logged on and what they are doing on three Linux or UNIX boxes or better find out system utilization, disk space and much more. With the help of ssh you can easily setup such nice system.

SSH Setup

Admin Linux workstation -> Server # 1 with ssh
adm.my.com              -> server1.my.com

SSH client is a program for logging into a remote machine and for executing commands on a remote machine. If command is specified, command is executed on the remote host instead of a login shell.

$ ssh  user@server1.my.com  w

Above command will gather up all logged in users information. However if you put this command in script to gather information from three server as follows it will prompt for a password:

ssh user@server1.my.com w
ssh user@server2.my.com w
ssh user@server3.my.com w

To get rid of password you can setup ssh key based login. Once ssh keys are in place you can simply create a script as follows:

#!/bin/bash
# Linux/UNIX box with ssh key based login
SERVERS="192.168.1.1 192.168.1.2 192.168.1.3"
# SSH User name
USR="jadmin"

# Email 
SUBJECT="Server user login report"
EMAIL="admin@somewhere.com"
EMAILMESSAGE="/tmp/emailmessage.txt"

# create new file
>$EMAILMESSAGE

# connect each host and pull up user listing
for host in $SERVERS
do
echo "--------------------------------" >>$EMAILMESSAGE
echo "* HOST: $host " >>$EMAILMESSAGE
echo "--------------------------------" >>$EMAILMESSAGE
ssh $USR@$host w >> $EMAILMESSAGE
done

# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

You need to setup your hostname and email id and then execute shell script. This is very simple yet powerful method to execute commands simultaneously on multiple Linux/UNIX servers. If you are really interested to see application output then visit here, it was produced by this script (see our forum for more). This is just small script but you are only limited by your own imagination. Few more advanced tools do exist I will cover them some time later.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Anonymous Says:

    Why not use dsh:
    http://www.netfort.gr.jp/~dancer/software/dsh.html

  2. Michael Says:

    If you want to fully parallelize the script you can wrap the ssh command in parentheses in order to execute in a subshell, I believe. That way the script doesn’t wait for the program to finish. I use this to dynamically test all the workstations in my lab (~150) to see which ones are up and running, so I can generate a machine list for a distributed MPICH program.

  3. Anonymous Says:

    Very nice script. I use(d?) to manually write a “for” loop every time I need to run the same command on multiple hosts.
    I’ve just modified your sample script, declaring another variable (COMMAND=$@) to use instead of “w”, so I can run
    ./dsh cat /etc/hosts to get the content of all servers’ /etc/hosts in my mailbox.

  4. WikiGeeiki Says:

    Excellent article! Thank you.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.