仅使用 proc 获取本地网络接口地址? [英] Get local network interface addresses using only proc?
本文介绍了仅使用 proc 获取本地网络接口地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何仅使用 proc 获取所有网络接口的 (IPv4) 地址? 经过一些广泛的调查,我发现了以下内容:
How can I obtain the (IPv4) addresses for all network interfaces using only proc? After some extensive investigation I've discovered the following:
ifconfig
使用SIOCGIFADDR
,这需要打开套接字并预先了解所有接口名称.Linux 上的任何手册页中也没有记录.proc
包含/proc/net/dev
,但这是一个接口统计列表.proc
包含/proc/net/if_inet6
,这正是我需要的,但对于 IPv6.- 通常在
proc
中很容易找到接口,但很少使用实际地址,除非是某些连接的明确部分. - 有一个名为
getifaddrs
的系统调用,它非常类似于您希望在 Windows 中看到的神奇"功能.它也在 BSD 上实现.然而,它不是非常面向文本,这使得它很难在非 C 语言中使用.
ifconfig
makes use ofSIOCGIFADDR
, which requires open sockets and advance knowledge of all the interface names. It also isn't documented in any manual pages on Linux.proc
contains/proc/net/dev
, but this is a list of interface statistics.proc
contains/proc/net/if_inet6
, which is exactly what I need but for IPv6.- Generally interfaces are easy to find in
proc
, but actual addresses are very rarely used except where explicitly part of some connection. - There's a system call called
getifaddrs
, which is very much a "magical" function you'd expect to see in Windows. It's also implemented on BSD. However it's not very text-oriented, which makes it difficult to use from non-C languages.
推荐答案
没有/proc/net/if_inet6 的 IPv4 模拟
There is no IPv4 analog of /proc/net/if_inet6
ifconfig 可以:
ifconfig does:
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)
ioctl(fd, SIOCGIFCONF, ...)
你会得到这样的东西:
ioctl(4, SIOCGIFCONF, {120, {{"lo", {AF_INET, inet_addr("127.0.0.1")}}, {"eth0", {AF_INET, inet_addr("10.6.23.69")}}, {"tun0", {AF_INET, inet_addr("10.253.10.151")}}}})
这篇关于仅使用 proc 获取本地网络接口地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文