Get local network interface addresses using only proc?

后端 未结 9 718
旧时难觅i
旧时难觅i 2020-12-02 18:21

How can I obtain the (IPv4) addresses for all network interfaces using only proc? After some extensive investigation I\'ve discovered the following:

9条回答
  •  一整个雨季
    2020-12-02 19:06

    /proc/net/fib_trie holds the network topography

    To simply print the addresses of all adapters:

    $ awk '/32 host/ { print f } {f=$2}' <<< "$(

    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 awky bash approach:

    #!/bin/bash
    
    ft_local=$(awk '$1=="Local:" {flag=1} flag' <<< "$(

    output:

    eth0:     192.168.0.5
              255.255.255.0
    
    lo:       127.0.0.1
              255.0.0.0
    
    wlan0:    192.168.1.14
              255.255.255.0
    

    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.

提交回复
热议问题