我正在寻找一个反向命令,该命令显示给定IP地址的网络适配器的名称。
基于我尝试过的所有内容,这似乎可以正常工作,您似乎只需要从示例中已指定的IP地址获取此信息即可。
IP地址交互提示以获取网络连接名称
(使用WMIC
和一些批处理FOR
循环token
和delim
解析来获取指定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
所有单行
原生WINDOWS仅具有NETSH所有接口(所有IPv4地址)
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]*"
仅具有IPCONFIG所有接口(所有IPv4地址)的本地Windows
IPCONFIG | FINDSTR /R "Ethernet* Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
使用PCRE2GREP(每个@SalvoF)
指定单IP地址
netsh interface ipv4 show address | pcre2grep -B2 "192\.168\.2\.4" | FIND /V "DHCP"
查找所有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"
查找所有IP地址(清理正则表达式(每个@SalvoF))
netsh interface ip show config | pcre2grep "^[A-Z]|IP.*([0-9]{1,3}(\.|)){4}"
请注意,pcre2grep
我尝试[+1]
按照他的建议按照@SalvoF进行操作,但是使用.... FIND /V
删除上面包含的行DHCP
似乎如您所述获得了所需的输出。我用NETSH
而不是IPCONFIG
。