gpt4 book ai didi

windows - 在 Windows 上如何使用批处理脚本获取网络接口(interface)名称是否为 eth0/eth1/eth2?

转载 作者:可可西里 更新时间:2023-11-01 09:57:54 28 4
gpt4 key购买 nike

我在 Windows 上工作,想获取网络接口(interface)名称,无论是 eth0、eth1、eth2 等等。我编写了 java 程序,通过传递主机/计算机名称,我可以找出 ip 地址,然后根据 ip地址我可以找出网络接口(interface)名称。代码如下:

public static String getNetworkInterface() {
try {
String computerHostName = "dummy";
if(!computerHostName.equals("Unknown Computer")) {
InetAddress address = InetAddress.getByName(computerHostName);
return NetworkInterface.getByInetAddress(address).getName();
}
else {
return null;
}
}
catch (SocketException e) {
e.printStackTrace();
}
catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}

上述方法返回的值类似于 eth0/eth1/eth2 等。

我想在 Windows 上使用批处理脚本来实现同样的事情。我写了以下提供主机名和 IP 地址的脚本。但我不知道如何获取网络接口(interface)名称。

for /f "skip=1 delims=" %%A in (
'wmic computersystem get name'
) do for /f "delims=" %%B in ("%%A") do set "compName=%%A"

echo hostname is %compName%

此脚本提供主机名/计算机名。

请帮我获取网络接口(interface)名称。

非常感谢。问候,是的

最佳答案

这应该为您提供网络接口(interface)名称:

@Echo Off
For /F "Skip=1Delims=" %%a In (
'"WMIC NIC Where (Not NetConnectionStatus Is Null) Get NetConnectionID"'
) Do For /F "Tokens=*" %%b In ("%%a") Do Echo=%%b
Timeout -1

关于windows - 在 Windows 上如何使用批处理脚本获取网络接口(interface)名称是否为 eth0/eth1/eth2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40840400/

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