Advertisement Header

Wednesday, 28 July 2010

How to Run Multiple commands Remotely Using Script in Linux/Unix

#!/bin/bash
# Linux/UNIX box with ssh key based login
SERVERS="Server1 server2 server3"
# SSH User name
USR="Admin"

# Email
SUBJECT="Server user login report"
EMAIL="sravan03ie29@hotmail.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
sudo ssh $USR@$host " hostname; df -h" >> $EMAILMESSAGE
done

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

No comments:

Post a Comment