Execute Commands on Multiple Linux or UNIX Servers

This source code is from- http://www.cyberciti.biz/tips/execute-commands-on-multiple-linux-or-unix-servers.html

You will need to setup SSH Share keys for this script – http://www.cyberciti.biz/tips/ssh-public-key-based-authentication-how-to.html

 

Bash for Loop Examples : http://www.cyberciti.biz/faq/bash-for-loop/

[sourcecode language=”bash”]

#!/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

&nbsp;

[/sourcecode]

Other options

Reference Article - http://www.cyberciti.biz/tips/execute-commands-on-multiple-linux-or-unix-servers-part-ii.html 

Leave a comment