一個切換內外網IP地址的批處理BAT

作了一個切換內外網的小腳本。沒想到這個老的沒剩幾顆牙的DOS居然功能如此強大。盛名之下名副其實啊!不虧是想當年叱吒風雲的操做系統啊!網絡

腳本內容1.創建兩個TXT文件,分別按行存儲內外網的IP,MASK,GATEWAY,DNS F,DNS B,FOR循環的用法到如今也沒吃透,網上找了不少資料,也僅僅是冰山一角的感受。測試

2.創建後綴爲.BAT的批處理文件,腳本內容以下spa

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
相關文章
相關標籤/搜索