1. 程式人生 > >Windows下通過指令碼快速修改IP地址 案例

Windows下通過指令碼快速修改IP地址 案例

@echo off 

:main 

cls 

 color 02

echo.---------------------------------------------

echo.  歡迎使用PrudentWoo製作的IP地址切換指令碼 

echo.  指令碼式針對Windows平臺進行指令碼切換
echo.  http://www.prudentwoo.com PrudentWoo?

echo.-------------------------------------- 

echo. 1. Wlan(無線網絡卡預置IP地址設定)           

echo. 2. Ethernet(乙太網網絡卡預置IP地址設定) 

echo. 3. IP地址自定義 

echo. 4. 清理DNS快取 

echo. 5. 開啟或關閉WIFI熱點 

echo. 6. IP地址資訊 

echo. 7. Exit 

echo.-------------------------------------- 

set /p choice=請輸入執行專案序號: 

echo. 

  if %choice%==1 goto Wlan 

  if %choice%==2 goto Ethernet 

  if %choice%==3 goto IP-Selfset 

  if %choice%==4 goto Clear DNS 

  if %choice%==5 goto WIFI 

  if %choice%==6 goto IP_Info 

  if %choice%==7  

      cls 

      goto end 

 

@rem 以下是無線網絡卡預置IP地址設定批處理 

:Wlan 

 cls 
color 03

 echo.您正在執行Wi-Fi IP地址設定操作! 

 echo.------------------------------ 

 echo 1. 設定為Wi-Fi DHCP 

 echo 2. 設定Wi-Fi靜態公網IP 

 echo 3. 返回 

 echo.----------------------------- 

 set /p choice=請選擇執行專案序號: 

 if %choice%==1 goto 1 

 if %choice%==2 goto 2 

 if %choice%==3 goto 3 

 

:1 

echo.正在進行DHCP設定,請稍等... echo.IP地址正在從DHCP自動獲取... 

netsh interface ip set address "Wi-Fi"dhcp 

echo.DNS地址正在從DHCP自動獲取... 

netsh interface ip set dns "乙太網"dhcp 

echo.全部設定完成!--- 

@Pause 

goto end 

 

:2 

echo.正在進行Wi-Fi公網IP設定,請稍等... 

echo.ip地址=192.168.1.123 

echo.子網掩碼=255.255.255.0 

echo.預設閘道器=192.168.1.1 

echo.首選DNS=10.252.8.5

echo.備選DNS=10.252.8.4 

net interface ip set address name="Wi-Fi" source=static 

addr=192.168.1.123 mask=255.255.255.0 >null 

net interface ip set address name="Wi-Fi" gateway=192.168.1.1

gwmetric=1 >null 

net interface ip set dns name="Wi-Fi" source=static addr=10.252.8.5

net interface ip add dns name="Wi-Fi" 10.252.8.4 index=2 

echo全部設定完成! 

echo請按任意鍵退出... 

@Pause 

goto end 

 

:3 

Goto main 

 

 

@rem 以下是乙太網網絡卡預置IP地址設定批處理 

:Ethernet 

 cls 
color 2f

 echo.您正在執行Ethernet IP地址設定操作! 

 echo.----------------------------------- 

 echo 1. 設定Ethernet為DHCP方式 

 echo 2. 設定Ethernet靜態公網IP1(辦公網路) 

 echo 3. 設定Ethernet靜態公網IP2(宿舍網路) 

 echo 4. 返回 

 echo.----------------------------------- 

 set /p choice=請選擇執行專案序號: 

 if %choice%==1 goto 1 

 if %choice%==2 goto 2 

 if %choice%==3 goto 3  if %choice%==4 goto 4 

 

:1 

cls 
color 26

echo.正在進行DHCP設定,請稍等... 

echo.IP地址正在從DHCP自動獲取... 

netsh interface ip set address name = "乙太網" source=dhcp 

echo.DNS地址正在從DHCP自動獲取... 

netsh interface ip set dns "乙太網" source=dhcp 

echo.全部設定完成! 

@Pause 

goto enthernet 

 

:2 

cls 
color 35

echo 正在進行Ethernet公網靜態IP1(辦公網路)設定,請稍等... 

echo. 

echo.設定引數為: 

echo.--------------------------- 

     echo.ip地址=198.9.2.123 

     echo.子網掩碼=255.255.255.0 

     echo.預設閘道器=198.9.2.1 

     echo.首選DNS=202.112.20.131 

echo.--------------------------- 

echo.正在設定上述IP引數... 
ipconfig /release

netsh interface ip add address name="乙太網" addr=198.9.2.123 mask=255.255.255.0 gateway=198.9.2.1
netsh interface ip add address name="乙太網" addr=10.2.2.234 mask=255.255.255.0 gateway=10.2.2.254
netsh interface ip add address name="乙太網" addr=10.0.0.10 mask=255.0.0.0
netsh interface ip add address name="乙太網" addr=192.168.133.20 mask=255.255.255.0
netsh interface ip add address name="乙太網" addr=192.168.54.345 mask=255.255.255.0
netsh interface ip add address name="乙太網" addr=192.168.7.1 mask=255.255.255.0
netsh interface ip add dns name = "乙太網" addr = 202.106.0.20
netsh interface ip add dns name = "乙太網" addr = 211.147.6.3
netsh interface ip add dns name = "乙太網" addr = 202.106.0.21
netsh interface ip add dns name = "乙太網" addr = 202.105.12.226
netsh interface ip add dns name = "乙太網" addr = 61.144.56.100
netsh interface ip add dns name = "乙太網" addr = 202.96.128.68
netsh interface ip add dns name = "乙太網" addr = 202.96.128.86

echo.全部設定完成! 

@Pause 

goto ethernet 

 

:3 

echo 正在進行Ethernet公網靜態IP2設定(宿舍網路),請稍等... 

echo. 

echo.設定引數為: 

echo.--------------------------- 

echo.ip地址= 192.168.1.33 

echo.子網掩碼=255.255.255.0 

echo.預設閘道器=192.168.1.33 

echo.首選DNS=61.144.56.100 

echo.--------------------------- 
ipconfig /release

netsh interface ip add address name="乙太網" addr=192.168.1.33 mask=255.255.255.0 gateway=192.168.1.1
netsh interface ip add dns name = "乙太網" addr = 202.106.0.20
netsh interface ip add dns name = "乙太網" addr = 211.147.6.3
netsh interface ip add dns name = "乙太網" addr = 202.106.0.21
netsh interface ip add dns name = "乙太網" addr = 202.105.12.226
netsh interface ip add dns name = "乙太網" addr = 61.144.56.100
netsh interface ip add dns name = "乙太網" addr = 202.96.128.68
netsh interface ip add dns name = "乙太網" addr = 202.96.128.86

echo.全部設定完成! 

@Pause 

goto enthernet 

 

:4 

goto main 

 

@rem 以下是以IP地址自定義批處理 

 

:ip-selfset 

cls 

echo.您正在執行Ethernet IP地址自定義設定操作! 

:ip1 

cls 
color 13

echo.IP自定義設定開始.... 

echo. 

echo 開始設定IP及子網掩碼 

set /p saddr=請輸入靜態地址:  

if /i "%saddr%"=="" goto ip1 

set /p mask=請輸入子網掩碼: 

if /i "%mask%"=="" goto ip1 

set /p gateway=請輸入預設閘道器: 

if /i "%gateway%"==""goto ip1 

goto IP_judge 

 

:IP_judge 

cls 

echo.您輸入的IP地址引數如下: 

echo.————————————————————  

echo.   IP地址:%saddr%  

echo. 子網掩碼:%mask%  

echo. 預設閘道器:%gateway%  

echo.———————————————————— 

echo. 

echo.以上引數正確請按 Y 繼續,錯誤請按N返回重設! 

echo. 

set /p choice= 您的選擇: 

if "%choice%"=="N" goto ip1 

if "%choice%"=="n" goto ip1 

if "%choice%"=="Y" goto ipaddr 

if "%choice%"=="y" goto ipaddr goto IP_judge 

echo. 

 

:ipaddr 

cmd /c netsh interface ip set address name="Local Area connection" source=static addr=%saddr% 

mask=%mask% gateway=%gateway% gwmetric=1 >nul 

goto ipdns 

 

:ipdns 

cls 

echo 開始設定DNS伺服器 

set /p dnsaddr=請輸入DNS伺服器: 

if /i "%dnsaddr%"=="" goto :ipdns 

@rem 以下語句設定DNS 

cmd /c netsh interface ip set dns name="Local Area connection" source=static addr=%dnsaddr% 

register=PRIMARY >nul 

echo.開始設定DNS備用伺服器 

set /p dnsaddr2=請輸入備用DNS伺服器: 

if /i "%dnsaddr2%"=="" goto :ipdns 

goto DNS_judge 

 

:DNS_judge 

cls 

echo. 

echo.——————————————————————————  

echo.   

echo. DNS首選伺服器:%dnsaddr%  

echo.   

echo. DNS備用伺服器: %dnsaddr2%  

echo.   

echo.—————————————————————————— 

echo.以上DNS引數正確請按Y繼續,否則請按N返回重設! 

echo. 

set /p choice= 您的選擇: 

if "%choice%"=="N" goto ipdns 

if "%choice%"=="n" goto ipdns 

if "%choice%"=="Y" goto ipdns2 

if "%choice%"=="y" goto ipdns2 

goto DNS_judge 

 

:ipdns2 

cmd /c netsh interface ip add dns name="Local Area Connection" addr=%dnsaddr2% 

index=2 >nul 

echo.設定完成 @pause 

goto main 

if errorlevel 2 goto main 

if errorlevel 1 goto end 

 

:Clear DNS 

cls 

cmd /c ipconfig/flushdns 

echo.DNS快取清理完成! 

@pause 

goto main 

 

:WIFI 

 cls 

 echo.您正在執行WIFI熱點設定操作! 

 echo.----------------------------------- 

 echo 1. 開啟WIFI Hoc 

 echo 2. 關閉WIFI Hoc 

 echo.----------------------------------- 

set /p choice= 輸入您的選擇序號: 

if "%choice%"=="1" goto start 

if "%choice%"=="2" goto stop 

:start 

cmd /c netsh wlan start hostednetwork 

@Pause 

goto main 

:stop 

@Pause 

goto main 

cmd /c netsh wlan stop hostednetwork 

 

:IP_Info 

 cls
 color 5f 

 echo.以下是您的IP地址資訊: 

 cmd /c ipconfig /all 

 @pause 

 goto main