Page 1 of 1

FOR command assistance: Setting variable from command output

Posted: 17 May 2010 15:33
by BrentNewland
I've got a tricky one (for me). I'm trying to create a batch file which will reset all the network interfaces on the computer to DHCP. Setting a single interface to DHCP isn't an issue (see http://samanathon.com/reset-your-ip-add ... atch-file/ for exact commands), but I want to set all the interfaces to DHCP (some viruses set bad IP addresses or DNS servers).

Here are the commands to run to change the interface to DHCP:
netsh int ip set address name = "Local Area Connection" source = dhcp
netsh int ip set dns name = "Local Area Connection" source = dhcp
netsh int ip set wins name = "Local Area Connection" source = dhcp


Here is the command used to list the interfaces on the machine:
netsh int ip show interface

And here is the output of that command:

Code: Select all

MIB-II Interface Information
------------------------------------------------------
Index:                              1
User-friendly Name:                 Loopback
GUID Name:                          Loopback
Type:                               Loopback
MTU:                                32768
Speed:                              10000000
Physical Address:
Admin Status:                       Up
Operational Status:                 Operational
Last Change:                        0
In Octets:                          0
In Unicast Packets:                 0
In Non-unicast Packets:             0
In Packets Discarded:               0
In Erroneous Packets:               0
In Unknown Protocol Packets:        0
Out Octets:                         0
Out Unicast Packets:                0
Out Non-unicast Packets:            0
Out Packets Discarded:              0
Out Erroneous Packets:              0
Output Queue Length:                0
Description:                        Internal loopback interface for 127.0.0 netw
ork

Index:                              2
User-friendly Name:                 Local Area Connection 3
GUID Name:                          {CB5FD14D-B673-488E-87D2-B9DEB098582B}
Type:                               Ethernet
MTU:                                1500
Speed:                              100000000
Physical Address:                   00-22-68-72-2E-14
Admin Status:                       Up
Operational Status:                 Operational
Last Change:                        3307940815
In Octets:                          2307725244
In Unicast Packets:                 27991495
In Non-unicast Packets:             229894
In Packets Discarded:               0
In Erroneous Packets:               0
In Unknown Protocol Packets:        1483213
Out Octets:                         2112210243
Out Unicast Packets:                27929817
Out Non-unicast Packets:            15069
Out Packets Discarded:              0
Out Erroneous Packets:              0
Output Queue Length:                0
Description:                        NVIDIA nForce Networking Controller - Packet
 Scheduler Miniport



What I need to do is loop through the output of that command, gather every "User-friendly Name" (excluding loopback, setting that to DHCP may cause some problems), then run the first commands to reset that interface to DHCP.

I've created a mockup of what it may look like, but I know it's not going to work. Perhaps someone knows how to do this, r has some code lying around?

Code: Select all

FOR /F "tokens=2* delims=    " %%A IN (netsh int ip show interface) DO (
netsh int ip set address name = "%%B" source = dhcp
netsh int ip set dns name = "%%B" source = dhcp
netsh int ip set wins name = "%%B" source = dhcp
)







(note to self: also need to export the registry settings for the routing and remote access service [remoteaccess], enable and start the service, wait 15 seconds, run above commands, then restore settings for that service and stop it)

Re: FOR command assistance: Setting variable from command ou

Posted: 17 May 2010 16:10
by aGerman
I didn't test it, but try something like that:

Code: Select all

REM ##find the line beginning with "User-friendly Name:" and set the value behind the colon to %%B
FOR /F "tokens=1* delims=:" %%A IN ('netsh int ip show interface^|findstr /b /i /c:"User-friendly Name:"') DO (
  REM ##trim the incipient spaces of %%B and set the value to %%C
  FOR /F "tokens=*" %%C IN ("%%B") DO (
    netsh int ip set address name = "%%C" source = dhcp
    netsh int ip set dns name = "%%C" source = dhcp
    netsh int ip set wins name = "%%C" source = dhcp
  )
)


Regards
aGerman

Re: FOR command assistance: Setting variable from command ou

Posted: 18 May 2010 12:50
by BrentNewland
aGerman wrote:I didn't test it, but try something like that:

Code: Select all

REM ##find the line beginning with "User-friendly Name:" and set the value behind the colon to %%B
FOR /F "tokens=1* delims=:" %%A IN ('netsh int ip show interface^|findstr /b /i /c:"User-friendly Name:"') DO (
  REM ##trim the incipient spaces of %%B and set the value to %%C
  FOR /F "tokens=*" %%C IN ("%%B") DO (
    netsh int ip set address name = "%%C" source = dhcp
    netsh int ip set dns name = "%%C" source = dhcp
    netsh int ip set wins name = "%%C" source = dhcp
  )
)


Regards
aGerman




You are a batch god! That worked perfectly.

Here's my tweaked code:

Code: Select all

@ECHO OFF
ECHO Resetting Winsock
netsh winsock reset
ECHO.
ECHO Flushing Winsock Catalog
netsh winsock reset catalog
ECHO.
ECHO Resetting TCP/IP
netsh interface ip reset %temp%\reset.txt
ECHO.
ECHO Resetting All Interfaces
netsh interface reset all
ECHO.
ECHO Resetting Firewall
netsh firewall reset
ECHO.
REM ##find the line beginning with "User-friendly Name:" and set the value behind the colon to %%B
FOR /F "tokens=1* delims=:" %%A IN ('netsh int ip show interface ^| findstr /B /I /c:"User-friendly Name:"') DO (
  REM ##trim the incipient spaces of %%B and set the value to %%C
  FOR /F "tokens=*" %%C IN ("%%B") DO (
    ECHO Setting Interface "%%C" IP to DHCP
    netsh int ip set address name = "%%C" source = dhcp
    ECHO Setting Interface "%%C" DNS to DHCP
    netsh int ip set dns name = "%%C" source = dhcp
    ECHO Setting Interface "%%C" WINS to DHCP
    netsh int ip set wins name = "%%C" source = dhcp
    ECHO.
  )
)
ECHO Flushing DNS
ipconfig /flushdns


Now I just need to have it reset the proxy through the registry and I'll have an all-in-one internet fix script :D

Re: FOR command assistance: Setting variable from command ou

Posted: 18 May 2010 13:23
by aGerman
You can change registry keys using the REG ADD command. But I'm not familar with that internet stuff. I know that it should be something at
HKEY_CURRENT_USER\ Software\ Microsoft\ Windows\ CurrentVersion\ Internet Settings
but you have to assist me which keys / values have to be changed.
BTW I'm not that sure if you have to restart the computer to renew the settings.

Re: FOR command assistance: Setting variable from command ou

Posted: 18 May 2010 23:11
by BrentNewland
aGerman wrote:You can change registry keys using the REG ADD command. But I'm not familar with that internet stuff. I know that it should be something at
HKEY_CURRENT_USER\ Software\ Microsoft\ Windows\ CurrentVersion\ Internet Settings
but you have to assist me which keys / values have to be changed.
BTW I'm not that sure if you have to restart the computer to renew the settings.


I'll be able to figure out the registry part, should be no problem.

As far as restarting, technically you are supposed to restart, but we tried it today on a system (would not update windows or any antivirus software) and it worked fine with no reboot.


What really sucks is that I made a script a while back that would get the current user's GUID (which I remember being a real PITA) and I don't have it any more. Thankfully I shouldn't need it.

Re: FOR command assistance: Setting variable from command ou

Posted: 19 May 2010 13:52
by BrentNewland
Little problem, Vista doesn't output the same format for the netsh int ip show interface.

Code: Select all

E:\>netsh int ip show interfaces

Idx  Met   MTU   State        Name
---  ---  -----  -----------  -------------------
  1   50 4294967295  connected    Loopback Pseudo-Interface 1
 11   25   1500  connected    Wireless Network Connection
  8   30   1500  disconnected  Local Area Connection


I think the easiest thing to do would be to have it check whether the OS is XP or not - I don't plan on using this on 2000/9x or Server 2003, and I think that 7 will have the same output as Vista.

Code: Select all

@ECHO OFF
REM Disable proxy http://support.microsoft.com/kb/819961
ECHO Resetting Winsock
netsh winsock reset
ECHO.
ECHO Flushing Winsock Catalog
netsh winsock reset catalog
ECHO.
ECHO Resetting TCP/IP
netsh interface ip reset %temp%\reset.txt
ECHO.
ECHO Resetting All Interfaces
netsh interface reset all
ECHO.
ECHO Resetting Firewall
netsh firewall reset
ECHO.
REM ver | find "XP" > nul
REM if %ERRORLEVEL% == 0 (
REM ##find the line beginning with "User-friendly Name:" and set the value behind the colon to %%B
FOR /F "tokens=5* delims= " %%A IN ('netsh int ip show interface ^| findstr /B /I /c:"User-friendly Name:"') DO (
  REM ##trim the incipient spaces of %%B and set the value to %%C
  FOR /F "tokens=*" %%C IN ("%%B") DO (
    ECHO Setting Interface "%%C" IP to DHCP
    netsh int ip set address name = "%%C" source = dhcp
    ECHO Setting Interface "%%C" DNS to DHCP
    netsh int ip set dns name = "%%C" source = dhcp
    ECHO Setting Interface "%%C" WINS to DHCP
    netsh int ip set wins name = "%%C" source = dhcp
    ECHO.
   )
 )
ECHO Resetting Proxy
proxycfg -d
) else (
REM ##
FOR /F "tokens=1* delims=:" %%A IN ('netsh int ip show interface ^| findstr /B /I /c:"User-friendly Name:"') DO (
  REM ##trim the incipient spaces of %%B and set the value to %%C
  FOR /F "tokens=*" %%C IN ("%%B") DO (
    ECHO Setting Interface "%%C" IP to DHCP
    netsh int ip set address name = "%%C" source = dhcp
    ECHO Setting Interface "%%C" DNS to DHCP
    netsh int ip set dns name = "%%C" source = dhcp
    ECHO Setting Interface "%%C" WINS to DHCP
    netsh int ip set wins name = "%%C" source = dhcp
    ECHO.
   )
 )
 ECHO Resetting Proxy
 netsh winhttp import proxy ie
 netsh winhttp reset proxy
)
ECHO Flushing DNS
ipconfig /flushdns


which I know won't work (damn you FOR loops :evil: If only this were PHP). The "else" also doesn't work, and I can't seem to figure out the syntax.

Re: FOR command assistance: Setting variable from command ou

Posted: 19 May 2010 15:33
by aGerman
Try this to figure out on which Win OS you work. AFAIK XP has version no. 5.x.x and Vista has 6.x.x
So you could figure out if it is 5, higher or lower using the VER command.

Code: Select all

@echo off &setlocal

for /f "delims=[. tokens=2" %%a in ('ver') do (
  for /f "tokens=2" %%b in ("%%a") do (
    if "%%b"=="5" (
      call :XP
    ) else (
      if "%%b" gtr "5" (
        call :higher
      ) else (
        call :lower
      )
    )
  )
)

pause
goto :eof


:XP
echo OS is XP
goto :eof

:higher
echo OS is Vista or Win7
goto :eof

:lower
echo doesn't work
goto :eof

Now you can use the subroutines :XP, :higher and :lower to replace the echo lines with your stuff.


Regarding the Vista format:
Which is the value you would need and which is the key word to find the right line?


BTW:
BrentNewland wrote:What really sucks is that I made a script a while back that would get the current user's GUID (which I remember being a real PITA) and I don't have it any more. Thankfully I shouldn't need it.

Code: Select all

@echo off &setlocal
for /f "tokens=1,2*" %%a in ('reg query HKCU^|findstr /r /c:"\<\{[0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*\}$"') do (
  call :findSID "%%a" "%%c"
)
echo.
pause
goto :eof

:findSID
for /f "delims=" %%a in ('reg query HKU^|findstr "\\"') do (
  for /f "delims=" %%b in ('reg query "%%a"^|findstr /r /c:"%~1.*%~2"') do (
    echo %computername%
    echo %username%
    echo %%~nxa
    echo %~1
    echo %~2
  )
)
goto :eof

This is what you could use to find the values into the registry.

Regards
aGerman

Re: FOR command assistance: Setting variable from command ou

Posted: 26 May 2010 05:31
by alan_b
BrentNewland wrote:

Code: Select all

.......................
REM ver | find "XP" > nul
REM if %ERRORLEVEL% == 0 (
REM ##find the line beginning with "User-friendly Name:" and set the value behind the colon to %%B
FOR /F "tokens=5* delims= " %%A IN ('netsh int ip show interface ^| findstr /B /I /c:"User-friendly Name:"') DO (
  REM ##trim the incipient spaces of %%B and set the value to %%C
  FOR /F "tokens=*" %%C IN ("%%B") DO (
   ...............
   )
 )
ECHO Resetting Proxy
proxycfg -d
) else (
.....................


The "else" also doesn't work, and I can't seem to figure out the syntax.


You started with correct syntax for the "else", but spoilt it by prefixing REM in the line
REM if %ERRORLEVEL% == 0 (

Alan