Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Ping test sends mail when ping fails

Programming


Tags
bash script, loop, mail, ping, pingtest

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-23-2016   -   Original Discussion by mort3924
mort3924 mort3924 is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 24 November 2016, 3:29 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question Ping test sends mail when ping fails

help with bash script!

im am working on this script to make sure my server will stay online, so i made this script..


Code:
HOSTS="192.168.138.155"
COUNT=4

pingtest(){
  for myhost in "$@"
do
  ping -c "$COUNT" "$myhost" &&return 1
done
return 0
}
if pingtest $HOSTS

#100% failed
echo "server failed at $(date)" | mail -s "server is down" mymail..
echo "host ($HOSTS) is down (ping failed) at $(date)"
else
echo "everyting is fine"
fi

but its sending me a mail even when there is connection to the server, and i only want it when the ping fails..
i have made the script run every 5 min. with crontab -e 5 * * * * filelocation

can someone please help me with this?
sorry if this is the wrong place to ask..


Moderator's Comments:
Ping test sends mail when ping fails Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-23-2016 at 01:40 PM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 11-23-2016   -   Original Discussion by mort3924
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 November 2017, 2:20 PM EST
Location: Aachen, Germany
Posts: 11,638
Thanks: 320
Thanked 3,618 Times in 3,323 Posts
The return code of your function is the wrong way: 0 indicates success; 1 (better: anything but 0) indicates error.
And, there's a then missing for the if construct.
And, there might be a logic error: the for loop would work on a list of hosts and return success if any of them would ping.
Sponsored Links
    #3  
Old Unix and Linux 11-23-2016   -   Original Discussion by mort3924
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 November 2017, 11:18 PM EST
Location: NM
Posts: 11,239
Thanks: 571
Thanked 1,116 Times in 1,030 Posts
try:
change
Code:
if pingtest $HOSTS

to

Code:
pingtest $HOSTS
if [ $? -eq 1 ] ; then

You also are missing the "then" keyword.
    #4  
Old Unix and Linux 11-24-2016   -   Original Discussion by mort3924
mort3924 mort3924 is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 24 November 2016, 3:29 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, but ist still not working, it keeps sending mail, even when the ping is working
Sponsored Links
    #5  
Old Unix and Linux 11-24-2016   -   Original Discussion by mort3924
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 November 2017, 2:20 PM EST
Location: Aachen, Germany
Posts: 11,638
Thanks: 320
Thanked 3,618 Times in 3,323 Posts
Post the xtrace log (option -x) of your script running.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Ping test using python ctrld Shell Programming and Scripting 7 11-25-2015 02:52 AM
How to get reason for ping failure using perls Net::Ping->new("icmp");? tavanagh Shell Programming and Scripting 4 06-14-2013 10:27 AM
Animation Ping on Solaris Like Cisco Ping gokcell Shell Programming and Scripting 1 12-04-2011 05:12 AM
help: infinant loop script - host ping test zeekblack Shell Programming and Scripting 2 04-23-2007 07:11 AM
PERL: ping and e-mail perleo Shell Programming and Scripting 20 03-28-2003 02:37 PM



All times are GMT -4. The time now is 04:55 PM.