Java InetAddress.getLocalHost().getHostAddress() returns IP 无效
Java InetAddress.getLocalHost().getHostAddress() returns invalid IP
当我说无效时,我的意思是无效。我得到一个与我机器接口的 any 无关的 IP 地址。我知道网站上有类似的问题,但更多的是关于人们获取他们不想要的接口地址(例如,127.0.0.1 而不是 LAN IP),但事实并非如此。我想知道这个 IP 是从哪里来的,我该如何摆脱它。
每当我调用 InetAddress.getLocalHost().getHostAddress()
,我得到 10.65.90.34。这不是从我的路由器、我的 ISP、我的 VPN 等任何东西分配的 IP。 ifconfig
的输出中没有提到这个 IP。我不知道这个IP来自哪里。有人建议在不同的问题上枚举所有接口,这是我这样做时得到的结果:
public void localIPTest() throws UnknownHostException, SocketException {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
System.out.println(hostAddress);
Enumeration Interfaces = NetworkInterface.getNetworkInterfaces();
while (Interfaces.hasMoreElements()) {
NetworkInterface Interface = (NetworkInterface) Interfaces.nextElement();
System.out.println(Interface.getDisplayName());
Enumeration Addresses = Interface.getInetAddresses();
while (Addresses.hasMoreElements()) {
InetAddress Address = (InetAddress) Addresses.nextElement();
System.out.println(Address.getHostAddress());
}
}
}
这输出:
10.65.90.34
utun0
fe80:0:0:0:a3c9:7561:3c37:3943%utun0
awdl0
fe80:0:0:0:b458:bbff:feac:2ee8%awdl0
utun1
172.20.97.60
en0
fe80:0:0:0:c88:b30a:16fc:4dbe%en0
192.168.0.106
lo0
fe80:0:0:0:0:0:0:1%lo0
0:0:0:0:0:0:0:1
127.0.0.1
只有当我连接到公司的 VPN 网络时才会出现此问题。然而这个 IP 并非来自 VPN 接口 (utun1) - 正如在上面的输出 172.xx 中所见。
我试图查看此 IP 是否来自 underyling OS(在我的情况下为 macOS),但事实也并非如此。
$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*'
inet 127.0.0.1
inet 192.168.0.106
inet 172.20.97.60
$ curl ifconfig.me
183.82.20.54
traceroute 8.8.8.8
的建议输出:
$ traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 64 hops max, 52 byte packets
1 192.168.0.1 (192.168.0.1) 2.317 ms 1.067 ms 0.847 ms
2 broadband.actcorp.in (106.51.140.1) 2.220 ms 2.585 ms 2.501 ms
3 broadband.actcorp.in (202.83.20.173) 3.120 ms 2.199 ms 4.124 ms
4 broadband.actcorp.in (202.83.20.181) 2.501 ms 2.534 ms 2.635 ms
5 * * *
6 72.14.194.18 (72.14.194.18) 43.590 ms 41.858 ms 42.637 ms
7 74.125.242.131 (74.125.242.131) 42.560 ms
108.170.253.121 (108.170.253.121) 42.911 ms
108.170.253.122 (108.170.253.122) 40.468 ms
8 72.14.239.59 (72.14.239.59) 42.028 ms
74.125.252.165 (74.125.252.165) 41.743 ms
209.85.248.251 (209.85.248.251) 43.130 ms
9 64.233.175.215 (64.233.175.215) 41.679 ms
72.14.236.223 (72.14.236.223) 43.124 ms
209.85.242.12 (209.85.242.12) 42.816 ms
10 216.239.35.145 (216.239.35.145) 42.076 ms
66.249.94.185 (66.249.94.185) 42.791 ms
216.239.35.167 (216.239.35.167) 42.456 ms
11 * * *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * google-public-dns-a.google.com (8.8.8.8) 41.985 ms *
同时为 ifconfig
添加完整输出:
$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
XHC20: flags=0<> mtu 0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether a4:5e:60:e5:b2:8b
inet6 fe80::c88:b30a:16fc:4dbe%en0 prefixlen 64 secured scopeid 0x5
inet 192.168.0.104 netmask 0xffffff00 broadcast 192.168.0.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
ether 06:5e:60:e5:b2:8b
media: autoselect
status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
ether b6:58:bb:ac:2e:e8
inet6 fe80::b458:bbff:feac:2ee8%awdl0 prefixlen 64 scopeid 0x7
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether 6a:00:00:58:9d:c0
media: autoselect <full-duplex>
status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether 6a:00:00:58:9d:c1
media: autoselect <full-duplex>
status: inactive
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether 6a:00:00:58:9d:c0
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en1 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 8 priority 0 path cost 0
member: en2 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 9 priority 0 path cost 0
nd6 options=201<PERFORMNUD,DAD>
media: <unknown type>
status: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
inet6 fe80::a3c9:7561:3c37:3943%utun0 prefixlen 64 scopeid 0xb
nd6 options=201<PERFORMNUD,DAD>
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
inet 172.20.97.60 --> 172.20.97.60 netmask 0xffffffff
来自 InetAddress.getLocalHost() 的文档:
This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.
因此,如果您有一个伪造的主机名,那么此函数将 return 一个伪造的 IP 地址。
您可以 运行 hostname
命令查看您的机器报告的主机名,然后使用 nslookup
查看该名称解析为什么(或者 ping $(hostname)
,这将进行名称解析)。
如果这是问题所在,并且您在 Mac 上拥有管理员访问权限,那么您可以更改名称(有关说明,请参阅 man hostname
)。但是,您可能需要与您的 IT 部门联系以了解正确的主机名应该是什么。
终于解决了。
正如@guest 指出的那样,这个问题是出于某种原因连接到 VPN 搞乱了名称解析,因此我的本地主机被解析为某个远程 IP。
解决方案是简单地添加一个 /etc/hosts 条目,手动将我的主机名映射到 127.0.0.1。那修好了。
当我说无效时,我的意思是无效。我得到一个与我机器接口的 any 无关的 IP 地址。我知道网站上有类似的问题,但更多的是关于人们获取他们不想要的接口地址(例如,127.0.0.1 而不是 LAN IP),但事实并非如此。我想知道这个 IP 是从哪里来的,我该如何摆脱它。
每当我调用 InetAddress.getLocalHost().getHostAddress()
,我得到 10.65.90.34。这不是从我的路由器、我的 ISP、我的 VPN 等任何东西分配的 IP。 ifconfig
的输出中没有提到这个 IP。我不知道这个IP来自哪里。有人建议在不同的问题上枚举所有接口,这是我这样做时得到的结果:
public void localIPTest() throws UnknownHostException, SocketException {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
System.out.println(hostAddress);
Enumeration Interfaces = NetworkInterface.getNetworkInterfaces();
while (Interfaces.hasMoreElements()) {
NetworkInterface Interface = (NetworkInterface) Interfaces.nextElement();
System.out.println(Interface.getDisplayName());
Enumeration Addresses = Interface.getInetAddresses();
while (Addresses.hasMoreElements()) {
InetAddress Address = (InetAddress) Addresses.nextElement();
System.out.println(Address.getHostAddress());
}
}
}
这输出:
10.65.90.34
utun0
fe80:0:0:0:a3c9:7561:3c37:3943%utun0
awdl0
fe80:0:0:0:b458:bbff:feac:2ee8%awdl0
utun1
172.20.97.60
en0
fe80:0:0:0:c88:b30a:16fc:4dbe%en0
192.168.0.106
lo0
fe80:0:0:0:0:0:0:1%lo0
0:0:0:0:0:0:0:1
127.0.0.1
只有当我连接到公司的 VPN 网络时才会出现此问题。然而这个 IP 并非来自 VPN 接口 (utun1) - 正如在上面的输出 172.xx 中所见。
我试图查看此 IP 是否来自 underyling OS(在我的情况下为 macOS),但事实也并非如此。
$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*'
inet 127.0.0.1
inet 192.168.0.106
inet 172.20.97.60
$ curl ifconfig.me
183.82.20.54
traceroute 8.8.8.8
的建议输出:
$ traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 64 hops max, 52 byte packets
1 192.168.0.1 (192.168.0.1) 2.317 ms 1.067 ms 0.847 ms
2 broadband.actcorp.in (106.51.140.1) 2.220 ms 2.585 ms 2.501 ms
3 broadband.actcorp.in (202.83.20.173) 3.120 ms 2.199 ms 4.124 ms
4 broadband.actcorp.in (202.83.20.181) 2.501 ms 2.534 ms 2.635 ms
5 * * *
6 72.14.194.18 (72.14.194.18) 43.590 ms 41.858 ms 42.637 ms
7 74.125.242.131 (74.125.242.131) 42.560 ms
108.170.253.121 (108.170.253.121) 42.911 ms
108.170.253.122 (108.170.253.122) 40.468 ms
8 72.14.239.59 (72.14.239.59) 42.028 ms
74.125.252.165 (74.125.252.165) 41.743 ms
209.85.248.251 (209.85.248.251) 43.130 ms
9 64.233.175.215 (64.233.175.215) 41.679 ms
72.14.236.223 (72.14.236.223) 43.124 ms
209.85.242.12 (209.85.242.12) 42.816 ms
10 216.239.35.145 (216.239.35.145) 42.076 ms
66.249.94.185 (66.249.94.185) 42.791 ms
216.239.35.167 (216.239.35.167) 42.456 ms
11 * * *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * google-public-dns-a.google.com (8.8.8.8) 41.985 ms *
同时为 ifconfig
添加完整输出:
$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
XHC20: flags=0<> mtu 0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether a4:5e:60:e5:b2:8b
inet6 fe80::c88:b30a:16fc:4dbe%en0 prefixlen 64 secured scopeid 0x5
inet 192.168.0.104 netmask 0xffffff00 broadcast 192.168.0.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
ether 06:5e:60:e5:b2:8b
media: autoselect
status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
ether b6:58:bb:ac:2e:e8
inet6 fe80::b458:bbff:feac:2ee8%awdl0 prefixlen 64 scopeid 0x7
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether 6a:00:00:58:9d:c0
media: autoselect <full-duplex>
status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether 6a:00:00:58:9d:c1
media: autoselect <full-duplex>
status: inactive
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether 6a:00:00:58:9d:c0
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en1 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 8 priority 0 path cost 0
member: en2 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 9 priority 0 path cost 0
nd6 options=201<PERFORMNUD,DAD>
media: <unknown type>
status: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
inet6 fe80::a3c9:7561:3c37:3943%utun0 prefixlen 64 scopeid 0xb
nd6 options=201<PERFORMNUD,DAD>
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
inet 172.20.97.60 --> 172.20.97.60 netmask 0xffffffff
来自 InetAddress.getLocalHost() 的文档:
This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.
因此,如果您有一个伪造的主机名,那么此函数将 return 一个伪造的 IP 地址。
您可以 运行 hostname
命令查看您的机器报告的主机名,然后使用 nslookup
查看该名称解析为什么(或者 ping $(hostname)
,这将进行名称解析)。
如果这是问题所在,并且您在 Mac 上拥有管理员访问权限,那么您可以更改名称(有关说明,请参阅 man hostname
)。但是,您可能需要与您的 IT 部门联系以了解正确的主机名应该是什么。
终于解决了。
正如@guest 指出的那样,这个问题是出于某种原因连接到 VPN 搞乱了名称解析,因此我的本地主机被解析为某个远程 IP。
解决方案是简单地添加一个 /etc/hosts 条目,手动将我的主机名映射到 127.0.0.1。那修好了。