Get local network interface addresses using only proc?

后端 未结 9 581
旧时难觅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:09
    ip -j -o -4 addr show dev eth0 | jq .[1].addr_info[0].local
    
    0 讨论(0)
  • 2020-12-02 19:19

    There is no IPv4 analog of /proc/net/if_inet6

    ifconfig does:

    fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)
    ioctl(fd, SIOCGIFCONF, ...)
    

    You'll get something like this:

    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")}}}})
    
    0 讨论(0)
  • 2020-12-02 19:19
    ip addr show dev eth0 | grep "inet " | cut -d ' ' -f 6  | cut -f 1 -d '/'
    
    0 讨论(0)
提交回复
热议问题