REM 设定网络适配器名称,请确保这个名称与你的网络适配器名称匹配 SET "AdapterName=以太网"
REM 配置1 SET "Gateway1=192.168.0.1" SET "DNS1_1=8.8.8.8" SET "DNS1_2=8.8.4.4"
REM 配置2 SET "Gateway2=192.168.0.3" SET "DNS2_1=192.168.0.3" SET "DNS2_2=8.8.8.8"
REM 调试信息 echo 当前网络配置: netsh interface ipv4 show config name="%AdapterName%" echo.
REM 获取当前网关 for /f "tokens=2 delims=:" %%G in ('netsh interface ipv4 show config name^="%AdapterName%" ^| findstr "默认网关"') do ( for /f "tokens=* delims= " %%H in ("%%G") do ( set currentGateway=%%H ) )
REM 调试信息 echo 当前默认网关: %currentGateway%
REM 根据当前网关切换配置 if "%currentGateway%"=="%Gateway1%" ( echo 当前配置为配置1,切换到配置2... netsh interface ipv4 set address name="%AdapterName%" source=static addr=192.168.0.10 mask=255.255.255.0 gateway=%Gateway2% netsh interface ipv4 set dns name="%AdapterName%" static %DNS2_1% netsh interface ipv4 add dns name="%AdapterName%" %DNS2_2% index=2 ) else if "%currentGateway%"=="%Gateway2%" ( echo 当前配置为配置2,切换到配置1... netsh interface ipv4 set address name="%AdapterName%" source=static addr=192.168.0.10 mask=255.255.255.0 gateway=%Gateway1% netsh interface ipv4 set dns name="%AdapterName%" static %DNS1_1% netsh interface ipv4 add dns name="%AdapterName%" %DNS1_2% index=2 ) else ( echo 无法识别当前网关配置: %currentGateway% )
REM 确认配置已更改 echo 配置已更改: netsh interface ipv4 show config name="%AdapterName%"