I'm thinking that this needs to be changed to a while clause, at the moment it'll wait till all 10000 pings are done, I need it to return when the ping is successful. The program "say" is on OSX it makes the computer speak.
#!/bin/bash echo begin ping if ping -c 100000 8.8.8.8 | grep timeout; then echo `say timeout`; else echo `say the internet is back up`; fi
OK I don't have rights to answer my own question so here's my answer for it after playing around:
Thanks, yeah I didn't know about $? until now. Anyway now I've gone and made this. I like that yours doesn't go forever but in my situation I didn't need it to stop until it's finished.
#!/bin/bash intertube=0 echo "begin ping" while [ $intertube -ne 1 ]; do ping -c 3 google.com if [ $? -eq 0 ]; then echo "ping success"; say success intertube=1; else echo "fail ping" fi done echo "fin script"
You can use the ping command to test name resolution services, too. If you ping a destination by IP address, and the ping succeeds, you know you have basic connectivity. If you ping the same destination by hostname, and it fails, you know name resolution is not working.
Step 1: Open the Windows command prompt. One way of doing this is by entering the key combination Windows + R and enter the command CMD. Step 2: Enter the command line ping with the -t option and any address and confirm by clicking [Enter].
To execute this script, open a terminal and type './ping.sh host', where ping.sh is the Bash script and host is the first argument. For example run './ping.sh www.google.com' like shown in Figure 2. The output is shown in Figure 3.
Type "-t" after the IP address to run the ping continuously or " -n x", replacing x with the desired number of packets to be sent.
You probably shouldn't rely on textual output of a command to decide this, especially when the ping
command gives you a perfectly good return value:
The ping utility returns an exit status of zero if at least one response was heard from the specified host; a status of two if the transmission was successful but no responses were received; or another value from
<sysexits.h>
if an error occurred.
In other words, use something like:
((count = 60)) # Maximum number to try. while [[ $count -ne 0 ]] ; do ping -c 1 8.8.8.8 # Try once. rc=$? if [[ $rc -eq 0 ]] ; then ((count = 1)) # If okay, flag loop exit. else sleep 1 # Minimise network storm. fi ((count = count - 1)) # So we don't go forever. done if [[ $rc -eq 0 ]] ; then # Make final determination. echo `say The internet is back up.` else echo `say Timeout.` fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With