How can I obtain the (IPv4) addresses for all network interfaces using only proc? After some extensive investigation I've discovered the following:
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/fib_trie
holds the network topographyTo simply print the addresses of all adapters:
To determine the adapter of those addresses (a) consult the adapters' destination networks from
/proc/net/route
, (b) match those networks with the ones of/proc/net/fib_trie
and (c) print the corresponding /32 host addresses listed under those networks.Again no
python
unfortunately, but a quite awkybash
approach:output:
Known limitation:
This approach does not work reliably for host addresses that share the network with other host addresses. This loss of network uniqueness makes it impossible to determine the correct host address from fib_trie as the order of those addresses does not necessarily match the order of networks of route.
Having said that, I'm not sure why you would want multiple host addresses belonging to the same network in first place. So in most use cases this approach should work just fine.
There is no IPv4 analog of /proc/net/if_inet6
ifconfig does:
You'll get something like this: