With ipconfig I can show the list of network adapters and their settings, e.g. the IP address.
I'm looking for a reverse command that displays the name of the network adapter for a given IP address.
I have tried filtering the output of ipconfig with a command like ipconfig | find "192.168.2.4" but then the adapter name is gone.
My output of ipconfig is (the tricky part seems that I have several addresses on one adapter here):
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:
...
8 Answers
How do I display the name of a network adapter for a given IP address?
This solution does not require any external commands (pcre2grep, sed, etc).
Use the following batch file (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
Usage:
getname ipaddress
Example:
F:\test>getname 192.168.42.78
Local Area Connection 2
F:\test>
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- for /f - Loop command against the results of another command.
- ipconfig - Configure IP (Internet Protocol configuration)
You could use this PS one liner:
$addr='192.168.2.4'; get-wmiobject Win32_NetworkAdapterConfiguration |? {$_.ipaddress -contains $addr} |select Description |% {$_.Description}
To use it directly from command line:
powershell "$addr='192.168.2.4'; get-wmiobject Win32_NetworkAdapterConfiguration |? {$_.ipaddress -contains $addr} |select Description |% {$_.Description}"
or if you want to reuse it put it in a script and make the address a parameter
Edit: to get a name as it shows in Win/Ipconfig:
$addr='192.168.2.4';
$netconf = get-wmiobject Win32_NetworkAdapterConfiguration |? {$_.ipaddress -contains $addr};
$netconf |% {$_.GetRelated("win32_NetworkAdapter")} | select NetConnectionID |%{$_.NetConnectionID}
(the assignment to intermediary variables is only to make it a bit more readable)
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]*"
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]*"