Send one email with output result from 2 commands

Hi,

i want to create a script which should do the following:

1) ping the remote servers and email the hosts which are down
2) netstat on port x on 2 server and email the result too.

I want both results to be sent in the same email.

I have few ideas but i can't finish it.

Here is my idea:
#!/bin/bash
HOSTS="host1 host2"

# email report

SUBJECT="Ping failed"
EMAILID="jon@abc.com"

for myHost in $HOSTS
do
jj=$(ping $myHost -w2 | awk '/received/ {print $4}')
if [ $jj -eq 0 ]
then
# 100% failed
echo "Host : $myHost is down (ping failed) at $(date)" | mail -s $SUBJECT" $EMAILID
fi
done

#check netstat

net=$(netstat -na|grep 5005)

if ($net)
then
echo $net
else
ssh host2 $net
echo "The app is running on $HOST and is connected on $net"| mail -s "$SUBJECT" $EMAILID

My issues are:

1) i don't know how to send in one mail both result
2) i don't know how to check the netstat command on if ...then

Many thanks for your help

(echo "first echo"; echo "second echo") | mailx -s test jon@abc.com

Hi,
You can append all the output into one file and then send it with
cat /tmp/resultfile | mail -s "$SUBJECT" $EMAILID
and then remove it afterwards.

/Lakris

thanks guys :b:

Just a note, you can redirect the output of an entire block in bash to whatever you want.. Such as..

#!/bin/bash
{

# code here

} > somefile.txt

Of course, it doesnt have to go to somefile.txt, it can go wherever. :slight_smile: