Get local network interface addresses using only proc?

后端 未结 9 785
旧时难觅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:01

    You may find the output of ip addr show easier to parse than output from other tools:

    $ ip addr show
    1: lo:  mtu 16436 qdisc noqueue state UNKNOWN
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:24:1d:ce:47:05 brd ff:ff:ff:ff:ff:ff
        inet 192.168.0.121/24 brd 192.168.0.255 scope global eth0
        inet6 fe80::224:1dff:fece:4705/64 scope link
           valid_lft forever preferred_lft forever
    3: eth1:  mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
        link/ether 00:24:1d:ce:35:d5 brd ff:ff:ff:ff:ff:ff
    4: virbr0:  mtu 1500 qdisc noqueue state UNKNOWN
        link/ether 92:e3:6c:08:1f:af brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
        inet6 fe80::90e3:6cff:fe08:1faf/64 scope link
           valid_lft forever preferred_lft forever
    

    Another option is the file /proc/net/tcp. It shows all currently-open TCP sessions, which is different than what you asked for, but might be Good Enough.

    $ cat tcp
      sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
       0: 00000000:0050 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 13536 1 ffff88019f0a1380 300 0 0 2 -1
       1: 00000000:1355 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 19877854 1 ffff880016e69380 300 0 0 2 -1
       2: 017AA8C0:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 13633 1 ffff88019f0a1a00 300 0 0 2 -1
       3: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 8971 1 ffff88019f0a0000 300 0 0 2 -1
       4: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 12952880 1 ffff880030e30680 300 0 0 2 -1
       5: 00000000:0539 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 14332 1 ffff88019f0a2080 300 0 0 2 -1
       6: 00000000:C000 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 14334 1 ffff88019f0a2700 300 0 0 2 -1
       7: 0100007F:0A44 00000000:0000 0A 00000000:00000000 00:00000000 00000000   119        0 51794804 1 ffff880016e6a700 300 0 0 2 -1
       8: 7900A8C0:B094 53D50E48:01BB 01 00000000:00000000 00:00000000 00000000  1000        0 64877487 1 ffff880100502080 23 4 16 4 -1
       9: 7900A8C0:9576 537F7D4A:01BB 06 00000000:00000000 03:00000E5D 00000000     0        0 0 3 ffff880100c84600
      10: 7900A8C0:CC84 0CC181AE:01BB 01 00000000:00000000 00:00000000 00000000  1000        0 61775908 1 ffff880198715480 35 4 11 4 -1
    $ irb
    irb(main):001:0> [0x79, 0x00, 0xa8, 0xc0]
    => [121, 0, 168, 192]
    

    My IP is 192.168.0.121; note the funny arithmetic to make it come out right. :)

提交回复
热议问题