OpenWrt Forum Archive

Topic: Help with ping script

The content of this topic has been archived on 7 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi guys, I understand very little about the command line so if someone can help me with a ping script I would be very grateful.

What you do is run a ping every time you get a reply command.

The spoiler scrpt performs an action if the ping fails, someone can tailor it to run only this action if the ping succeed (once ip 10.64.64.64)?

#!/bin/sh

tries=0
while [[ $tries -lt 5 ]]
do
    if /bin/ping -c 1 8.8.8.8 >/dev/null
    then
        exit 0
    fi
    tries=$((tries+1))
done

sleep 5
ifup wan3

With this I want to restart the wan3 for correct IP (sometimes the modem fails to get the real IP but initiated with standard IP).

Grateful for the attention!

Isn't the proper syntax in line 4 (without the double "[[" and "]]")?

while [ $tries -lt 5 ]

thanks for the reply but the change you recommended did not work , the script still runs action when the ping fails.

I wanted to try to use it for another purpose , run a command if ping response was positive to a specific IP .

Maybe I can reset my modem when the IPv4 is not detected correctly (sometimes fails to detect and has a standard IP in logs) .

(Last edited by murilo.xd on 5 Jul 2016, 20:53)

Okay, maybe I didn't understand your problem. What about changing line 6 to (adding the "!")?

if ! /bin/ping -c 1 8.8.8.8 >/dev/null
MagicSimon wrote:

Okay, maybe I didn't understand your problem. What about changing line 6 to (adding the "!")?

if ! /bin/ping -c 1 8.8.8.8 >/dev/null

Perfect! Thank you, that's exactly how I wanted!

Just a few final questions for possible future needs , the ping can be done to multiple addresses ( single positive result for execution ) ? can limit the ping test to a particular interface?

Example: ping 192.168.0.1 to the IP 192.168.0.120

Example: ping to "br-wan " or " 3g-wan3 "


Thank you for your attention and patience !

(Last edited by murilo.xd on 6 Jul 2016, 05:07)

murilo.xd wrote:

Just a few final questions for possible future needs , the ping can be done to multiple addresses ( single positive result for execution ) ? can limit the ping test to a particular interface?

Example: ping 192.168.0.1 to the IP 192.168.0.120

Example: ping to "br-wan " or " 3g-wan3 "

Question 1: Not that I am aware of. It sounds more like some script should do, e.g. sends serveral ping to different addresses and do a certain action, if at least one is successful.

Question 2: option "-I" should do that. From the ping manpage:

-I interface
              interface is either an address, or an interface name.  If interface is an address, it sets source address to specified
              interface  address.   If  interface in an interface name, it sets source interface to specified interface.  For ping6,
              when doing ping to a link-local scope address, link specification (by the '%'-notation  in  destination,  or  by  this
              option) is required.
MagicSimon wrote:
murilo.xd wrote:

Just a few final questions for possible future needs , the ping can be done to multiple addresses ( single positive result for execution ) ? can limit the ping test to a particular interface?

Example: ping 192.168.0.1 to the IP 192.168.0.120

Example: ping to "br-wan " or " 3g-wan3 "

Question 1: Not that I am aware of. It sounds more like some script should do, e.g. sends serveral ping to different addresses and do a certain action, if at least one is successful.

Question 2: option "-I" should do that. From the ping manpage:

-I interface
              interface is either an address, or an interface name.  If interface is an address, it sets source address to specified
              interface  address.   If  interface in an interface name, it sets source interface to specified interface.  For ping6,
              when doing ping to a link-local scope address, link specification (by the '%'-notation  in  destination,  or  by  this
              option) is required.

Thanks!!!

(Last edited by murilo.xd on 6 Jul 2016, 21:59)

The discussion might have continued from here.