Single Blog

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=”[email protected]
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

Comments (0)

Post a Comment