如何在 Linux 中检查以太网?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4231507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check Ethernet in Linux?
提问by RAM812
I need the test case for Ethernet in Linux using C code to check eth0
. If eth0
is down, we enable the net then check if up and the test is passed.
我需要使用 C 代码检查 Linux 中以太网的测试用例eth0
。如果eth0
关闭,我们启用网络,然后检查是否开启并通过测试。
回答by Yann Ramin
The network interfaces can be seen in sysfs
: /sys/class/net/eth[x]
. There you can check the link, interface status, and more.
网络接口可以在sysfs
: 中看到/sys/class/net/eth[x]
。您可以在那里检查链接、接口状态等。
回答by jim mcnamara
You may want to take advantage of libudev to get around in /sys:
你可能想利用 libudev 来绕过 /sys:
http://www.signal11.us/oss/udev/
回答by loxxy
I simply check if an Ip address is assigned to the network card.
我只是检查是否为网卡分配了 IP 地址。
You can use something like this to check if lan is up in the given network card (say eth0) :
您可以使用类似这样的方法来检查给定网卡中的 lan 是否已启动(例如eth0):
/sbin/ifconfig eth0| grep 'inet addr:' | wc -l
This should simply return 0 or 1 based on whether or not an ip address is assigned to the nic.
根据是否为 nic 分配了 ip 地址,这应该简单地返回 0 或 1。
Also you may use Yann Ramin'smethod to list out all the nic's & perform the check.
您也可以使用Yann Ramin 的方法列出所有网卡并执行检查。
I missed out noticing you are looking for a c code. Maybe adding a tag would be good.
我错过了注意到您正在寻找 ac 代码。也许添加一个标签会很好。
Either ways, I think you can look at the same file (ifconfig) manually in c by reading it for an ip.
无论哪种方式,我认为您可以通过读取 ip 来手动在 c 中查看同一个文件(ifconfig)。
回答by TrueY
To check if the link is up, try something like this. It works without root privileges.
要检查链接是否已启动,请尝试这样的操作。它无需root权限即可工作。
#include <stdio.h> // printf
#include <string.h> // strncpy
//#include <sys/socket.h> // AF_INET
#include <sys/ioctl.h> // SIOCGIFFLAGS
#include <errno.h> // errno
#include <netinet/in.h> // IPPROTO_IP
#include <net/if.h> // IFF_*, ifreq
#define ERROR(fmt, ...) do { printf(fmt, __VA_ARGS__); return -1; } while(0)
int CheckLink(char *ifname) {
int state = -1;
int socId = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (socId < 0) ERROR("Socket failed. Errno = %d\n", errno);
struct ifreq if_req;
(void) strncpy(if_req.ifr_name, ifname, sizeof(if_req.ifr_name));
int rv = ioctl(socId, SIOCGIFFLAGS, &if_req);
close(socId);
if ( rv == -1) ERROR("Ioctl failed. Errno = %d\n", errno);
return (if_req.ifr_flags & IFF_UP) && (if_req.ifr_flags & IFF_RUNNING);
}
int main() {
printf("%d\n", CheckLink("eth0"));
}
If IFF_UP is set, it means interface is up (see ifup). If IFF_RUNNING is set then interface is plugged. I also tried using the ethtool ioctl call, but it failed when the gid was not root. But just for the log:
如果设置了 IFF_UP,则表示接口已启动(请参阅 ifup)。如果设置了 IFF_RUNNING,则接口被插入。我也尝试使用 ethtool ioctl 调用,但是当 gid 不是 root 时它失败了。但仅针对日志:
...
#include <asm/types.h> // __u32
#include <linux/ethtool.h> // ETHTOOL_GLINK
#include <linux/sockios.h> // SIOCETHTOOL
...
int CheckLink(char *ifname) {
...
struct ifreq if_req;
(void) strncpy( if_req.ifr_name, ifname, sizeof(if_req.ifr_name) );
struct ethtool_value edata;
edata.cmd = ETHTOOL_GLINK;
if_req.ifr_data = (char*) &edata;
int rv = ioctl(socId, SIOCETHTOOL, &if_req);
...
return !!edata.data;
}
回答by Subsentient
As this question is somewhat important, I'll add another answer despite its extreme age. You can read the contents of /sys/class/net/eth0/operstate which will simply contain the string "up\n" or "down\n".
由于这个问题有点重要,尽管它的年龄非常大,我还是会添加另一个答案。您可以读取 /sys/class/net/eth0/operstate 的内容,其中仅包含字符串“up\n”或“down\n”。