Ila*_*ste 2 php linux networking
我在Linux上使用命令行PHP打开蓝牙拨号连接,我需要一个快速的方法来检查互联网连接是否有效.好吧,不一定要脏,但很快就会受到赞赏.:) exec
用于运行外部命令不是问题.
我正在考虑ping一些稳定的服务器(例如谷歌),但我想知道是否有更好的方法.也许检查输出ifconfig
?一个响应清晰响应的命令("无法连接到服务器","连接")自然是最好的.想法?
caf*_*caf 11
如果您想要在不依赖外部服务器的情况下工作,您可以检查默认路由.此命令将打印出默认路由的数量:
/sbin/route -n | grep -c '^0\.0\.0\.0'
Run Code Online (Sandbox Code Playgroud)
如果是的话0
,你无法进入外面的世界.
如果您对使用exec感到满意,我会(检查您的互联网连接)ping您的ISP的DNS服务器(通过IP)
exec("ping -c 1 $ip 2>&1", $output, $retval);
Run Code Online (Sandbox Code Playgroud)
您也可以通过使用fsockopen尝试从www.google.com(wget/curl'方案')获取页面,在纯PHP中执行此操作.
if(!fsockopen("www.google.com", 80)) {
echo "Could not open www.google.com, connection issues?";
}
Run Code Online (Sandbox Code Playgroud)