在Linux上使用命令行PHP检查Internet连接 [英] Checking internet connection with command-line PHP on linux
问题描述
我正在Linux上使用命令行PHP来打开蓝牙拨号连接,并且我需要一种快捷的方法来检查Internet连接是否处于活动状态.好吧,不必脏,但很快就会受到赞赏.:)使用 exec
运行外部命令不是问题.
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.
我正在考虑ping一些稳定的服务器(例如google),但我想知道是否有更好的方法.也许检查 ifconfig
的输出?自然而然,最好的命令是响应明确的响应,例如(无法连接到服务器",已连接").想法?
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'
如果它是 0
,则无法进入外界.
If it's 0
, you can't get to the outside world.
这篇关于在Linux上使用命令行PHP检查Internet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!