设置 Windows 网卡的 IP 和 DNS

MS-DOS
::Setting_Win_IP_and_DNS.bat
::设置 Windows 网卡的 IP 和 DNS
::By e.c.

@echo off
::设置窗口底色为绿色
color 2F
title Setting Win IP and DNS
echo ---------- Script requires administrator permissions ----------
echo ------------------- 需要以管理员的权限运行 --------------------
echo.

::eth-name 为网卡名称,可用ipconfig /all查询,如 "本地连接"
set eth-name=无线网络连接

::【是否设置为静态IP】
set static_ip=0
::【是否设置为自动获取DNS】
set auto_dns=1

::【设置IP】
set ip=10.15.1.70
::【设置子网掩码】
set netmasks=255.255.255.0
::【设置网关地址】
set gw=10.15.1.254

::【首选DNS】
set firstDNS=8.8.8.8
::【备用DNS】
set secondDNS=114.114.114.114

echo 【%eth-name%】
echo.

if %static_ip%==1 (call:static_ip) else (call:auto_ip)
if %auto_dns%==1 (call:auto_dns) else (call:static_dns)

echo # 更新 %eth-name% 网卡
ipconfig /renew %eth-name% >> nul
echo.

netsh interface ip show config %eth-name%
echo.

echo ---------------------- 接口 IP 配置结束 ----------------------
echo.

pause
exit


:static_ip   
    echo # 设置为静态IP
    echo # IP=%ip%; 
    echo # NetMasks=%netmasks%; 
    echo # Gateway=%gw%;
    netsh interface ip set address name=%eth-name% source=static addr=%ip% mask=%netmasks% gateway=%gw% gwmetric=0
goto:eof

:auto_ip
    echo # 设置为自动获取IP
    netsh interface ip set address name=%eth-name% source=dhcp
goto:eof

:static_dns
    echo # 设置首选DNS:%firstDNS%
    netsh interface ip set dnsservers name=%eth-name% source=static addr=%firstDNS% register=PRIMARY >> nul
    echo # 设置备用DNS:%secondDNS%
    netsh interface ip add dnsservers name=%eth-name% addr=%secondDNS% index=2 >> nul
    
    ::echo #设置 WINS static 服务器模式和地址...
    ::netsh interface ip set wins name=%eth-name% source=static addr=none
goto:eof

:auto_dns
    echo # 设置自动获取DNS
    netsh interface ip set dnsservers name=%eth-name% source=dhcp register=PRIMARY

    ::echo #设置 WINS DHCP 服务器模式和地址
    ::netsh interface ip set wins name=%eth-name% source=dhcp
goto:eof