# [Solved] How to detect network is up and running?

## solamour

I'd like to find out whether the network connection to the outside world is up and running, preferably in a bash script. The network interface itself might be up, but that doesn't necessarily mean the outgoing traffic would correctly find its destination, so I can't rely on that alone.

I was thinking, perhaps I can 1) ping a certain site, such as "example.org", 2) get the return code ("$?"), and 3) if the return code is anything other than 0, then conclude there is a network problem.

Might work, but looks kinda crude. There might be a better way. I'd appreciate any suggestions.

__

solLast edited by solamour on Fri Sep 16, 2011 5:51 am; edited 2 times in total

----------

## Veldrin

This is basically how nagios (or any other monitoring tool) is doing it.

OTOH, using example.org uses already 2 services: dns and ping.

If you wanted just to test e.g internet connectivity, use an IP address. (Google DNS, L3 DNS are the usual suspects)

just my .02$

V.

----------

## chiefbag

Curious to know why you would need to make such a test?

If you were relying on using some other server for backups etc then you would run your test against that server.

If you were using your server to monitor itself for an incoming connection and then expect it to notify you then that would be pointless as it would have no way of notifying you due to lack of connectivity.

----------

## jfp

 *Quote:*   

> 'd like to find out whether the network connection to the outside world is up and running, preferably in a bash script. The network interface itself might be up, but that doesn't necessarily mean the outgoing traffic would correctly find its destination, so I can't rely on that alone. 

 

I was looking for a solution for the same problem. There are probably many possible answers, but two seem to stand out: dig (net-dns/bind-tools) and hostx (net-dns/host).

Dig seems to be very powerful, but its syntax is fairly mind bending...

hostx (yes there is an x in hostx) is much simpler and seems to do the job perfectly.

```
zippy ~ # hostx www.gentoo.org

www.gentoo.org          CNAME   www-bytemark-v4v6.gentoo.org

www-bytemark-v4v6.gentoo.org    A       89.16.167.134

zippy ~ # echo $?

0

zippy ~ # hostx www.ihopethisdoesnotexist.org

www.ihopethisdoesnotexist.org   A       208.68.143.50

 !!! www.ihopethisdoesnotexist.org A record has zero ttl

zippy ~ # echo $?

0

zippy ~ # #pull ethernet cable

zippy ~ # hostx www.ihopethisdoesnotexist.org

Nameserver not running

www.ihopethisdoesnotexist.org A record not found, try again

zippy ~ # echo $?

69

zippy ~ # #insert ethernet cable

zippy ~ # hostx www.ihopethisdoesnotexist.org

www.ihopethisdoesnotexist.org   A       208.68.143.50

 !!! www.ihopethisdoesnotexist.org A record has zero ttl

zippy ~ # echo $?

0

zippy ~ # #supress hostx output and just use $?

zippy ~ # hostx www.ihopethisdoesnotexist.org > /dev/null 2>&1

zippy ~ # echo $?

0

zippy ~ # 

```

I haven't written a script/function to make use of hostx yet, so I can't show a working example.

----------

## solamour

 *chiefbag wrote:*   

> Curious to know why you would need to make such a test?

 

This one particular app that I need to run upon booting doesn't seem to work if there is no network connection; if there is no network, it's supposed to keep trying until the network comes back, but it just gave up and exited. Perhaps I should look into its source code, but for now, I need to save that for another day.

Recently there was a city-wide complete blackout around where I live, and when the power eventually came back, the machine in question booted up faster than the rest of the network. I had to make a long trip to the remote site.

__

sol

----------

## solamour

The author of the said app replied with an environment variable that changes the app's behavior; "AUTOSSH_GATETIME=0" in net-misc/autossh was the answer. It was clearly described in the manual, and I certainly should have read it more carefully.

Anyhow, the problem is solved, but I did manage to write a rudimentary bash script to handle the issue, although it most likely won't win any beauty contests. I welcome all suggestions/criticism.

```
#!/bin/bash

done=false

until [ $done = true ]

do

   ping -c1 8.8.8.8 > /dev/null 2>&1

   if [ $? -eq 0 ]

   then

      # Network is available, so

      # do something interesting.

      done=true

   else

      sleep 3

   fi

done

```

__

sol

----------

