I'm looking for a reverse command that displays the name of the
network adapter for a given IP address.
Based on everything I tried, this should work seems you say you need to get this information ONLY from the IP address which you already specify in your example.
INTERACTIVE PROMPT FOR IP ADDRESS TO GET NETWORK CONNECTION NAME
(Use WMIC
and some batch FOR
loop token
and delim
parsing to get the network connection name for a specified IP address.)
(The result value will echo to a command window and a message box window. It's all batch script but dynamically builds some VBS script functions to simplify the process for anyone that needs.)
@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
ALL ONE-LINERS
NATIVE WINDOWS ONLY WITH NETSH ALL INTERFACES (ALL IPv4 ADDRESSES)
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]*"
![enter image description here](../../I/static/images/7ea91e7db70b481f01cb27093c66444f4c541c758cd926e7a18d9af5c6542879.png)
NATIVE WINDOWS ONLY WITH IPCONFIG ALL INTERFACES (ALL IPv4 ADDRESSES)
IPCONFIG | FINDSTR /R "Ethernet* Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
![enter image description here](../../I/static/images/a08b94c6535ca6a83df395804ac99165d5093320351b131f38c9dc9db589e66c.png)
USING PCRE2GREP (per @SalvoF)
SINGLE IP ADDRESS SPECIFIED
netsh interface ipv4 show address | pcre2grep -B2 "192\.168\.2\.4" | FIND /V "DHCP"
FIND ALL IP ADDRESSES
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"
FIND ALL IP ADDRESSES (Cleaned Up Regex (per @SalvoF))
netsh interface ip show config | pcre2grep "^[A-Z]|IP.*([0-9]{1,3}(\.|)){4}"
Please note that the pcre2grep
I tried is per @SalvoF [+1]
as he suggested but using the.... FIND /V
to remove the line above containing DHCP
seems to get the desired output as you described. I used NETSH
rather than IPCONFIG
as well.
For a given local address, or to find out what interface routing uses to reach a particular remote address? – Ben Voigt – 2015-11-22T14:11:18.447
@BenVoigt: it's an address assigned to a network interface, so the result should be one adapter only. If it were about routing, the result could be many adapters (potentially with different metrics). – Thomas Weller – 2015-11-22T17:49:20.043