Extract ip address from specific NIC in win7 batch file

2

I am building a batch to automatically switch a proxy config and the related ip settings bui i am having trouble extracting the ip address of the network adapter i need. I have one "real" nic, and two virtual Vmware nic's.

Using following code returns the address of the last one in the list (second virtual nic), instead of the first one. Been playing around with delimiters/tokens settings to no avail, anyone know how to handle this?

ipconfig > C:\Windows\Temp\ipconf.txt
for /f "delims=: tokens=2" %%a in ('ipconfig ^| findstr /R /C:"IPv4 Address"') DO ECHO %%a > C:\Windows\Temp\ip.txt
set /p ip= <C:\Windows\Temp\ip.txt

ECHO %ip%

PAUSE

I also have to add i am not a very experienced scripter so i might be making (very) noobish mistakes.

Jake

Posted 2013-04-24T09:29:46.270

Reputation: 398

Answers

0

Some more experimenting did what i needed, in a crude fashion though, but it works because only the address on the first line is returned.

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IPv4 Address"') do echo %%a >> C:\Windows\Temp\ip.txt
set /p ip= < C:\Windows\Temp\ip.txt

For those interested in switching proxy settings on the fly effectively in win7 here's the full script. It will need some manual editing of nic name, proxy , default gateways and dns address entries for your setup.

@ECHO OFF


REM Elevation changer script, uac prompt called to confirm.

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------


REM Find current state of proxy settings and assign output to variable to make decision: dis- or enable?

REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable > C:\Windows\Temp\proxyinfo.txt
FOR /f "tokens=3 delims= " %%a IN (C:\Windows\Temp\proxyinfo.txt) DO ECHO %%a > C:\Windows\Temp\proxyval.txt
set /p var= <C:\Windows\Temp\proxyval.txt


REM Find current ip address and assign output to variable to re-insert in config commands 

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IPv4 Address"') do echo %%a >> C:\Windows\Temp\ip.txt
set /p ip= < C:\Windows\Temp\ip.txt


REM Decide action to follow

IF %var% == 0x0 ECHO Proxy is currently disabled.
IF %var% == 0x1 ECHO Proxy is currently enabled.


REM Bailout option

ECHO Switch the proxy state? This will also adjust dns and default gateway settings.
choice /c yn 

if errorlevel 2 GOTO BAIL
if errorlevel 1 GOTO DOIT


:BAIL
ECHO Nothing has changed, run this script again when you made up your mind.
ECHO Press any key to close this window.
GOTO CLEAN


:DOIT

IF %var% == 0x0 GOTO ISDISABLED
IF %var% == 0x1 GOTO ISENABLED


:ISDISABLED

REM Enable all required settings to activate proxy in registry (enable, address, override for local)

ECHO Changing settings to enable proxy now...
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
    /v ProxyEnable /t REG_DWORD /d 1 /f > nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
    /v ProxyServer /t REG_SZ /d 172.16.255.1:8080 /f > nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
    /v ProxyOverride /t REG_SZ /d  ^<local^>  /f > nul

REM Change dns settings

netsh interface ip set dns "Local Area Connection" static 172.16.100.100 primary n
netsh interface ip add dns "Local Area Connection" 172.16.100.101 index=2 n

REM Change default gateway

netsh interface ip set address "Local Area Connection" static address=%ip% mask=255.255.0.0 gateway=10.100.255.254
ECHO Proxy is now enabled, run this script again to disable. 
ECHO Press any key to close this window.
GOTO CLEAN


:ISENABLED

REM Disable proxy

ECHO Changing settings to disable proxy now...
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
    /v ProxyEnable /t REG_DWORD /d 0 /f > nul

REM Change dns settings

netsh interface ip set dns "Local Area Connection" static 8.8.8.8 primary n
netsh interface ip add dns "Local Area Connection" 8.8.4.4 index=2 n

REM Change default gateway

netsh interface ip set address "Local Area Connection" static address=%ip% mask=255.255.0.0 gateway=10.100.255.250
ECHO Proxy is now disabled, run this script again to enable. 
ECHO Press any key to close this window.
GOTO CLEAN


:CLEAN
del C:\Windows\Temp\proxy*.txt
del C:\Windows\Temp\ip*.txt
GOTO END


:END
PAUSE > nul

Jake

Posted 2013-04-24T09:29:46.270

Reputation: 398