Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Windows how to get network interface name whether its eth0/eth1/eth2 using batch script?

I am working on windows and wanted to get the network interface name whether its eth0, eth1, eth2 etc etc etc. I wrote java program and by passing host/computer name I can find out ip address and thereafter based on an ip address I can find out network interface name. The code is as below:

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;
    }

The above method returns the value something like eth0/eth1/eth2 etc.

Same thing I want to achieve using batch script on windows. I wrote following script which is providing hostname and an ip address. But I don't know as to how to get the network interface name.

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%

This scripts gives hostname/computername.

Please help me to get network interface name.

Thanks a lot. regards, Yeshwant

like image 373
Yeshwant KAKAD Avatar asked Dec 04 '25 11:12

Yeshwant KAKAD


2 Answers

This should give you the Network Interface Name:

@Echo Off
For /F "Skip=1Delims=" %%a In (
    '"WMIC NIC Where (Not NetConnectionStatus Is Null) Get NetConnectionID"'
) Do For /F "Tokens=*" %%b In ("%%a") Do Echo=%%b
Timeout -1
like image 125
Compo Avatar answered Dec 06 '25 23:12

Compo


If you want to find the network interface based on a IP-address you can use:

@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

I use netsh instead of WMIC because I don't have much experience in WMIC but netsh is sufficient in your case.
Note that the SetLocal EnableDelayedExpansion is very important as this script is based on checking the ERRORLEVEL. The cmd-parser parses every block of commands delimited with () as being one single command as if it was written on one line (in our case FOR ... ( ... ) ). It is thus impossible to use a changing variable with its updated value inside a () unless you use delayed expansion. If you don't, only the value before entering the block can be used. As ERRORLEVEL can change in each iteration (will be 0 if the IP-address has been found by the findstr above it in the addresses of an interface or 1 if it hasn't), we always need the updated value. This is also why I use !ERRORLEVEL! instead of the classic %ERRORLEVEL%.
More info about EnableDelayedExtension can be found here.

Running the script above with set ownIP=127.0.0.1 (don't forget to change to your IP) gives the following result:

>search_interface.bat
IP-address 127.0.0.1 from interface Loopback Pseudo-Interface 1

If you'd like to pass the IP-address as an argument each time you call the script, use set ownIP=%~1 (but don't forget to add a check if an argument has been passed in that case).

Hope it helps.

Good luck!

like image 27
J.Baoby Avatar answered Dec 07 '25 01:12

J.Baoby