Skip to content

Instantly share code, notes, and snippets.

@moolex
Created January 4, 2013 05:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save moolex/4450102 to your computer and use it in GitHub Desktop.
Save moolex/4450102 to your computer and use it in GitHub Desktop.
Windows下自动设置IP的批处理脚本
@echo off
::需要设置的网络,一般为“本地连接”或者“无线网络连接”
set name="无线网络连接"
::请根据您的参数修改以下数据
::设置 IP地址
set ipaddress=18.1.16.169
::设置 子掩码
set mask=255.255.252.0
::设置 网关
set gateway=18.1.19.254
::设置 首选DNS服务器
set dns1=18.0.0.1
::设置 备用DNS服务器
set dns2=61.177.7.1
::请根据您的参数修改以上数据
set /p type=请选择设置方式,1: 静态,2: 动态:
if /i %type%==1 goto :STATIC_IP
if /i %type%==2 goto :DHCP_IP
:STATIC_IP
echo 正在进行静态IP设置,请稍等...
netsh interface ip set address name=%name% source=static addr=%ipaddress% mask=%mask%
echo. IP地址 = %ipaddress% .完成设置
echo. 子掩码 = %mask% .完成设置
netsh interface ip set address name=%name% gateway=%gateway% gwmetric=1 >nul
echo. 网关 = %gateway% .完成设置
netsh interface ip set dns name=%name% source=static addr=%dns1% register=PRIMARY >nul
echo. 首选DNS = %dns1% .完成设置
netsh interface ip add dns name=%name% addr=%dns2% index=2 >nul
echo. 备用DNS = %dns2% .完成设置
goto :SETEND
:DHCP_IP
echo 正在进行动态IP设置,请稍等...
netsh interface ip set address name=%name% source=dhcp
netsh interface ip set dns name=%name% source=dhcp register=PRIMARY
netsh interface ip set wins name=%name% source=dhcp
goto :SETEND
:SETEND
echo. ===============IP设置完成================
echo. =========================================
echo. == 以下测试网络是否正常
echo. == 若出现如下形式:
echo. == "Reply from %gateway%: bytes=32 time<1ms TTL=64"
echo. == 则网络正常,否则网络不正常!
echo. =========================================
ping %gateway% -n 1
pause
@sqybi
Copy link

sqybi commented Feb 23, 2017

This doesn't work on Win10. We need to set gateway together with addr and mask:

netsh interface ip set address name=%name% source=static addr=%ipaddress% mask=%mask%
gateway=%gateway% gwmetric=1 >nul

@yhhwpp
Copy link

yhhwpp commented Jun 28, 2017

win10 提示文件夹名、目录名或卷标语法不正确

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment