萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Windows XP 教學

netsh 指令極速切換 IP/DNS 批次檔一鍵套用

筆記型電腦在公司、家裡、客戶端、咖啡廳頻繁切換網路環境,每次手動改 IP、DNS 太繁瑣。Windows 內建 netsh 指令支援命令列完整設定網路參數,寫成 `.bat` 批次檔,雙擊即可一鍵切換,效率倍增。

範例 1:設定靜態 IP、子網路遮罩、預設閘道、DNS

建立 set_static_ip.bat (以系統管理員身分執行):

@echo off
rem 修改以下變數為目標環境參數
set IFACE="區域連線"
set IP=192.168.1.100
set MASK=255.255.255.0
set GW=192.168.1.1
set DNS1=168.95.1.1
set DNS2=8.8.8.8

netsh interface ip set address %IFACE% static %IP% %MASK% %GW% 1
netsh interface ip set dns %IFACE% static %DNS1% primary
netsh interface ip add dns %IFACE% %DNS2% index=2
echo 靜態 IP 設定完成!
pause

說明: `IFACE` 需與「網路連線」視窗顯示的連線名稱完全一致 (常見為「區域連線」、「乙太網路」、「Wi-Fi」)。

範例 2:切換為 DHCP 自動取得 IP/DNS

建立 set_dhcp.bat:

@echo off
set IFACE="區域連線"
netsh interface ip set address %IFACE% dhcp
netsh interface ip set dns %IFACE% dhcp
echo 已切換為 DHCP 自動取得
pause

進階:互動式選單批次檔

@echo off
:MENU
cls
echo ============ 網路環境快速切換 ============
echo 1. 公司靜態 IP
echo 2. 家裡 DHCP
echo 3. 客戶端靜態 IP
echo 4. 咖啡廳 DHCP
echo 0. 離開
set /p choice=請輸入選項:
if "%choice%"=="1" call :COMPANY
if "%choice%"=="2" call :HOME
if "%choice%"=="3" call :CLIENT
if "%choice%"=="4" call :CAFE
if "%choice%"=="0" exit
goto MENU

:COMPANY
set IFACE="乙太網路"
netsh interface ip set address %IFACE% static 10.10.10.50 255.255.255.0 10.10.10.1 1
netsh interface ip set dns %IFACE% static 10.10.10.10
netsh interface ip add dns %IFACE% 8.8.8.8 index=2
goto END

:HOME
set IFACE="Wi-Fi"
netsh interface ip set address %IFACE% dhcp
netsh interface ip set dns %IFACE% dhcp
goto END

:CLIENT
set IFACE="乙太網路"
netsh interface ip set address %IFACE% static 192.168.5.20 255.255.255.0 192.168.5.1 1
netsh interface ip set dns %IFACE% static 192.168.5.1
goto END

:CAFE
set IFACE="Wi-Fi"
netsh interface ip set address %IFACE% dhcp
netsh interface ip set dns %IFACE% dhcp
goto END

:END
echo 設定完成!
pause
goto MENU

常用 netsh 指令速查

指令功能
netsh interface ip show config顯示所有介面目前 IP 設定
netsh interface ip set address "名稱" static IP 遮罩 閘道 躍點設定靜態 IP
netsh interface ip set address "名稱" dhcp設定 DHCP
netsh interface ip set dns "名稱" static DNS設定靜態 DNS (主要)
netsh interface ip add dns "名稱" DNS index=2新增次要 DNS
netsh interface ip delete dns "名稱" all刪除所有 DNS
netsh interface ip delete address "名稱" gateway=all刪除所有閘道

若執行提示「指定了無效的介面」,請啟用「Remote Registry」服務 (services.msc → Remote Registry → 自動/啟動)。將常用環境做成批次檔放桌面,切換網路環境從此一鍵搞定!

copyright © 萬盛學電腦網 all rights reserved