嵌入式 Linux 设备检测互联网连接的智能方法

发布于 2024-08-05 12:36:47 字数 220 浏览 3 评论 0原文

我们的团队正在开发基于Linux 2.6 的互联网媒体设备。目前,我们通过 ping www.google.com 来检测是否建立了互联网连接(通过有线以太网 i/f)。

我们测试设备的某些网络不支持 ICMP 数据包转发,因此我们的应用程序代码错误地将互联网报告为不可用。案件。

有谁知道一种更精确的方法来推断互联网连接是否可以通过 /dev/eth0 获得,而无需诉诸 ping 知名服务?

Our team is developing a Internet Media device based on Linux 2.6. Currently we detect whether Internet connectivity is established (via a wired Ethernet i/f) by pinging www.google.com

Some networks we have tested the device on do not support ICMP packet forwarding so our application code mistakenly reports the Internet as unavailable in this case.

Does anyone know of a more refined approach to deducing whether Internet connectivity is available through /dev/eth0 without resorting to pinging a well-known service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一曲爱恨情仇 2024-08-12 12:36:47

作为撰写 ChristopheD 引用的答案的人,这不是我在这里使用的方法。它适用于另一个问题,因为在这种情况下,我们正在检查当前计算机是否存在直接 PPP 链路 - 在这种情况下,您连接到一个任意网络,该网络可能有也可能没有默认路由,独立于更广泛的互联网连接。

由于您的应用程序需要全球 DNS 连接,因此我会检查这一点 - 查找您知道始终存在的地址 - 例如 com.NS 类型查询。领域。使用相当长的超时和/或在放弃之前重试几次。像这样的事情:

dig NS +aaonly com.

忽略输出并测试退出值 - 0 表示查找能够联系根服务器,其他任何值都不能联系。

As the guy who wrote that answer referenced by ChristopheD, that's not the approach I'd use here. It worked for the other question, because in that case we were checking for the presence of a direct PPP link from the current machine - in this case, you're connected to an abitrary network which may or may not already have a default route, independent of its wider internet connectivity.

Since you need global DNS connectivity for your app, I'd check for that - look up an address you know will always exist - like an NS type query for the com. domain. Use a reasonably long timeout and/or retry a few times before giving up. Something like this:

dig NS +aaonly com.

Ignore the output and test the exit value - 0 indicates the lookup was able to contact the root servers, anything else and it wasn't.

陪你到最终 2024-08-12 12:36:47

最终您希望设备能够使用互联网来实现某些有用的功能。如果设备通常连接到一个明确定义的服务器,那么向该服务器发送查询将会很有用。

在“无”和“有用的功能”之间有很多步骤,您可以检查其中任何一个作为“连接性”的某种衡量标准:

  • 插入以太网电缆(或建立 PPP 链接,或 Wi-Fi,无论您的低层)
  • DHCP 服务器分配的地址(如果适用)
  • 默认网关正在运行
  • DNS 服务器正在响应
  • 目标服务器正在响应(可能不是 ICMP ping,而是一个小型 HTTP 查询或您可能使用的任何其他协议)

取决于您的客户的精明程度,或者如果您想要一个有用的错误/诊断,他们可以将其报告给技术支持,那么检测所有这些可能会很有用。

能够将跟踪路由作为诊断工具也可能很有用。

Ultimately you want the device to be able to use the Internet for some useful function. If there's a well-defined server that the device commonly connects to, then it would be useful to send a query to that.

In between "nothing" and "useful function" there are many steps, and you could potentially check any of those as some measure of "connectivity":

  • Ethernet cable plugged in (or PPP link established, or Wi-Fi, whatever is your low layer)
  • DHCP server assigned address (if applicable)
  • Default gateway is functioning
  • DNS server is responding
  • Destination server is responding (maybe not an ICMP ping, but a small HTTP query or whatever other protocol you might be using)

Depending in how savvy your customer is, or if you want a useful error/diagnostic that they can report to tech support, it could be useful detecting all of those.

Might also be useful being able to do a traceroute as a diagnostic tool.

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