在 Linux 上:如何以编程方式确定是否启用并插入了 NIC 接口? [英] On Linux: how can I programmatically determine if a NIC interface is enabled and plugged in?
问题描述
我想确定网卡是否已启用、启动和插入.基本上,我想知道网卡是否可以工作.我需要来自 C++ 程序的这些信息,并且希望在网络无法正常工作时显示错误消息.如果可能的话,我想避免使用 shell 命令来确定这些信息.
I want to determine if a network card is enabled, up, and plugged in. Basically, I want to know if the network card will work. I need this information from with a C++ program, and would like to display an error message when the network isn't working properly. If possible I would like to avoid using shell commands to determine this information.
推荐答案
你可以查看 /sys/class/net/eth0/operstate
其中 eth0 是你的接口,看看它是否启动.
You can look at /sys/class/net/eth0/operstate
where eth0 is your interface to see if it's up.
查看/sys/class/net/eth0/carrier
看是否有运营商.
虽然我猜想执行 ifconfig
和朋友会让你对 *BSDs 有更多的兼容性.
Though I guess executing ifconfig
and friends will give you more compatibility to *BSDs.
这篇关于在 Linux 上:如何以编程方式确定是否启用并插入了 NIC 接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!