Discussion:
parsing interface list from netshell
(too old to reply)
Petr Laznovsky
2009-11-18 22:42:59 UTC
Permalink
Hi there, I am beginner in the CMD scripting.

I want to get interface list, each iface into each variable. I have two
problems with this. My command is:

for /f "tokens=3,* delims= " %i in ('netsh in sh in') do echo %i %j

I am use local (Czech) language version of WXP and the tranlation of
nesth use two words for loopback ("zpetna smycka" something like "loop
back" with space between those two words). When I try to parse it, there
is problem with delimiter bacause there are two words for loopback in
the column "TYPE", something like this:


Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Dedicated dummy
Enabled Dedicated Local Area Connection
Enabled Internal Internal
Enabled Loop Back Loop Back

Second probles is I have no idea how to push ecach line of output (name
of each iface) into separated variable.

Any ideas??

Petr Laznovsky
Pegasus [MVP]
2009-11-18 23:18:47 UTC
Permalink
Post by Petr Laznovsky
Hi there, I am beginner in the CMD scripting.
I want to get interface list, each iface into each variable. I have two
for /f "tokens=3,* delims= " %i in ('netsh in sh in') do echo %i %j
I am use local (Czech) language version of WXP and the tranlation of nesth
use two words for loopback ("zpetna smycka" something like "loop back"
with space between those two words). When I try to parse it, there is
problem with delimiter bacause there are two words for loopback in the
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Dedicated dummy
Enabled Dedicated Local Area Connection
Enabled Internal Internal
Enabled Loop Back Loop Back
Second probles is I have no idea how to push ecach line of output (name of
each iface) into separated variable.
Any ideas??
Petr Laznovsky
When I run the command
for /f "tokens=3,* delims= " %i in ('netsh in sh in') do echo %i %j
then my console output looks radically different from yours, even when
allowing for linguistic differences. I am therefore unable to give you a
solution that is guaranteed to work for you. And while I appreciate that the
Czech translation of "loopback" generates two words, I do not understand
what you're trying to extract from the console output.

The code below is an example for your second question: How to assign each
line of the screen output to a variable. The text file d:\temp\test.txt
contains the exact console lines that you posted in your note.

@echo off
SetLocal EnableDelayedExpansion
set Line=0
for /F "delims=" %%a in ('type "d:\temp\test.txt"') do (
set /a Line=!Line! + 1
set Var=%%a
echo Line!Line!=!Var!
)
Petr Laznovsky
2009-11-19 11:43:30 UTC
Permalink
Post by Pegasus [MVP]
Post by Petr Laznovsky
Hi there, I am beginner in the CMD scripting.
I want to get interface list, each iface into each variable. I have two
for /f "tokens=3,* delims= " %i in ('netsh in sh in') do echo %i %j
I am use local (Czech) language version of WXP and the tranlation of nesth
use two words for loopback ("zpetna smycka" something like "loop back"
with space between those two words). When I try to parse it, there is
problem with delimiter bacause there are two words for loopback in the
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Dedicated dummy
Enabled Dedicated Local Area Connection
Enabled Internal Internal
Enabled Loop Back Loop Back
Second probles is I have no idea how to push ecach line of output (name of
each iface) into separated variable.
Any ideas??
Petr Laznovsky
When I run the command
for /f "tokens=3,* delims= " %i in ('netsh in sh in') do echo %i %j
then my console output looks radically different from yours,
The output I post is not output from full parsing command, but from
"netsh in sh in" in English OS version. I want to demonstrate the "two
words for loopback" problem. Real outpul from "netsh in sh in" is following:

Admin. stav Stav Typ Název rozhraní
-------------------------------------------------------------------------
Povoleno Vyhrazený Bluetooth Network
Povoleno Vyhrazený ETH0
Povoleno Vyhrazený WLAN0
Povoleno Vyhrazený Bezdrátové pøipojení k síti 4
Povoleno Vyhrazený Bezdrátové pøipojení k síti 2
Povoleno Vyhrazený Hamachi
Povoleno Vyhrazený Bezdrátové pøipojení k síti 3
Povoleno Vnitøní Vnitøní
Povoleno Zpìtná smyèka Zpìtná smyèka
--------------------------------------------------------------------------

Output from real FOR /F command is:

-----------------------------------------------------------
c:\>echo Stav Typ Název rozhraní
Stav Typ Název rozhraní

c:\>echo Bluetooth Network
Bluetooth Network

c:\>echo ETH0
ETH0

c:\>echo WLAN0
WLAN0

c:\>echo Bezdrátové pøipojení k síti 4
Bezdrátové pøipojení k síti 4

c:\>echo Bezdrátové pøipojení k síti 2
Bezdrátové pøipojení k síti 2

c:\>echo Hamachi
Hamachi

c:\>echo Bezdrátové pøipojení k síti 3
Bezdrátové pøipojení k síti 3

c:\>echo Vnitøní
Vnitøní

c:\>echo smyèka Zpìtná smyèka
smyèka Zpìtná smyèka
----------------------------------------------------------

on last line you can see: "back Loop back" instead of "Loop back" or
"Loopback"


even when
Post by Pegasus [MVP]
allowing for linguistic differences. I am therefore unable to give you a
solution that is guaranteed to work for you. And while I appreciate that the
Czech translation of "loopback" generates two words, I do not understand
what you're trying to extract from the console output.
I am administrator of IP network consist of many small subnets (/27 and
/29) without use of DHCP. Technicians with the laptops win WXP need time
by time connect to some subnets in some racks and need to change IP
address manually which take some time and mistakes are done frequently.
Therefor I wrote attached batch which allow him to choose location from
batch menu and batch will setup IP subsystem automatically. But there is
one issue: Setup address automatically by netsh need to provide iface
name and because on some node technicians connect through ethernet aand
on some nodes tahy need to connect throuh wireless my idea is first
provide him menu with list of network adapters present in the laptop,
let him choose adapter, than fill adapter name into variable and pass
into next part of batch (already done) which is choose
location/ip_address and setup to system.

Sorry for my poor english, hope you understood...
Post by Pegasus [MVP]
The code below is an example for your second question: How to assign each
line of the screen output to a variable. The text file d:\temp\test.txt
contains the exact console lines that you posted in your note.
@echo off
SetLocal EnableDelayedExpansion
set Line=0
for /F "delims=" %%a in ('type "d:\temp\test.txt"') do (
set /a Line=!Line! + 1
set Var=%%a
echo Line!Line!=!Var!
)
Now I have no time, but I will try ASAP. Thank you for this...

L.
Tom Lavedas
2009-11-19 15:28:36 UTC
Permalink
Post by Petr Laznovsky
Post by Pegasus [MVP]
Post by Petr Laznovsky
Hi there, I am beginner in the CMD scripting.
I want to get interface list, each iface into each variable. I have two
for /f "tokens=3,* delims= " %i in ('netsh in sh in') do echo %i %j
I am use local (Czech) language version of WXP and the tranlation of nesth
use two words for loopback ("zpetna smycka" something like "loop back"
with space between those two words). When I try to parse it, there is
problem with delimiter bacause there are two words for loopback in the
Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled                       Dedicated        dummy
Enabled                       Dedicated        Local Area Connection
Enabled                       Internal         Internal
Enabled                       Loop Back        Loop Back
Second probles is I have no idea how to push ecach line of output (name of
each iface) into separated variable.
Any ideas??
Petr Laznovsky
When I run the command
for /f "tokens=3,* delims= " %i in ('netsh in sh in') do echo %i %j
then my console output looks radically different from yours,
The output I post is not output from full parsing command, but from
"netsh in sh in" in English OS version. I want to demonstrate the "two
Admin. stav    Stav          Typ             Název rozhraní
-------------------------------------------------------------------------
Povoleno                      Vyhrazený        Bluetooth Network
Povoleno                      Vyhrazený        ETH0
Povoleno                      Vyhrazený        WLAN0
Povoleno                      Vyhrazený        Bezdrátové připojení k síti 4
Povoleno                      Vyhrazený        Bezdrátové připojení k síti 2
Povoleno                      Vyhrazený        Hamachi
Povoleno                      Vyhrazený        Bezdrátové připojení k síti 3
Povoleno                      Vnitřní          Vnitřní
Povoleno                      Zpětná smyčka    Zpětná smyčka
--------------------------------------------------------------------------
-----------------------------------------------------------
c:\>echo Stav Typ             Název rozhraní
Stav Typ             Název rozhraní
c:\>echo Bluetooth Network
Bluetooth Network
c:\>echo ETH0
ETH0
c:\>echo WLAN0
WLAN0
c:\>echo Bezdrátové připojení k síti 4
Bezdrátové připojení k síti 4
c:\>echo Bezdrátové připojení k síti 2
Bezdrátové připojení k síti 2
c:\>echo Hamachi
Hamachi
c:\>echo Bezdrátové připojení k síti 3
Bezdrátové připojení k síti 3
c:\>echo Vnitřní
Vnitřní
c:\>echo smyčka Zpětná smyčka
smyčka Zpětná smyčka
----------------------------------------------------------
on last line you can see: "back Loop back" instead of "Loop back" or
"Loopback"
even when
{snip}

Here is a way to get the lines parsed the way you want. What you do
with it, is up to you ...

@echo off
setlocal enabledelayedexpansion
for /f "skip=2 delims=-" %%a in (
'type test.txt'
) do (
set _tmp=%%a
set _tmp=!_tmp: =$!
for /f "tokens=1-3 delims=$" %%B in (
'echo.!_tmp!'
) do (
if not "%%B"==" " echo 1:%%B 2:%%C 3:%%D
)
)

where the test file is exactly what you posted above. That could be
replaced with your netsh statement (between the single quote marks).
Each line of the output would be parsed and displayed in the last ECHO
statement as FOR variables %%B, %%C and %%D. These variables ARE case
sensitive. The dollar sign is an arbitrary character assumed to never
appear in the text to be parsed. Replace it with another, if there is
a problem with it (two places).

Hope that helps,
_____________________
Tom Lavedas

Loading...