如何获得我的计算机的ip地址,而不是虚拟盒适配器



在Java中,我试图获得本地机器的ip地址

String address = InetAddress.getLocalHost().getHostAddress();

在python中我有

socket.gethostbyname(socket.gethostname())

这两个都不返回我正在寻找的ip地址。现在我已经安装了VirtualBox和几个虚拟机,但是当我运行上面的2行代码时,它们没有打开。查看我的网络连接,它显示我有一个用于VirtualBox的局域网适配器,当我查看上面代码返回的ip和VirtualBox适配器的ip时,它们是相同的。有没有办法让我得到我的计算机本地IPv4地址,而不是其他虚拟适配器ip的不禁用它们?

一台机器可以有多个IP地址。下面将列举所有适配器并显示每个适配器的地址:

Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces();
while(i.hasMoreElements())
{
NetworkInterface n = i.nextElement();
System.out.println(n.getName());
Enumeration<InetAddress> ea = n.getInetAddresses();
while(ea.hasMoreElements())
{
InetAddress a = ea.nextElement();
System.out.println("   "+a.toString());
}
}

您可以自行决定对哪个适配器和地址感兴趣。

相关内容

最新更新