Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows上如何使用批处理脚本获取网络接口名称是否为eth0/eth1/eth2?_Windows_Batch File_Networking_Interface - Fatal编程技术网

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

在Windows上如何使用批处理脚本获取网络接口名称是否为eth0/eth1/eth2?,windows,batch-file,networking,interface,Windows,Batch File,Networking,Interface,我在windows上工作,希望获得网络接口名称,无论是eth0、eth1、eth2等。我编写了java程序,通过传递主机/计算机名称,我可以找到ip地址,然后根据ip地址,我可以找到网络接口名称。代码如下: public static String getNetworkInterface() { try { String computerHostName = "dummy"; if(!computerHostName.equals(

我在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%
此脚本提供主机名/计算机名

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

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

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

@Echo关闭
对于/F“Skip=1Delims=”%a In(
“'WMIC NIC其中(非NetConnectionStatus为空)获取NetConnectionID”'
)Do For/F“令牌=*”%%b In(“%%a”)Do Echo=%%b
超时-1

如果要根据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
如果希望在每次调用脚本时将IP地址作为参数传递,请使用
set-ownIP=%~1
(但不要忘记在这种情况下添加检查是否传递了参数)

希望能有帮助


祝你好运

试试看,
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)
你好,我试过你的建议了。您是否可以指出您看到的任何错误(如果有):for/f“tokens=2-5 delims=,”%%a in('wmic nic where“netconnectionid like%%'”get MACAddress^,adapter type^,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”get-MACAddress,AdapterType,netconnectionid,Name/format:csv')do(回显%a,%b,%c,%d)没有可用的实例。无效的XSL格式(或)文件名。c:\Users\user>我为/f“tokens=2-5 delims=,”%%a in('wmic nic where“netconnectionid”类似“%”指定了格式的完整路径“get MACAddress^,AdapterType^,NetConnectionId^,名称/格式:”%WINDIR%\System32\wbem\en US\csv“')do(echo%%a,%%b,%%c,%%d)结果是:c:\Users\user>testtemp.bat c:\Users\user>for/F“令牌=2-5 delims=,%a in('wmic nic其中“NetConnectionId”类似“%”获取MACAddress,AdapterType,NetConnectionId,名称/格式:“C:\Windows\System32\wbem\en US\csv”')do(echo%a,%b,%C,%d)没有可用的实例。,,)ser>(echo,@yeswant,在命令行
中为/f“tokens=2-5 delims=,%a in('wmic nic where“netconnectionid like%%”get MACAddress^,AdapterType^,netconnectionid^,Name/format:csv')尝试此操作)执行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其中“netconnectionid like”%%“get MACAddress^,AdapterType^,netconnectionid^,名称/格式:csv')不回显%a,%b,%c,%d没有可用的实例,,,,s\user>echo,,,c:\Users\user>Compo:这给我的结果如下:c:\Users\user>testtemp1.bat没有实例可用。按任意键继续…C:\Users\user>这意味着它显示的正是,您没有返回0–65535之间状态的网络连接。这并不意味着我的代码有问题。这只为我打印了所有连接的接口…他没有要求提供IP地址的接口吗?旁边it’我不是特别要求这样做,我最后的回答并没有改变这一点,如果没有网络连接,那么我打赌它不会有IP地址谢谢你J.Baoby。我首先提供了我电脑的IP地址,然后是127.0.0.1。结果如下:C:\Users\user>notepad testtemp1.bat C:\Users\user>testtemp1.bat IP地址192.168.1.152从接口本地连接C:\Users\user>testtemp1.bat IP地址127.0.0.1从接口环回伪接口1 C:\Users\user>它没有给我我想要的。为什么通过java程序我得到eth4,但批处理脚本给我值“局域网”/“环回伪接口1”“后者是一个接口,Java使其更具可移植性,这是他们演示两者的地方2可能会给出相同的结果,但在Windows中显然不是这样。在我们的例子中,第一个来自Windows,是Windows本身决定了他将传递给Java的名称。我在注册表项中进行了查找。我查找了
netsh
wmic
提供的名称,发现它们是完整的与Windows提供给Java的描述性名称不同的注册目录。您也可以搜索您,只需打开
regedit
(通过例如的运行面板)并搜索(
Ctrl+F
)Java提供的显示名和
netsh提供的显示名这是我的荣幸。希望能有所帮助。Windows决定“隐藏”某些真实设备的真正原因对我来说也是一个谜。也许出于安全原因……如果我找到更多关于它的信息,我一定会让你知道