What's new

(Help Required) Run script only if able to ping Google

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

faria

Senior Member
(Solved) Run script only if able to ping Google

Hi Guys,
Basically i want the router to update the host file from my adblock but only to do so if it can ping Google if not then quit.

here is my current script:

Code:
#!/bin/sh

rm -f /tmp/adblock/hosts.downloaded
sh /jffs/scripts/adblock
the new script would follow this logic:
Code:
#!/bin/sh
# if ping google successful  then run the commands below, if not then quit.

rm -f /tmp/adblock/hosts.downloaded
sh /jffs/scripts/adblock

(note)the script is run by a cronjob.

Any help writing this script is most welcome.
 
Last edited:
Code:
#!/bin/sh

if ping -c 2 8.8.8.8 > /dev/null
then
	rm -f /tmp/adblock/hosts.downloaded
	sh /jffs/scripts/adblock
fi

8.8.8.8 is one of google's public DNS servers, so should be reliable. Also, using the IP address rather than its host name means you're not reliant on your local DNS service being available.
 
Last edited:
Code:
#!/bin/sh
# should work
ping -c 1 google.com
if  test "$?" == "0"
then
 echo "worked"
else
 echo "Failed"
fi
# should fail
ping -c 1 172.17.7.7
if  test "$?" == "0"
then
 echo "worked"
else
  echo "Failed"
fi
 
Thanks guys! Very much appreciated.

-c Xnumber means the number of pings tried, correct ?
 
Thanks guys! Very much appreciated.

-c Xnumber means the number of pings tried, correct ?
Correct. If any of the pings fail an error code is returned.

Code:
# ping
BusyBox v1.20.2 (2014-06-06 16:30:42 EDT) multi-call binary.

Usage: ping [OPTIONS] HOST

Send ICMP ECHO_REQUEST packets to network hosts

        -4,-6           Force IP or IPv6 name resolution
        -c CNT          Send only CNT pings
        -s SIZE         Send SIZE data bytes in packets (default:56)
        -t TTL          Set TTL
        -I IFACE/IP     Use interface or IP address as source
        -W SEC          Seconds to wait for the first response (default:10)
                        (after all -c CNT packets are sent)
        -w SEC          Seconds until ping exits (default:infinite)
                        (can exit earlier with -c CNT)
        -q              Quiet, only displays output at start
                        and when finished

#
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top