如何找到路由器提供给我的本地 IP?

如何找到路由器提供给我的本地 IP?

我知道我可以通过 GUI 获取路由器/交换机提供给我的本地 IP 地址:

网络设置 > 单击我的连接上的齿轮图标 > 详细信息

GUI 视图查看本地 IP 地址

我可以看到我的本地 IPv4 地址是192.168.0.33

现在我想通过脚本获取该信息,但诸如ifconfig或之类的命令ip addr show没有帮助,因为它们会发送大量不必要的信息,尤其是当我启动并运行大量 docker 网络时:

$ ifconfig 
br-22f8c9813546: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::42:6cff:fef9:ddd0  prefixlen 64  scopeid 0x20<link>
        ether 02:42:6c:f9:dd:d0  txqueuelen 0  (Ethernet)
        RX packets 10391  bytes 15285773 (15.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19709  bytes 2896606 (2.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-4ee9377a4fb8: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.27.0.1  netmask 255.255.0.0  broadcast 172.27.255.255
        ether 02:42:ac:d2:0b:19  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-5d61846f1fe4: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.16.1  netmask 255.255.240.0  broadcast 192.168.31.255
        ether 02:42:39:05:be:f5  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-63d1527ef2dd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.23.0.1  netmask 255.255.0.0  broadcast 172.23.255.255
        inet6 fe80::42:b4ff:fe53:65fb  prefixlen 64  scopeid 0x20<link>
        ether 02:42:b4:53:65:fb  txqueuelen 0  (Ethernet)
        RX packets 11  bytes 308 (308.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6308  bytes 1053677 (1.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-7f9030324082: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.24.0.1  netmask 255.255.0.0  broadcast 172.24.255.255
        inet6 fe80::42:f3ff:fef5:70f3  prefixlen 64  scopeid 0x20<link>
        ether 02:42:f3:f5:70:f3  txqueuelen 0  (Ethernet)
        RX packets 411  bytes 59490 (59.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7005  bytes 1115316 (1.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-ede87c785445: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.26.0.1  netmask 255.255.0.0  broadcast 172.26.255.255
        ether 02:42:4f:6e:2b:ac  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:32ff:fe09:c11d  prefixlen 64  scopeid 0x20<link>
        ether 02:42:32:09:c1:1d  txqueuelen 0  (Ethernet)
        RX packets 141965  bytes 8102281 (8.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 489342  bytes 834915500 (834.9 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.33  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::1bf5:8c15:d648:f3a2  prefixlen 64  scopeid 0x20<link>
        inet6 2a02:8108:41bf:c4b0:94ab:8d54:b734:1cb0  prefixlen 64  scopeid 0x0<global>
        inet6 2a02:8108:41bf:c4b0::3c00  prefixlen 128  scopeid 0x0<global>
        inet6 2a02:8108:41bf:c4b0:d1bb:4aa7:7cce:b458  prefixlen 64  scopeid 0x0<global>
        ether 00:2b:67:55:f0:7e  txqueuelen 1000  (Ethernet)
        RX packets 8392238  bytes 7753304474 (7.7 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3495895  bytes 829060783 (829.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 435614  bytes 34898994 (34.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 435614  bytes 34898994 (34.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth21dba4f: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::70b7:7dff:fef6:1a08  prefixlen 64  scopeid 0x20<link>
        ether 72:b7:7d:f6:1a:08  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 93  bytes 12013 (12.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth2353f27: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::7caa:faff:fee1:1aa8  prefixlen 64  scopeid 0x20<link>
        ether 7e:aa:fa:e1:1a:a8  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 106  bytes 13631 (13.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth6f6e69f: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::5f:eff:fead:28dd  prefixlen 64  scopeid 0x20<link>
        ether 02:5f:0e:ad:28:dd  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 90  bytes 11699 (11.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth70fd5c3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::6c48:28ff:feed:5680  prefixlen 64  scopeid 0x20<link>
        ether 6e:48:28:ed:56:80  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 92  bytes 11959 (11.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth78edec0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::98a5:e8ff:fe5b:a00  prefixlen 64  scopeid 0x20<link>
        ether 9a:a5:e8:5b:0a:00  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 90  bytes 11699 (11.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth82f3647: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::7021:5fff:fe12:b352  prefixlen 64  scopeid 0x20<link>
        ether 72:21:5f:12:b3:52  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 90  bytes 11699 (11.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethc53bf34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::1816:e4ff:fe6b:a52f  prefixlen 64  scopeid 0x20<link>
        ether 1a:16:e4:6b:a5:2f  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 106  bytes 13631 (13.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethff5b5ce: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::bc46:8eff:fe96:953f  prefixlen 64  scopeid 0x20<link>
        ether be:46:8e:96:95:3f  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 90  bytes 11699 (11.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.106  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::2c2e:15bf:db59:7079  prefixlen 64  scopeid 0x20<link>
        ether 80:30:49:50:7f:25  txqueuelen 1000  (Ethernet)
        RX packets 454632  bytes 104035107 (104.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 253268  bytes 36336390 (36.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

信息隐藏于:

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.33  netmask 255.255.255.0  broadcast 192.168.0.255

但我不知道如何正确过滤它。有什么更简洁的方法可以获取路由器/交换机提供给我的本地 IP 地址?

我想要以下输出:

$ find-my-local-ip
192.168.0.33

答案1

发布者Bruno Bronosky 谈 Unix 和 Linux

 ip route get 8.8.8.8 | sed -n '/src/{s/.*src *\([^ ]*\).*/\1/p;q}'

正在做我想做的事。

答案2

您可以使用 python3 程序打印计算机的 IP 地址。它将为您提供默认接口的 IP,并且不需要任何互联网连接。这最初由 fatal_error 在stackoverflow.com

import socket

def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        s.connect(('10.255.255.255', 1))
        IP = s.getsockname()[0]
    except Exception:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

print(get_ip())

get_ip.py将其保存在名为并运行的文件中

python3 get_ip.py

相关内容