Get local network interface addresses using only proc?

后端 未结 9 773
旧时难觅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 18:57

    cat /proc/net/tcp

    Get the second column, with the heading "local_address", e.g. "CF00A8C0:0203"

    The part after ":" is a port number.

    From the rest use the last two (C0) as a hex number, e.g. C0 is 192, which is the start of the address in this example.

    Took the following into my notes a while ago, from some smart point in the net:

    The IP address is displayed as a little-endian four-byte hexadecimal number; that is, the least significant byte is listed first, so you'll need to reverse the order of the bytes to convert it to an IP address.

    The port number is a simple two-byte hexadecimal number.

提交回复
热议问题