设置windows网卡IP-DNS脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lile777/article/details/79898199

工作中如果经常需要切换网卡的配置,在窗口中手动设置总是很麻烦,索性花点时间写个bat脚本来自动设置,代码如下:

@echo off
:: 设置窗口底色为绿色
color 2F
title 设置网卡IP-DNS

echo ---------- This file just test on Microsoft Windows 10 家庭中文版 ----------

echo ---------- Script requires administrator permissions ----------

::eth-name 为网卡名称,可用ipconfig /all查询,如 "本地链接"
set eth-name="以太网"

set dhcpOpen=0
if %dhcpOpen%==1 (goto dhcpfunc)

::ip 为你想更改的IP
set ip=192.168.1.70

::gw 为网关地址
set gw=192.168.1.1

::netmasks 为子网掩码
set netmasks=255.255.255.0

rem firstDNS
set firstDNS=192.168.1.1
rem secondDNS
set secondDNS=202.103.24.68


echo #正在将本机IP更改到: ip=%ip%; firstDNS=%firstDNS%, secondDNS=%secondDNS%

echo #设静态IP...
netsh interface ip set address name=%eth-name% source=static addr=%ip% mask=%netmasks% gateway=%gw% gwmetric=0

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

if %dhcpOpen%==0 (goto end)

:dhcpfunc
echo 自动获取IP-DNS
echo # %eth-name% 的接口 IP  配置
echo #设自动获取ip
netsh interface ip set address name=%eth-name% source=dhcp 
echo #设自动获取dns
netsh interface ip set dnsservers name=%eth-name% source=dhcp register=PRIMARY
echo #设置 WINS 服务器模式和地址 
netsh interface ip set wins name=%eth-name% source=dhcp

:end
echo #显示%eth-name%配置结果...
netsh interface ip show config %eth-name%
echo # 接口 IP 配置结束

pause
exit

猜你喜欢

转载自blog.csdn.net/lile777/article/details/79898199