查找未运行的网络接口
如果我运行以下代码,它只会打印处于 RUNNING 状态的接口。有没有办法获取未运行且可能处于运行状态或处于关闭状态的接口列表?
int main()
{
struct ifreq *pIfr;
struct ifconf ifc;
char buf[1024];
int s, i, j;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s==-1)
return -1;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ioctl(s, SIOCGIFCONF, &ifc);
pIfr = ifc.ifc_req;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; pIfr++) {
printf("name=%s\n", pIfr->ifr_name);
}
close(s);
return 0;
}
~
If I run the following code, it only prints interfaces that are in the RUNNING state. Is there a way to get a list of interfaces that are not RUNNING and could be either UP or DOWN?
int main()
{
struct ifreq *pIfr;
struct ifconf ifc;
char buf[1024];
int s, i, j;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s==-1)
return -1;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ioctl(s, SIOCGIFCONF, &ifc);
pIfr = ifc.ifc_req;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; pIfr++) {
printf("name=%s\n", pIfr->ifr_name);
}
close(s);
return 0;
}
~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
check man netdevice -- " 内核用当前正在运行的所有 L3 接口地址填充 ifreqs: "
如果接口未运行,则地址未明确定义...但您可以获得名称:
" 接口的名称无法通过 /proc/net/dev 找到任何地址或未设置 IFF_RUNNING 标志的地址。”
check man netdevice -- " The kernel fills the ifreqs with all current L3 interface addresses that are running: "
address is not well-defined if the interface is not running ... but you can get the names:
" The names of interfaces with no addresses or that don’t have the IFF_RUNNING flag set can be found via /proc/net/dev."
看起来像带有 SIOCGIFNAME 的 ioctl 循环返回所有接口。输入是索引,调用返回接口名称。
Looks like an ioctl loop with SIOCGIFNAME returns all interfaces. The input is an index, and the call returns the interface name.