0

Good evening I would need a script in bash that follows this command: ping -c 4 -i "IP" for 3 times. Each ping must be performed at a distance of 10 minutes and only when it fails all 3 times, it send an e-mail. Could you help me?

Ex.

ping -c 4 -i X.X.X.X

Execution type: first ping, if all packet loss, wait 10 minutes and execute again ping command, if still failed execute again last ping and if failed, send an e-mail

#!/bin/bash
HOSTS="X.X.X.X"

pingtest(){
  for myHost in "$@"
  do
    ping -c 4 -i 5 $HOSTS && return 1
  done
  return 0
}

if pingtest $HOSTS
then
  # 100% failed
  echo "Server failed" | mail -s "Server Down" [email protected]

fi

but how can repeat it 3 times and only after it fails with packet loss, send an email? Thank you

5
  • So you want to ping 4 times 3 times? What constitutes a failure? What if 1 of the 4 packets sent in the first run succeed? Is that a success or a failure? What does a "distance of 10 minutes" mean?
    – jesse_b
    Jul 25, 2019 at 14:13
  • 2
    This is certainly not a "request for learning material"...it may be "too broad" though.
    – jesse_b
    Jul 25, 2019 at 14:15
  • Ping command must be run 3 times Failure is 100% packet loss Execution type: first ping, if all packet loss, wait 10 minutes and execute again ping command, if still failed execute again last ping and if failed, send an e-mail
    – Gianluca
    Jul 25, 2019 at 14:23
  • 2
    Ok, that's a number of things the script should do. Have you tried to implement any of them? Where have you succeeded and what issues have you faced?
    – ilkkachu
    Jul 25, 2019 at 14:59
  • 2
    Did you try to look for previous solutions online that could be modified to work in your case?
    – number9
    Jul 25, 2019 at 15:23

3 Answers 3

0

If using an external program is acceptable, you could use monitor-ip. It's written in C and rather configurable. Unlike a bash script that loops over and continually executes ping or a cron job, it can run 100s of pings a second while consuming less than 1% of CPU time.

For example, you could use something like this:

sudo ./monitor-ip --interval 5.0 --missed-max 20 --reset -- 1.2.3.4 \
        bash -c 'mail -s "Server Down!" [email protected] <<< "$MONITOR_NOTIFY_REMOTE_ADDRESS unreachable"'

This will send pings to 1.2.3.4 at an interval of 5 seconds until 20 consecutive pongs are not received (1 minute of downtime), then send an email to [email protected]. It would continue to send emails at 1 minute intervals until the status condition is resolved.

Full disclosure: I wrote monitor-ip.

0

Here is an example, how to get result from ping:

#!/bin/bash

HOST="X.X.X.X"
WAITFOR=5
TIMES=3

ping $HOST -c $TIMES -i $WAITFOR &> /dev/null
pingReturn=$?

if [ $pingReturn -eq 0 ]; then
    # It works
    echo "Success!!!"
    exit 0
else
    # No access
    echo "Fail"
    exit 1
fi

You can use your method for sending email instead of echo statements, that I have put in. You also have three variables HOST, TIMES, and WAITFOR, which you can set to your desired values. If you want 10 minutes between pings, you have to set WAITFOR to value 600.

-2

Below script works fine for below scenarios

 #!/bin/bash
    echo "enter the hostname or IP of the host"
    read h
    ping -c1 $h
    if [ $? != 0 ]
    then
    sleep 6
    ping -c1 $h
    if [ $? != 0 ]
    then
    sleep 6
    ping -c1 $h
    if [ $? != 0 ]
    then
    echo "host $h is not pinging  and its not reachable"
    mail -s "host $h is not pinging  and its not reachable" emailid </dev/null
    else
    echo "host $h is pinging"
    fi
    fi
    fi
1
  • You really should lay out your code readably with indentation, etc., particularly if you're trying to help someone else. Jan 3, 2023 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .