查找 IP 网络接口的命令

Tho*_*ler 15 networking windows ip-address ip batch-file

使用ipconfigI 可以显示网络适配器列表及其设置,例如 IP 地址。

我正在寻找显示给定 IP 地址的网络适配器名称的反向命令。

我曾尝试ipconfig使用类似的命令过滤输出,ipconfig | find "192.168.2.4"但随后适配器名称消失了。

我的输出ipconfig是(棘手的部分似乎是我在这里的一个适配器上有多个地址):

Windows IP Configuration


Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::xxxx:xxxx:xxxx:xxxx%11
   IPv4 Address. . . . . . . . . . . : 192.168.2.4
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.178.20
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.178.1
                                       192.168.2.1

Ethernet adapter VMware Network Adapter VMnet1:
...
Run Code Online (Sandbox Code Playgroud)

Dav*_*ill 8

如何显示给定 IP 地址的网络适配器名称?

这种解决方案要求任何外部命令(pcre2grepsed等)。

使用以下批处理文件 (getname.cmd):

@echo off
setlocal
setlocal enabledelayedexpansion
set "_adapter="
set "_ip="
for /f "tokens=1* delims=:" %%g in ('ipconfig /all') do (
  set "_tmp=%%~g"
  if "!_tmp:adapter=!"=="!_tmp!" (
    if not "!_tmp:IPv4 Address=!"=="!_tmp!" (
      for %%i in (%%~h) do (
      if not "%%~i"=="" set "_ip=%%~i"
      )
    set "_ip=!_ip:(Preferred)=!"
    if "!_ip!"=="%1" (
        @echo !_adapter!
      )
    )
  ) else (
    set "_ip="
    set "_adapter=!_tmp:*adapter =!"
  )
)
endlocal
Run Code Online (Sandbox Code Playgroud)

用法:

getname ipaddress
Run Code Online (Sandbox Code Playgroud)

例子:

F:\test>getname 192.168.42.78
Local Area Connection 2
F:\test>
Run Code Online (Sandbox Code Playgroud)

进一步阅读

  • @SalvoF **是**编程。批处理有变量,`goto`,`for`,`if`,函数,宏......你还需要什么?;) (3认同)

Vom*_*yle 6

我正在寻找一个反向命令来显示给定 IP 地址的网络适配器名称。

根据我尝试过的一切,这应该可行,似乎您说您只需要从您在示例中指定的 IP 地址获取此信息。

交互式提示输入 IP 地址以获取网络连接名称

(使用WMIC一些批处理FOR循环tokendelim解析来获取指定 IP 地址的网络连接名称。)

(结果值将回显到命令窗口和消息框窗口。这都是批处理脚本,但动态构建一些 VBS 脚本函数,以简化任何需要的人的过程。)

@ECHO ON

:SetTempFiles
SET tmpIPaddr=%tmp%\~tmpipaddress.vbs
SET tmpNetConName1=%tmp%\~tmpNetConName1.txt
SET tmpNetConName2=%tmp%\~tmpNetConName2.txt
SET tmpBatFile=%tmp%\~tmpBatch.cmd
SET tmpVBNetCon=%tmp%\~tmpVBNetCon.vbs

IF EXIST "%tmpIPaddr%" DEL /F /Q "%tmpIPaddr%"
IF EXIST "%tmpNetConName1%" DEL /Q /F "%tmpNetConName1%"
IF EXIST "%tmpNetConName2%" DEL /Q /F "%tmpNetConName2%"
IF EXIST "%tmpBatFile%" DEL /Q /F "%tmpBatFile%"
IF EXIST "%tmpVBNetCon%" DEL /Q /F "%tmpVBNetCon%"

:InputBox
SET msgboxTitle=IP ADDRESS
SET msgboxLine1=Enter the IP address to get its Windows connection name
>"%tmpIPaddr%" ECHO wsh.echo inputbox("%msgboxLine1%","%msgboxTitle%")
FOR /F "tokens=*" %%N IN ('cscript //nologo "%tmpIPaddr%"') DO CALL :setvariables %%N
GOTO EOF

:setvariables
SET IPAddress=%~1
FOR /F "USEBACKQ TOKENS=3 DELIMS=," %%A IN (`"WMIC NICCONFIG GET IPADDRESS,MACADDRESS /FORMAT:CSV | FIND /I "%IPAddress%""`) DO (SET MACAddress=%%~A)
FOR /F "USEBACKQ TOKENS=3 DELIMS=," %%B IN (`"WMIC NIC GET MACADDRESS,NETCONNECTIONID /FORMAT:CSV | FIND /I "%MACAddress%""`) DO ECHO(%%~B>>"%tmpNetConName1%"

::: Parse Empty Lines
FINDSTR "." "%tmpNetConName1%">"%tmpNetConName2%"

::: Build Dynamic Batch with ECHO'd Network Connection Value
FOR /F "tokens=*" %%C IN (%tmpNetConName2%) DO ECHO ECHO %%~C>>"%tmpBatFile%"
IF NOT EXIST "%tmpBatFile%" GOTO :NullExit
START "" "%tmpBatFile%"

::: Build Dynamic VBS with Message Box Network Connection Value
FOR /F "tokens=*" %%C IN (%tmpNetConName2%) DO (SET vbNetconName=%%~C)
ECHO msgbox "%vbNetconName%",0,"%vbNetconName%">"%tmpVBNetCon%"
START /B "" "%tmpVBNetCon%"
EXIT /B

:NullExit
ECHO msgbox "Cannot find MAC Address, check to confirm IP Address was correct.",0,"Invalid IP">"%tmpVBNetCon%"
START /B "" "%tmpVBNetCon%"
EXIT /B
Run Code Online (Sandbox Code Playgroud)

所有单线

仅具有 NETSH 所有接口(所有 IPv4 地址)的本机 Windows

NETSH INT IP SHOW CONFIG | FINDSTR /R "Configuration for interface.* Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

仅具有 IPCONFIG 所有接口(所有 IPv4 地址)的本机 Windows

IPCONFIG | FINDSTR /R "Ethernet* Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


使用 PCRE2GREP(根据 @SalvoF)

指定单个 IP 地址

netsh interface ipv4 show address | pcre2grep -B2 "192\.168\.2\.4" | FIND /V "DHCP"
Run Code Online (Sandbox Code Playgroud)

查找所有 IP 地址

netsh interface ip show config | pcre2grep -B2 ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$ | FIND /V "DHCP" | FIND /V "Gate" | FIND /V "Metric" | FIND /V "Subnet"
Run Code Online (Sandbox Code Playgroud)

查找所有 IP 地址(清理正则表达式(根据 @SalvoF))

netsh interface ip show config | pcre2grep "^[A-Z]|IP.*([0-9]{1,3}(\.|)){4}"
Run Code Online (Sandbox Code Playgroud)

请注意,pcre2grep我尝试的是按照他建议的@SalvoF [+1],但使用...FIND /V删除上面包含的行DHCP似乎得到了您所描述的所需输出。我用的NETSH也不是IPCONFIG那么好。

  • 感谢您的投入!顺便说一句,我注意到我这边的前导行和尾随行是由于语言问题造成的。为了规避这些,你对此有何看法? `netsh 接口 ip 显示配置 | pcre2grep "^[AZ]|IP.*([0-9]{1,3}(\.|)){4}"` (我稍微清理了一下正则表达式)。我在这个地方学到了很多东西! (2认同)

wmz*_*wmz 6

你可以使用这个 PS one liner:

$addr='192.168.2.4'; get-wmiobject Win32_NetworkAdapterConfiguration |? {$_.ipaddress -contains $addr} |select Description |% {$_.Description}
Run Code Online (Sandbox Code Playgroud)

要直接从命令行使用它:

powershell "$addr='192.168.2.4'; get-wmiobject Win32_NetworkAdapterConfiguration |? {$_.ipaddress -contains $addr} |select Description |% {$_.Description}"
Run Code Online (Sandbox Code Playgroud)

或者如果你想重用它,把它放在一个脚本中并使地址成为一个参数

编辑:获取在 Win/Ipconfig 中显示的名称:

$addr='192.168.2.4'; 
$netconf = get-wmiobject Win32_NetworkAdapterConfiguration |? {$_.ipaddress -contains $addr};
$netconf |% {$_.GetRelated("win32_NetworkAdapter")} | select NetConnectionID |%{$_.NetConnectionID}
Run Code Online (Sandbox Code Playgroud)

(对中间变量的赋值只是为了让它更具可读性)

  • 它在另一张桌子上@wmz。PS 岩石虽然:`powershell "$ip = '192.168.2.4';foreach($int in (gwmi Win32_NetworkAdapter)) {gwmi Win32_NetworkAdapterConfiguration -Filter """Index = $($int.index)""" | ? {$ _.IPAddress -包含 $ip} | % {$int.NetConnectionID} }"` (2认同)