脚本代码如下,手动更改公司分配的 IP 地址、子网掩码/网关、DNS 地址即可。
脚本下载地址:
FileCodeBox下载:https://file.sakurain.net/#/?code=UBVPU
Cloudrever下载:https://pan.sakurain.net/s/J4IL/ew3pt8er
@echo off
:: 修改网络配置.bat 一键 DHCP?静态 自动切换
:: 需管理员权限,否则自提权(UAC)
fltmc >nul 2>&1 || (
powershell -NoProfile -Command "Start-Process '\"%~f0\"' -Verb RunAs -Wait"
exit /b
)
:: ======== 用 户 配 置 ========
set "PROFILE=WLAN"
set "IP=10.20.14.43"
set "MASK=255.255.255.0"
set "GATEWAY=10.20.14.1"
set "DNS1=101.226.4.6"
set "DNS2=1.2.4.8"
:: =================================
echo.
echo 正在检测接口“%PROFILE%”当前配置…
:: 判断是否为 DHCP
netsh interface ip show config name="%PROFILE%" | findstr /ri "DHCP.*[是Yes]" >nul && set "CUR=DHCP" || set "CUR=STATIC"
echo 当前为 %CUR% 模式,即将自动切换…
echo.
if /i "%CUR%"=="DHCP" (
echo 1. 关闭 DHCP
netsh interface ip set address name="%PROFILE%" dhcp >nul 2>&1
netsh interface ip set dns name="%PROFILE%" dhcp >nul 2>&1
echo 2. 设置静态 IP
netsh interface ip set address name="%PROFILE%" static %IP% %MASK% %GATEWAY% 1 >nul 2>&1
netsh interface ip set dns name="%PROFILE%" static %DNS1% >nul 2>&1
netsh interface ip add dns name="%PROFILE%" %DNS2% index=2 >nul 2>&1
echo.
echo ===== 已切换为手动(静态)配置 =====
echo IP 地址 : %IP%
echo 子网掩码 : %MASK%
echo 默认网关 : %GATEWAY%
echo 首选 DNS : %DNS1%
echo 备用 DNS : %DNS2%
echo ======================================
) else (
echo 1. 恢复 DHCP 获取
netsh interface ip set address name="%PROFILE%" dhcp >nul 2>&1
netsh interface ip set dns name="%PROFILE%" dhcp >nul 2>&1
echo.
echo ===== 已恢复为 DHCP 自动获取 =====
echo 接口“%PROFILE%”现在通过 DHCP 获取 IP/DNS
echo ====================================
)
echo.
echo 操作完成,5 秒后自动关闭…
timeout /t 5 >nul
