tags:

views:

663

answers:

5

I'm using command-line PHP on linux to open bluetooth dialup connection, and I need a quick'n'dirty way to check if internet connection is active. Well, doesn't have to be dirty, but quick is appreciated. :) Using exec to run external commands is not a problem.

I'm thinking of pinging some stable server (e.g. google), but I'm wondering if there's some better way. Maybe checking output of ifconfig? A command that would respond with a clear response like ("cannot connect to server","connected") would naturally be best. Ideas?

+1  A: 

You can use wget to some external web with timeout parameter -W for such thing. It returns 1 if timeout elapsed without success, and 0 if it gets within timeout. Also, ping can be used for such purpose, as you suggested.

igustin
My `wget` doesn't seem to have option `-W`. Timeout is instead defined as `-T` Also, I how do I get it to return either 0 or 1, using `exec('wget www.google.com -T 1')` just returns an empty string.
Ilari Kajaste
Ups, sorry, yes - it's -T. I was looking at ping's options at that moment. :-(Try this: exec("wget www.google.com -T 1; echo $?").Also, I suggest to increase timeout to few seconds, to avoid false reports due to some delays or lags.
igustin
+3  A: 

If you're comfortable with using exec, I would (to check your internet connection) ping your ISP's DNS servers (by IP)

exec("ping -c 1 $ip 2>&1", $output, $retval);

You could probably also do it in pure PHP by using fsockopen to try and fetch a page from www.google.com (the wget/curl 'scenario').

if(!fsockopen("www.google.com", 80)) {
    echo "Could not open www.google.com, connection issues?";
}
ChristopheD
Ilari Kajaste
You can read all about it here: http://tldp.org/LDP/abs/html/io-redirection.html. Quicker explanation: http://osgeek.blogspot.com/2009/07/redirection-and-2.html
ChristopheD
@ChristopheD: Ah, ok. So it redirects stderr (2) to wherever stdout (1) is going (which in this case is PHP's `exec`). Thanks for the links!
Ilari Kajaste
+2  A: 

I suggest using ping too.. you could use something like:

exec("ping -c 4 www.google.com", $output, $status);
if ($status == 0) {
  // ping succeeded, we have connection working
} else {
  // ping failed, no connection
}

If you decide to use wget, remember to prepend http:// to the url of the page you want to download, and i suggest adding "-q -O -" to the options to make wget not (try to) saving to file..

Another way to do that is by using curl/sockets on php, but i think it's not the quickest way you are looking for..

redShadow
+7  A: 

If you want something that'll work without relying on an outside server, you can check for a default route. This command will print out the number of default routes:

/sbin/route -n | grep -c '^0\.0\.0\.0'

If it's 0, you can't get to the outside world.

caf
This is very interesting! This seems like a much better approach than accessing a server. I tried it, and it works great. Thanks!
Ilari Kajaste
A: 

Internet connection? What's it? Please, define "internet connection"! You want to check whether the user is connected--to where? To your company's server? Well, ping it! Or to where? Aright, ping google.com, but with that you'll only check whether the user's connected to google's server, since all other sites may be banned by system administrator.

Generally, network routing is such a domain where you can't check whether you're connected to some kind of "Internet". You can only check particular servers.

Pavel Shved
Yeah, yeah, I know that in theory internet connection nor intenet itself doesn't really exist. I just didn't want to write five paragraps of definition for my problem because it had a more simple way to explain the issue. In practice, this is a problem in only something like 0,00001% of cases. When I have a computer that's not connected, every one of us knows what it means to connect the computer to the "internet". Yes, I'm sure my accepted answer using `route` won't work in the cases when there's some crazy network trouble or access restrictions, but hey, I'll just settle with the 99,99999%.
Ilari Kajaste
I'd increase probability to 0.1%, and it can go quite nasty if it happens. Suppose the PC is connected to the Internet, but there's no connection (problems at ISP). *You will have to handle such situation anyway* (to let user see sane messages, offer "Work offline" so it stops futile attempts to connect to your server). But if you rely on routing information, you may fall into false sense that if routes are ok, the Internet is ok as well. Crashes, bug reports and lost data are guaranteed.
Pavel Shved
@Pavel: Ok, maybe one in a million was quite a bit extreme. :) But I think you misunderstand what I'm using this for, since you're talking about data loss and crashes. I would never *rely* on this method for any *specific* connection - it's just a crude, quick'n'dirty way to understand something general about the computer status, and I'm using it only for myself. Checking `route` helps determine if connection is active, and if a reconnection attempt is needed. It's reliable in most cases, and in the cases it isn't, I'll have to look at the problem a bit deeper anyway.
Ilari Kajaste
@Ilari Kajaste: ah, this way, it's cool then. But I was really surprised that noone has addressed the problem in the way I did. I just *had* to post an answer like that. I hope further readers will find our discussion valuable. :-)
Pavel Shved
@Pavel: Yes, it's indeed a good thing to have such detail in a high-profile site like this. I really wasn't thinking about less experienced users as audience when writing the question, and although the "quick'n'dirty" is pointing to right direction, you are indeed correct that the issue should be addressed so nobody gets the wrong idea. In fact, I'll probably revise the question a bit to take less experienced users into account as well.
Ilari Kajaste