Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash loop ping successful

Tags:

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" 
like image 713
edumike Avatar asked May 25 '11 02:05

edumike


People also ask

How do I know if my ping is successful?

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.

How do I run a ping command in a for loop?

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].

How do I ping in bash?

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.

How do I ping before response?

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.


1 Answers

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 
like image 100
paxdiablo Avatar answered Oct 28 '22 15:10

paxdiablo