一个切换内外网IP地址的批处理BAT - 老杨的猫

时间:2024-04-17 09:14:01

做了一个切换内外网的小脚本。没想到这个老的没剩几颗牙的DOS竟然功能如此强大。盛名之下名副其实啊!不亏是想当年叱咤风云的操作系统啊!

脚本内容1.建立两个TXT文件,分别按行存储内外网的IP,MASK,GATEWAY,DNS F,DNS B,FOR循环的用法到现在也没吃透,网上找了很多资料,也仅仅是冰山一角的感觉。

2.建立后缀为.BAT的批处理文件,脚本内容如下

3.很笨拙的实现方式,有很大改善空间

@echo off
color f5
echo.

:main
echo.
echo.
echo ======= 请按数字键选择(1-5):=======
echo.
choice /c 12345 /m "(1)=切换内网 (2)=切换外网 (3)=测试网络  (4)=退出"

if errorlevel 4 goto end
if errorlevel 3 goto pingin
if errorlevel 2 goto out
if errorlevel 1 goto in

:in
setlocal enabledelayedexpansion
set a=0 
set b=0 
set c=0 
set d=0 
set e=0
for /f %%i in (内网IP.txt) do set /a a+=1 & if !a!==1 set ip=%%i
for /f %%i in (内网IP.txt) do set /a b+=1 & if !b!==2 set mask=%%i
for /f %%i in (内网IP.txt) do set /a c+=1 & if !c!==3 set gateway=%%i
for /f %%i in (内网IP.txt) do set /a d+=1 & if !d!==4 set dns1=%%i
for /f %%i in (内网IP.txt) do set /a e+=1 & if !e!==5 set dns2=%%i
echo %ip%
echo %mask%
echo %gateway%
echo %dns1%
echo %dns2%
goto runin %ip% %mask% %gateway% %dns1% %dns2%


:out
setlocal enabledelayedexpansion
set a=0 
set b=0 
set c=0 
set d=0 
set e=0
for /f %%i in (外网IP.txt) do set /a a+=1 & if !a!==1 set ip=%%i
for /f %%i in (外网IP.txt) do set /a b+=1 & if !b!==2 set mask=%%i
for /f %%i in (外网IP.txt) do set /a c+=1 & if !c!==3 set gateway=%%i
for /f %%i in (外网IP.txt) do set /a d+=1 & if !d!==4 set dns1=%%i
for /f %%i in (外网IP.txt) do set /a e+=1 & if !e!==5 set dns2=%%i
echo %ip%
echo %mask%
echo %gateway%
echo %dns1%
echo %dns2%
goto runin %ip% %mask% %gateway% %dns1% %dns2%


:pingin
ping %dns1%
goto main

:end
exit

:runin
rem netsh interface ip set address name="本地连接" source=dhcp
rem ipconfig/flushdns
netsh interface ip set dns name="本地连接" source=dhcp
echo 正在更改IP地址为%1......请稍后
netsh interface ip set address "本地连接" static %ip% %mask% %gateway% 1
echo IP地址修改成功!!! 
echo 正在更改DNS地址为%4......请稍后

netsh interface ip set dns name="本地连接" source=dhcp
netsh interface ip set dns name="本地连接" source=static addr=%dns1%
netsh interface ip add dns 本地连接 %dns2%
echo DNS地址修改成功!!!
goto main