筆電在辦公室(靜態 IP)、家裡(DHCP)、會議室(不同靜態 IP)間頻繁移動,手動修改網路設定極為繁瑣。使用批次檔(.bat)呼叫 netsh 指令,即可一鍵切換網路設定檔。
@echo off netsh interface ip set address name="區域連線" source=dhcp netsh interface ip set dns name="區域連線" source=dhcp echo 已切換為 DHCP 自動取得 IP pause
說明: name="區域連線" 為網路介面卡名稱,中文版預設為「區域連線」,英文版為 "Local Area Connection",請依實際情況修改(可執行 netsh interface show interface 查詢)。
@echo off netsh interface ip set address "區域連線" static 192.168.0.84 255.255.255.0 192.168.0.254 netsh interface ip set dns "區域連線" static 192.168.0.254 netsh interface ip add dns "區域連線" 202.106.196.115 index=2 netsh interface ip add dns "區域連線" 202.106.0.20 index=3 echo 已切換為靜態 IP:192.168.0.84 pause
參數說明:
static IP 子網路遮罩 閘道:依您的網路環境修改。set dns 設定首選 DNS,add dns index=2/3 新增備用 DNS。netsh interface show interface 確認正確名稱。ipconfig /flushdns 清除快取。這兩支腳本已陪伴筆者多年,跨版本(XP/7/10/11)通用,是系統管理員與行動辦公族的必備工具。