This is Part I in a series - 'Execute 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.
Our Sample 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 firstname.lastname@example.org 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 email@example.com w
ssh firstname.lastname@example.org w
ssh email@example.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="firstname.lastname@example.org" 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.
- Continue reading second part: Run distributed commands on Linux/BSD/UNIX systems using tentakel tool.
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop