在 Linux 上使用命令行 PHP 检查互联网连接

发布于 2024-08-04 20:33:05 字数 241 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乖乖兔^ω^ 2024-08-11 20:33:05

如果您想要不依赖外部服务器就能工作的东西,您可以检查默认路由。该命令将打印出默认路由的数量:

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

如果是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:

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

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

讽刺将军 2024-08-11 20:33:05

如果您对使用 exec 感到满意,我会(检查您的互联网连接) ping 您的 ISP 的 DNS 服务器(通过 IP)

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

您也可以使用 fsockopen 尝试从 www.google.com 获取页面(wget/curl“场景”)。

if(!fsockopen("www.google.com", 80)) {
    echo "Could not open www.google.com, connection issues?";
}  

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?";
}  
祁梦 2024-08-11 20:33:05

我建议也使用ping..你可以使用类似的东西:

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

如果你决定使用wget,记得在你想要下载的页面的url前面加上http://,我建议添加“ -q -O -" 选项使 wget 不(尝试)保存到文件。

另一种方法是在 php 上使用curl/sockets,但我认为这不是您正在寻找的最快方法。

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

满身野味 2024-08-11 20:33:05

您可以使用 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.

花之痕靓丽 2024-08-11 20:33:05

互联网连接?是什么?请定义“互联网连接”!您想要检查用户是否连接到哪里?到你们公司的服务器?好吧,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文