在 Linux 上使用命令行 PHP 检查互联网连接
我在 Linux 上使用命令行 PHP 来打开蓝牙拨号连接,并且我需要一种快速的方法来检查互联网连接是否处于活动状态。嗯,不一定要脏,但要快。 :) 使用 exec
运行外部命令不是问题。
我正在考虑 ping 一些稳定的服务器(例如谷歌),但我想知道是否有更好的方法。也许检查 ifconfig
的输出?能够以明确的响应(例如“无法连接到服务器”、“已连接”)进行响应的命令自然是最好的。有想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想要不依赖外部服务器就能工作的东西,您可以检查默认路由。该命令将打印出默认路由的数量:
如果是
0
,则无法访问外界。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:
If it's
0
, you can't get to the outside world.如果您对使用 exec 感到满意,我会(检查您的互联网连接) ping 您的 ISP 的 DNS 服务器(通过 IP)
您也可以使用 fsockopen 尝试从 www.google.com 获取页面(wget/curl“场景”)。
If you're comfortable with using exec, I would (to check your internet connection) ping your ISP's DNS servers (by IP)
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').
我建议也使用ping..你可以使用类似的东西:
如果你决定使用wget,记得在你想要下载的页面的url前面加上http://,我建议添加“ -q -O -" 选项使 wget 不(尝试)保存到文件。
另一种方法是在 php 上使用curl/sockets,但我认为这不是您正在寻找的最快方法。
I suggest using ping too.. you could use something like:
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..
您可以使用 wget 到某些外部网络,并使用 timeout 参数 -W 来实现此类操作。如果超时后没有成功,则返回 1;如果在超时内成功,则返回 0。此外,正如您所建议的,ping 也可用于此类目的。
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.
互联网连接?是什么?请定义“互联网连接”!您想要检查用户是否连接到哪里?到你们公司的服务器?好吧,ping一下!或者去哪里?好吧,ping google.com,但这样你只会检查用户是否连接到谷歌的服务器,因为所有其他网站可能会被系统管理员禁止。
一般来说,网络路由是这样一个域,您无法检查您是否连接到某种“互联网”。您只能检查特定服务器。
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.