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