我正在构建一个批处理来自动切换代理配置和相关的 IP 设置,但是我无法提取所需网络适配器的 IP 地址。我有一个“真实”网卡和两个虚拟 Vmware 网卡。
使用以下代码将返回列表中最后一个地址(第二个虚拟网卡),而不是第一个地址。尝试了分隔符/标记设置但无济于事,有人知道如何处理吗?
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
我还必须补充一点,我并不是一位经验丰富的脚本编写者,所以我可能会犯一些(非常)低级的错误。
答案1
尽管我进行了更多的实验,但效果还是很粗糙,因为只返回了第一行的地址。
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
对于那些有兴趣在 win7 中有效地即时切换代理设置的人来说,这里有完整的脚本。它将需要手动编辑 nic 名称、代理、默认网关和 dns 地址条目以进行设置。
@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