仅使用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
的系统调用,这是一个非常神奇"的功能, d希望在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屋!
查看全文