M
M
miceZipper2015-08-25 16:40:17
Automation
miceZipper, 2015-08-25 16:40:17

BAT file. How to list network interfaces?

I have several network interfaces. I want to display their list using a BAT file in the format: number - name - IP. Compiled a BAT file, but it only outputs the first interface. I can not debug and understand what's wrong.
@echo off
setlocal enabledelayedexpansion
set /A COUNTER=0
set IFACE=
set IFACE_IP=
echo Select network interface number:
echo.
FOR /F "usebackq skip=2 delims=" %%a IN (`netsh interface show interface`) DO (
FOR /F "tokens=4* delims= " %%b IN (^"%%a^") DO (
set /A COUNTER+=1
set IFACE=%%b %%c
FOR /F "usebackq skip=5 tokens=5 delims= " %%d IN (`netsh interface ipv4 show ipaddresses ^"!IFACE!^" normal` ) DO (
set IFACE_IP=%%d
echo! COUNTER! - IFACE! - IFACE_IP!
)
)
)
pause
My interfaces:

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rainbird, 2015-08-25
@miceZipper

That's right, but you have an extra space added to the interface names from the 1st word:

set IFACE=%%b %%c
             ^
          вот тут

it is better to shove the entire tail of the netsh interface show interface after the 3rd token into a variable:
@echo off
setlocal enabledelayedexpansion
set /A COUNTER=0
set IFACE=
set IFACE_IP=
echo Выберите номер сетевого интерфейса:
echo.

FOR /F "usebackq skip=2 delims=" %%a IN (`netsh interface show interface`) DO (
  FOR /F "tokens=3* delims= " %%b IN (^"%%a^") DO (
    set /A COUNTER+=1
    set IFACE=%%c
    FOR /F "usebackq skip=5 tokens=5 delims= " %%d IN (`netsh interface ipv4 show ipaddresses ^"!IFACE!^" normal`) DO (
      set IFACE_IP=%%d
      echo !COUNTER! - !IFACE! - !IFACE_IP!
    )
  )
)
 
pause

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question