当前位置:DOS资源站资料中心批处理教程 → 跨网段网关DNS切换BAT脚本

跨网段网关DNS切换BAT脚本

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2008-6-1 20:06:37

方式一 :

@echo off
@color fc
@title 海洋技术俱乐部-Opteron-跨网段网关DNS快速切换-%date% %time%
::网段
@set oa=192.168.1
::网关
@set GateWaya=192.168.1.1
::子网及DNS
@set maska=255.255.255.0
@set dnsa=61.153.177.196
@set dnsc=61.153.177.197
::网段
@set ob=192.168.1
::网关
@set GateWayb=192.168.1.2
::子网及DNS
@set maskb=255.255.255.0
@set dnsb=61.153.177.198
@set dnsd=61.153.177.199
@setlocal enabledelayedexpansion
@arp.exe -d
@ping %dnsa% -n 1&ping %dnsb% -n 1
@cls
@if exist ipconfig.txt del ipconfig.txt 
@ipconfig>ipconfig.txt
@if exist GateWay.txt del gateway.txt
@find /i "Default Gateway" ipconfig.txt>gateway.txt
@for /f "skip=2 tokens=13" %%X in (GateWay.txt) do set GateWay=%%X
@find /i "IP Address" ipconfig.txt>ipaddr.txt
@for /f "skip=2 tokens=15" %%k in (ipaddr.txt) do set var=%%k
@set var1=!var:~9!
@if /i "%GateWay%"=="%GateWaya%" goto A
@if /i "%GateWay%"=="%GateWayb%" goto B
:A
@cls
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@echo ※                                                                           ※
@echo         注意:你现在的网关是[%GateWaya%] 正在切换到[%GateWayb%]请稍等……
@echo ※                                                                           ※
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@set str=%ob%%var1%
@if exist ipconfig.txt del ipconfig.txt
@if exist GateWay.txt del gateway.txt
@if exist ipaddr.txt del ipaddr.txt
@if exist net.txt del net.txt
@echo pushd interface ip >net.txt
@echo set address name="本地连接" source=static addr=%str% mask=%maskb% >>net.txt
@echo set address name="本地连接" gateway=%gatewayb% gwmetric=0 >>net.txt
@echo set dns name="本地连接" source=static addr=%dnsb% register=PRIMARY >>net.txt
@echo add dns name="本地连接" addr=%dnsd% index=2 >>net.txt
@echo set wins name="本地连接" source=static addr=none >>net.txt
@echo popd >>net.txt
@netsh -f net.txt
@route -p add 0.0.0.0 mask 0.0.0.0 %GateWayb% metric 20
@route delete 0.0.0.0 mask 0.0.0.0 %GateWaya% metric 20
@goto C
:B
@cls
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@echo ※                                                                           ※
@echo         注意:你现在的网关是[%GateWayb%] 正在切换到[%GateWaya%]请稍等……
@echo ※                                                                           ※
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@set str=%oa%%var1%
@if exist ipconfig.txt del ipconfig.txt
@if exist GateWay.txt del gateway.txt
@if exist ipaddr.txt del ipaddr.txt
@if exist net.txt del net.txt
@echo pushd interface ip >net.txt
@echo set address name="本地连接" source=static addr=%str% mask=%maska% >>net.txt
@echo set address name="本地连接" gateway=%gatewaya% gwmetric=0 >>net.txt
@echo set dns name="本地连接" source=static addr=%dnsa% register=PRIMARY >>net.txt
@echo add dns name="本地连接" addr=%dnsc% index=2 >>net.txt
@echo set wins name="本地连接" source=static addr=none >>net.txt
@echo popd >>net.txt
@netsh -f net.txt
@route -p add 0.0.0.0 mask 0.0.0.0 %GateWaya% metric 20
@route delete 0.0.0.0 mask 0.0.0.0 %GateWayb% metric 20
@goto C
:C
@if exist net.txt del net.txt
@exit

方式二 :

@echo off
@color fc
@title 海洋技术俱乐部-Opteron-跨网段网关DNS快速切换-%date% %time%
::网段
@set oa=192.168.1
::网关
@set GateWaya=192.168.1.1
::子网及DNS
@set maska=255.255.255.0
@set dnsa=61.153.177.196
@set dnsc=61.153.177.197
::网段
@set ob=192.168.1
::网关
@set GateWayb=192.168.1.2
::子网及DNS
@set maskb=255.255.255.0
@set dnsb=61.153.177.198
@set dnsd=61.153.177.199
setlocal enabledelayedexpansion
@arp.exe -d
@ping %dnsa% -n 1&ping %dnsb% -n 1
@cls
@if exist ipconfig.txt del ipconfig.txt 
@ipconfig>ipconfig.txt
@if exist GateWay.txt del gateway.txt
@find /i "Default Gateway" ipconfig.txt>gateway.txt
@for /f "skip=2 tokens=13" %%X in (GateWay.txt) do set GateWay=%%X
@find /i "IP Address" ipconfig.txt>ipaddr.txt
@for /f "skip=2 tokens=15" %%k in (ipaddr.txt) do set var=%%k
@set var1=!var:~9!
@if /i "%GateWay%"=="%GateWaya%" goto A
@if /i "%GateWay%"=="%GateWayb%" goto B
:A
@cls
@set str=%ob%%var1%
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@echo ※                                                                           ※
@echo         注意:你现在的网关是[%GateWaya%] 正在切换到[%GateWayb%]请稍等……
@echo ※                                                                           ※
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@if exist ipconfig.txt del ipconfig.txt
@if exist GateWay.txt del gateway.txt
@if exist ipaddr.txt del ipaddr.txt
@netsh interface ip set address name="本地连接" source=static addr=%str% mask=%maskb%
@netsh interface ip set address name="本地连接" gateway=%GateWayb% gwmetric=0
@netsh interface ip set dns name="本地连接" source=static addr=%dnsb% register=PRIMARY
@netsh interface ip add dns name="本地连接" addr=%dnsd% index=2
@netsh interface ip set wins name="本地连接" source=static addr=none
@route -p add 0.0.0.0 mask 0.0.0.0 %GateWayb% metric 20
@route delete 0.0.0.0 mask 0.0.0.0 %GateWaya% metric 20
@goto C
:B
@cls
@set str=%oa%%var1%
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@echo ※                                                                           ※
@echo         注意:你现在的网关是[%GateWayb%] 正在切换到[%GateWaya%]请稍等……
@echo ※                                                                           ※
@echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
@if exist ipconfig.txt del ipconfig.txt
@if exist GateWay.txt del gateway.txt
@if exist ipaddr.txt del ipaddr.txt
@netsh interface ip set address name="本地连接" source=static addr=%str% mask=%maska%
@netsh interface ip set address name="本地连接" gateway=%GateWaya% gwmetric=0
@netsh interface ip set dns name="本地连接" source=static addr=%dnsa% register=PRIMARY
@netsh interface ip add dns name="本地连接" addr=%dnsc% index=2
@netsh interface ip set wins name="本地连接" source=static addr=none
@route -p add 0.0.0.0 mask 0.0.0.0 %GateWaya% metric 20
@route delete 0.0.0.0 mask 0.0.0.0 %GateWayb% metric 20
@goto C
:C
@exit