On Linux: how can I programmatically determine if a NIC interface is enabled and plugged in?

前端 未结 5 1053
旧巷少年郎
旧巷少年郎 2021-02-05 11:51

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, a

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 12:14

    You can look at /sys/class/net/eth0/operstate where eth0 is your interface to see if it's up.

    Look at /sys/class/net/eth0/carrier to see if there is a carrier.

    Though I guess executing ifconfig and friends will give you more compatibility to *BSDs.

提交回复
热议问题