0

Testing multiple unix server Connectivity using ping command and send mail the hostname/ip-address of the servers if any hosts are unreachable

There is file host.txt which contain the list of the servers we want to ping for e.g

www.abc.domain01.com
www.abc.domain02.com
www.abc.domain03.com
www.abc.domain04.com 

in this case i'm considering there are total four servers ; but it can be 100 servers i know how to check the connectivity of multiple servers but i don't know how to get the mail if particular server(s) is unreachable MY CODE IS

[email protected]
for i in $(cat host.txt)
do 
      ping -c4 $i 

if [ $? -ne 0 ]
then 

echo "$i is unreachable"|mail -s "ping alert" $email 
    fi
done 

suppose server www.abc.domain01.com is unreachable so i want see the output in my mail like www.abc.domain01.com

4
  • possible duplicate of email from bash script Commented Jun 20, 2014 at 21:19
  • I don't see anything obviously wrong here. What is your evidence that it is not working? Does echo "test from my server" | mail -s "mailTest" [email protected] work? If not, then sendmail probably isn't work, or there are rules that are blocking your message or numerous other things. Real error messages are best in your question. Good luck. Commented Jun 20, 2014 at 22:08
  • It was the problem with mail , now it is working well . i have one doubts ... i don't want to display the output of the ping on screen .Once i run this script i just want alert in my email . where i should redirect the output , Please help me with this . Commented Jun 24, 2014 at 8:08
  • as you're testing ping status with $?, you can redirect ping output to /dev/null, i.e. ping -c4 $i 2>/dev/null. Check man ping. maybe your version has something like -s for silent. Commented Jun 24, 2014 at 15:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.