drum
drum

Reputation: 5661

How to programmatically check for connection?

In Linux (Ubuntu), I want to programmatically check if there is Internet connection (or if eth0 is connected).

I'm doing this because I am writing a program that requires network connection on a system that is highly prone to lose connection.

So I was thinking maybe a script that I can run periodically to check.

Can you give me good suggestions?

Upvotes: 2

Views: 2021

Answers (3)

Justin.Wood
Justin.Wood

Reputation: 715

Here is a quick script that will accomplish what you want:

[email protected]
ping -c 5 8.8.8.8 >> /dev/null

if [ $? -eq  0 ]
    then
    echo "Able to reach internet!" | mail $EMAIL
else
    echo "Unable to reach internet!" | mail $EMAIL
fi

Obviosly you can change the mail to something else to do depending on what your goal is EDIT: to explain, this pings googles dns server to ensure you are connected and sends you an email one way or the other. The email part on failure will only work of course if you have a local email server on your network.

Upvotes: 6

paulsm4
paulsm4

Reputation: 121881

/sbin/ifconfig would be an excellent "get adapter status" command to script.

cron would be an excellent way to execute the script.

Upvotes: 1

I also suggest to ping or perhaps wget some distant server (preferably the one you want to connect to). The network could work well on the local campus, but not well on intercontinental connections (e.g. because some cables has been cut).

Upvotes: 0

Related Questions