gpt4 book ai didi

java - 如何从 Java 枚举所有启用的 NIC 卡的 IP 地址?

转载 作者:IT老高 更新时间:2023-10-28 20:37:33 25 4
gpt4 key购买 nike

没有解析ipconfig的输出,有人有100%纯java的方法吗?

最佳答案

这很简单:

try {
InetAddress localhost = InetAddress.getLocalHost();
LOG.info(" IP Addr: " + localhost.getHostAddress());
// Just in case this host has multiple IP addresses....
InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
if (allMyIps != null && allMyIps.length > 1) {
LOG.info(" Full list of IP addresses:");
for (int i = 0; i < allMyIps.length; i++) {
LOG.info(" " + allMyIps[i]);
}
}
} catch (UnknownHostException e) {
LOG.info(" (error retrieving server host name)");
}

try {
LOG.info("Full list of Network Interfaces:");
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
LOG.info(" " + intf.getName() + " " + intf.getDisplayName());
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
LOG.info(" " + enumIpAddr.nextElement().toString());
}
}
} catch (SocketException e) {
LOG.info(" (error retrieving network interface list)");
}

关于java - 如何从 Java 枚举所有启用的 NIC 卡的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/494465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com