【问题标题】:On Windows how to get network interface name whether its eth0/eth1/eth2 using batch script?在 Windows 上如何使用批处理脚本获取网络接口名称是否为 eth0/eth1/eth2?
【发布时间】:2016-11-28 08:58:52
【问题描述】:

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

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%

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

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

非常感谢。 问候, 好想

【问题讨论】:

  • 试试,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 )
  • 您好 elzooilogico,我尝试了您的建议。你能指出任何你看到的错误吗: 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 ) 我得到的错误:C:\Users\user>testnew.bat C:\Users\user >for /F "tokens=2-5 delims=," %a in ('wmic nic where "netconnectionid like '%'" get MACAddress,AdapterType,NetConnectionId,Name /format:csv') 做 (echo %a, % b, %c, %d ) 没有可用的实例。无效的 XSL 格式(或)文件名。 C:\用户\用户>
  • 我指定了格式的完整路径:for /f "tokens=2-5 delims=," %%a in ('wmic nic where "netconnectionid like '%%'" get MACAddress^,AdapterType ^,NetConnectionId^,Name /format:"%WINDIR%\System32\wbem \en-US\csv"') 做 ( echo %%a, %%b, %%c, %%d ) 结果是:C: \Users\user>testtemp.bat C:\Users\user>for /F "tokens=2-5 delims=," %a in ('wmic nic where "netconnectionid like '%'" get MACAddress,AdapterType,NetConnectionId,名称 /format:"C:\Windows\System32\wbem\en-US\csv"') do (echo %a, %b, %c, %d ) 没有可用的实例。 , , , ) ser>(echo , , ,
  • @yeswant,在命令行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 中试试这个。如果要放入批处理文件,则必须将任何% 写为%%,因此echo %a, %b, ... 将是echo %%a, %%b, ...
  • 感谢 Elzoologico:我从命令行尝试过,结果如下。知道什么是没有可用的实例意味着: C:\Users\user>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 没有可用的实例。 , , , s\user>echo , , , C:\Users\user>

标签: windows batch-file networking interface


【解决方案1】:

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

@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

【讨论】:

  • Compo:这给了我如下结果:C:\Users\user>testtemp1.bat 没有可用的实例。按任意键继续... C:\Users\user>
  • 这意味着它所显示的内容,您没有返回 0–65535 之间状态的网络连接。 这并不意味着我的代码有问题。
  • 这只是为我打印了所有连接的接口......他不是要求给定IP地址的接口吗?
  • 除了没有明确要求之外,我最后的回复并没有真正改变这一点,如果没有网络连接,那么我敢打赌它不会有 IP 地址
【解决方案2】:

如果您想根据 IP 地址查找网络接口,您可以使用:

@echo off
SetLocal EnableDelayedExpansion

set ownIP=W.X.Y.Z
set netifc=
FOR /F "tokens=4*" %%G IN ('netsh interface ipv4 show interfaces ^| findstr /I "[^a-zA-Z]connected"') DO (
    REM set /P =interface %%G is connected < nul >> !progress!
    netsh interface ipv4 show addresses "%%H" | findstr /c "%ownIP%" > nul 2>&1
    IF !ERRORLEVEL! EQU 0 (
        set netifc=%%H
    )
)

:outLoop
echo IP-address %ownIP% from interface %netifc%
EndLocal
exit /b 0

我使用netsh 而不是WMIC,因为我在WMIC 方面没有太多经验,但netsh 在您的情况下就足够了。
请注意SetLocal EnableDelayedExpansion 非常重要,因为此脚本基于检查ERRORLEVEL。 cmd-parser 将每个以() 分隔的命令块解析为单个命令,就好像它写在一行上(在我们的例子中是FOR ... ( ... ))。因此,除非您使用延迟扩展,否则不可能在 () 内使用具有更新值的变化变量。如果不这样做,则只能使用进入块之前的值。由于ERRORLEVEL 可以在每次迭代中更改(如果IP 地址已被其上方的findstr 在接口地址中找到,则为0,否则为1),我们总是需要更新的值。这也是我使用!ERRORLEVEL! 而不是经典的%ERRORLEVEL% 的原因。
有关EnableDelayedExtension 的更多信息,请访问here

使用set ownIP=127.0.0.1 运行上面的脚本(不要忘记更改您的 IP)会得到以下结果:

>search_interface.bat
IP-address 127.0.0.1 from interface Loopback Pseudo-Interface 1

如果您想在每次调用脚本时将 IP 地址作为参数传递,请使用 set ownIP=%~1(但不要忘记在这种情况下添加检查参数是否已传递)。

希望对你有帮助。

祝你好运!

【讨论】:

  • 谢谢 J. Baoby。我首先提供了我的 PC 的 IP 地址,然后提供了 127.0.0.1。结果如下: C:\Users\user>notepad testtemp1.bat C:\Users\user>testtemp1.bat IP-address 192.168.1.152 from interface Local Area Connection C:\Users\user>testtemp1.bat IP-address 127.0.0.1 from interface Loopback Pseudo-Interface 1 C:\Users\user> 它没有给我我想要的。如何通过 java 程序得到 eth4,但批处理脚本给了我“局域网”/“环回伪接口 1”的值。 ?
  • NetworkInterface类中,有2种方法容易让人混淆:getDisplayNamegetName。这是 2 种不同的方法并且有不同的工作方式:第一种描述了操作系统(在我们的例子中是 Windows)提供的设备,后者是一个接口名称 Java 自己制作以使其更便携
  • 这里是a little tuto,他们展示了他们两个。请注意,在某些操作系统(我认为主要是类 Unix 操作系统)中,2 可能会给出相同的结果,但在 Windows 中显然不是这样。由于在我们的例子中第一个来自 Windows,Windows 本身决定了他将哪些名称传递给 Java。我已经在注册表项中进行了查找。我查看了 netshwmic 提供的名称,发现它们与 Windows 给 Java 的描述性名称完全不同。
  • 您也可以进行搜索,只需打开regedit(例如通过“运行”面板)并搜索(Ctrl+F)Java 提供的显示名称和netsh 提供的显示名称.我根据this site 进行了搜索(我只是省略了“重命名”部分:p)
  • 这是我的荣幸。希望能帮助到你。 Windows 决定“隐藏”一些真实设备的真正原因对我来说仍然是个谜。也许出于安全原因...如果我找到更多关于它的信息,我一定会告诉你
猜你喜欢
  • 1970-01-01
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多