如何以编程方式获取 OSX 网络服务的连接状态?

发布于 2024-09-05 13:35:42 字数 554 浏览 6 评论 0原文

在 OS X 系统偏好设置中,当我单击“网络”时,我会看到“以太网”旁边有一个绿点,“AirPort”和“FireWire”旁边有一个红点。这是因为当我通过以太网访问网络和互联网时,我关闭了 AirPort 和 FireWire。

我需要以编程方式确定系统偏好设置中显示的网络服务中哪些有绿点,哪些有红点。对于以太网和 FireWire,显示的状态为“已连接”或“未连接”,对于 AirPort,显示的状态为“打开”或“关闭”。也许其他网络服务有其他状态标签。

我已经挑选了“/Library/Preferences/SystemConfiguration”中的所有 plist 文件,特别是“preferences.plist”和“NetworkInterfaces.plist”。我可以在那里获取各种信息,例如位置集、网络服务顺序、代理信息(这对我的任务也很重要),但我找不到如何确定给定网络服务是打开还是关闭的 -相当于显示绿点。

我还尝试过使用系统配置框架,特别是 SCNetworkConnectionGetStatus 函数,但我得到的只是无效的连接状态。

有谁知道如何实际检索此连接状态信息?

谢谢。

In the OS X System Preferences, when I click on 'Network' I see a green dot by 'Ethernet', and red dots by 'AirPort' and 'FireWire'. This is because I turned off AirPort and FireWire, as I access networks and the Internet via Ethernet.

I need to programmatically determine which of these network services displayed in System Preferences have green dots and which have red dots. For Ethernet and FireWire the displayed status is 'Connected' or 'Not Connected', and for AirPort the displayed status is 'On' or 'Off'. Perhaps other network services have other status labels.

I have picked through all the plist files in '/Library/Preferences/SystemConfiguration', particularly 'preferences.plist' and 'NetworkInterfaces.plist'. I can get all sorts of information there, such as the Location set, network service order, proxy information (which is also important to my task), but I cannot find how to determine whether a given network service is on or off--the equivalent of having the green dot displayed.

I have also tried using System Configuration framework, specifically the SCNetworkConnectionGetStatus function, but all I get are invalid connection statuses.

Does anyone know how to actually retrieve this connection status information?

Thanks.

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

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

发布评论

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

评论(1

嘿咻 2024-09-12 13:35:42

控制面板中的绿点和红点通常与 OSX 上 ifconfig 命令的输出相关。

通常,它可能因计算机而异,en0 是以太网端口,en1 是机场。 (至少在我使用的所有 Macbook pro 上)

看起来您可以使用 ifconfig -m 来获取有关支持的媒体的信息。

$ ifconfig -m en0
en0: flags=8863 mtu 1500
    ether (redacted)
    media: autoselect
    status: inactive
    supported media:
        media autoselect
        media 10baseT/UTP mediaopt half-duplex
        media 10baseT/UTP mediaopt full-duplex
        media 10baseT/UTP mediaopt full-duplex mediaopt hw-loopback
        media 10baseT/UTP mediaopt full-duplex mediaopt flow-control
        media 100baseTX mediaopt half-duplex
        media 100baseTX mediaopt full-duplex
        media 100baseTX mediaopt full-duplex mediaopt hw-loopback
        media 100baseTX mediaopt full-duplex mediaopt flow-control
        media 1000baseT mediaopt full-duplex
        media 1000baseT mediaopt full-duplex mediaopt hw-loopback
        media 1000baseT mediaopt full-duplex mediaopt flow-control
        media none

看到 10xxbaseT 通常是以太网的好兆头。

无线似乎没有报告任何支持的媒体。

ifconfig 的手册页包含大量信息,但可能有一些系统库用于以编程方式访问我不知道的这些信息,但这应该可以帮助您入门。

The green and red dots in the Control pannel generally correlate to the the output of the ifconfig command on OSX.

Typically and it can vary from computer to computer, en0 will be the ethernet port and en1 the airport. (At least on all the Macbook pros I use)

It looks like you can use ifconfig -m to get information about supported media.

$ ifconfig -m en0
en0: flags=8863 mtu 1500
    ether (redacted)
    media: autoselect
    status: inactive
    supported media:
        media autoselect
        media 10baseT/UTP mediaopt half-duplex
        media 10baseT/UTP mediaopt full-duplex
        media 10baseT/UTP mediaopt full-duplex mediaopt hw-loopback
        media 10baseT/UTP mediaopt full-duplex mediaopt flow-control
        media 100baseTX mediaopt half-duplex
        media 100baseTX mediaopt full-duplex
        media 100baseTX mediaopt full-duplex mediaopt hw-loopback
        media 100baseTX mediaopt full-duplex mediaopt flow-control
        media 1000baseT mediaopt full-duplex
        media 1000baseT mediaopt full-duplex mediaopt hw-loopback
        media 1000baseT mediaopt full-duplex mediaopt flow-control
        media none

Seeing 10xxbaseT is usually a good sign it's ethernet.

Wireless doesn't seem to report any supported media.

The man page for ifconfig has a ton of information but there may be some system libraries for programatically accessing this information that I'm unaware of but this should get you started.

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