Batch file to get local network connection name in Windows XP

I am currently writing a simple batch script to automatically set DNS for a LAN connection. Here is the script:

REM Set DNS 
netsh interface ip set dns name="Local Area Connection" static X.X.X.X
netsh interface ip add dns name="Local Area Connection" Y.Y.Y.Y index=2
netsh interface ip add dns name="Local Area Connection" Z.Z.Z.Z index=3

      

But the point is, if the LAN name is not the default (i.e. LAN connection), the script won't work.

Is there some way to find all the names of a LAN connection and establish all DNS connections of those connections using a batch file?

Any help would be appreciated :)

+3


source to share


2 answers


I tested this code on Windows 7. You may need to make some changes for Windows XP.

@Echo Off
For /f "skip=2 tokens=4*" %%a In ('NetSh Interface IPv4 Show Interfaces') Do (
    Call :UseNetworkAdapter %%a "%%b"
)
Exit /B

:UseNetworkAdapter
:: %1 = State
:: %2 = Name (quoted); %~2 = Name (unquoted)
If %1==connected (
    :: Do your stuff here, for example:
    Echo %2
)
Exit /B

      

I just want to point out that I always use Call

, not parenthesized script. Too often people get confused when environment variables don't behave as expected in script brackets. I find calling the label easier for the script to work with.

EDIT: Clarification.



The command For

reads each line of a file or the result of a command.
In ('command')

tells him to read each line of results command

.
skip=2

skips the first two lines of the output, in this case the column header.
tokens=4*

says to read the fourth thing on each line as one variable ( 4

), and everything after it as another variable ( *

). %%a

says to store the specified tokens in %%a

and %%b

accordingly.
Do (commands)

executes commands

for each line.

My conclusion NetSh Interface IPv4 Show Interface

:

Idx     Met         MTU          State                Name
---  ----------  ----------  ------------  ---------------------------
  1          50  4294967295  connected     Loopback Pseudo-Interface 1
 15          50        1500  disconnected  Bluetooth Network Connection
 24          10        1500  connected     Network Bridge

      

So, I take the fourth token (state) and all tokens after that (Name) and pass them to the script function call. Here they are extracted as command line parameters, namely %1

and %2

.
Note that each Name consists of two or three tokens due to spaces, so using *

instead of 5

.

+10


source


Windows XP 1st output of "netsh int ip show config" command:

Configuration for the Local Area Connection Interface



@echo off
for /F tokens^=2^ delims^=^" %%a in ('netsh int ip show config') do set "sUserFriendyName=%%a"
echo/set http://www.opendns.com/ DNS for interface "%sUserFriendyName%"
netsh int ip delete dns "%sUserFriendyName%" all
netsh interface ip set dns name="%sUserFriendyName%" source=static addr=208.67.222.222 register=PRIMARY
netsh interface ip add dns name="%sUserFriendyName%" addr=208.67.220.220 index=2
ipconfig /flushdns

      

0


source







All Articles