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/


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

Other options

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s