win10给网卡添加指定dns的bat
时间: 2023-09-08 15:03:58 浏览: 76
在Win10系统中,可以通过编写批处理脚本(.bat)来为网卡添加指定的DNS服务器。
首先,打开记事本或其他文本编辑器,在空白处输入以下代码,并保存为一个带有.bat文件扩展名的文件,例如"add_dns.bat"。
@echo off
setlocal enabledelayedexpansion
set adapter="本地连接" // 将"本地连接" 替换为您要更改DNS的网卡名称
set dns1=8.8.8.8 // 将"8.8.8.8" 替换为您要添加的首选DNS服务器的IP地址
set dns2=8.8.4.4 // 将"8.8.4.4" 替换为您要添加的备用DNS服务器的IP地址
for /F "tokens=1,2 delims=:" %%A in ('netsh interface show interface ^| findstr "%adapter%"') do (
set /a idx=%%A-1
set interface=%%B
)
netsh interface ipv4 set dns name="%interface%" source=static addr=%dns1% index=%idx%
netsh interface ipv4 add dns name="%interface%" addr=%dns2% index=%idx%
上述代码中,我们首先使用 setlocal enabledelayedexpansion 命令启用延迟环境变量扩展,然后使用 set adapter= 命令来设置要更改DNS的网卡名称。接下来,使用 set dns1= 和 set dns2= 命令来设置要添加的首选和备用DNS服务器的IP地址。
在执行循环过程中,我们使用 netsh interface show interface 命令来获取给定网卡名称的索引和接口信息,并使用 for 命令对其进行解析并将结果存储在相应变量中。
最后,我们使用 netsh interface ipv4 set dns 和 netsh interface ipv4 add dns 命令,分别将指定的首选和备用DNS服务器的IP地址添加到指定的网卡上。确保将名称="%interface%" 中的"interface" 替换为相应的接口变量。
请注意,在运行.bat文件之前,请以管理员身份运行命令提示符。运行.bat文件后,网卡的DNS服务器设置将更改为指定的IP地址。
希望以上信息对您有所帮助!