我正在使用 Windows 操作系统,想要获取网络接口的名称,无论是 eth0、eth1、eth2 等等。我编写了一个 Java 程序,通过传递主机/计算机名称,可以找到 IP 地址,然后根据 IP 地址可以找到网络接口的名称。代码如下:
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地址。但是我不知道如何获取网络接口名称。
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%
这个脚本提供了主机名/计算机名。
请帮我获取网络接口名称。
非常感谢。 祝好, Yeshwant
for /f "tokens=2-5 delims=," %%a in ('wmic nic where "netconnectionid like '%%'" get MACAddress^,AdapterType^,NetConnectionId^,Name /format:csv') do ( echo %%a, %%b, %%c, %%d )
- elzooilogicofor /f "tokens=2-5 delims=," %a in ('wmic nic where "netconnectionid like '%%'" get MACAddress^,AdapterType^,NetConnectionId^,Name /format:csv') do echo %a, %b, %c, %d
。如果您要将其放在批处理文件中,则必须将任何%
写成%%
,因此echo %a, %b, ...
将变为echo %%a, %%b, ...
。 - elzooilogico