Windows系统下使用批处理脚本配置同时连接内外网

1. 本脚本仅限Windows系统下使用

2. 电脑要有两张网卡,有限网卡连接内网,无线网卡连接外网

3. 脚本内容如下所示,将其保存为.bat文件,用管理员权限执行即可。本脚本仅在英文系统上测试过,需要先输入有限网卡的名字。

@echo off
chcp 65001
setlocal enabledelayedexpansion
netsh interface show interface
REM 设置有限网卡的名称
:setName
set wiredLanName=
set /p wiredLanName=please type in wired adapter name:
echo adapter name:%wiredLanName%
if "%wiredLanName%" == "" goto setName
set isDHCP=
set intranetIp=
set intranetGatewayIp=
set intranetMask=
:getConfig
netsh interface ipv4 show addresses %wiredLanName%
FOR /F "usebackq skip=2 delims=: tokens=1,2" %%i IN (`netsh interface ipv4 show addresses %wiredLanName%`) DO (
	FOR /F "tokens=1" %%k IN ("%%i") DO (
		FOR /F "tokens=1,3" %%l IN ("%%j") DO (
			if "%%k" == "DHCP" (
				@set isDHCP=%%l
			)
			if "%%k" == "IP" (
				@set intranetIp=%%l
			)
			if "%%k" == "Default" (
				@set intranetGatewayIp=%%l
			)
			if "%%k" == "Subnet" (
				@set intranetMask=%%m
			)
		)
	)
)
echo isDHCP:%isDHCP%
echo intranetIp:%intranetIp%
echo intranetGatewayIp:%intranetGatewayIp%
set intranetMask=%intranetMask:~0,-1%
echo intranetMask=%intranetMask%


if "%isDHCP%" == "Yes" goto setStaticIp
:setDHCP
REM set dynamic ip
netsh interface ipv4 set address name=%wiredLanName% source=dhcp
ping 127.0.0.1 -n 10 > nul
goto getConfig
:setStaticIp
REM set static ip
netsh interface ipv4 set address %wiredLanName% static %intranetIp% %intranetMask% none
ping 127.0.0.1 -n 10 > nul


:setRoute
route -p delete 10.3.0.0 
route -p delete 10.200.0.0
route -p delete 10.127.0.0
route -p delete 10.203.0.0


route -p add 10.3.0.0 mask 255.255.0.0 %intranetGatewayIp%
route -p add 10.127.0.0 mask 255.255.0.0 %intranetGatewayIp%
route -p add 10.200.0.0 mask 255.255.0.0 %intranetGatewayIp%
route -p add 10.203.0.0 mask 255.255.0.0 %intranetGatewayIp%


ping 127.0.0.1 -n 3 > nul


:exit
endlocal
pause
	FOR /F "tokens=1" %%k IN ("%%i") DO (
		FOR /F "tokens=1,3" %%l IN ("%%j") DO (
			if "%%k" == "DHCP" (
				@set isDHCP=%%l
			)
			if "%%k" == "IP" (
				@set intranetIp=%%l
			)
			if "%%k" == "Default" (
				@set intranetGatewayIp=%%l
			)
			if "%%k" == "Subnet" (
				@set intranetMask=%%m
			)
		)
	)
)
echo isDHCP:%isDHCP%
echo intranetIp:%intranetIp%
echo intranetGatewayIp:%intranetGatewayIp%
set intranetMask=%intranetMask:~0,-1%
echo intranetMask=%intranetMask%


if "%isDHCP%" == "Yes" goto setStaticIp
:setDHCP
REM set dynamic ip
netsh interface ipv4 set address name=%wiredLanName% source=dhcp
ping 127.0.0.1 -n 10 > nul
goto getConfig
:setStaticIp
REM set static ip
netsh interface ipv4 set address %wiredLanName% static %intranetIp% %intranetMask% none
ping 127.0.0.1 -n 10 > nul


:setRoute
route -p delete 10.3.0.0 
route -p delete 10.200.0.0
route -p delete 10.127.0.0
route -p delete 10.203.0.0


route -p add 10.3.0.0 mask 255.255.0.0 %intranetGatewayIp%
route -p add 10.127.0.0 mask 255.255.0.0 %intranetGatewayIp%
route -p add 10.200.0.0 mask 255.255.0.0 %intranetGatewayIp%
route -p add 10.203.0.0 mask 255.255.0.0 %intranetGatewayIp%


ping 127.0.0.1 -n 3 > nul


:exit
chcp 936
endlocal
pause

 

你可能感兴趣的:(解决方案)