首页 > 解决方案 > 仅当适配器当前设置为静态分配时才设置 dns 地址的批处理文件

问题描述

第一篇文章,编码新手。如果适配器 dhcp enabled 设置设置为 No,我正在寻找一个批处理文件来设置 dns 服务器。我实际上有这个工作但它有限。目前,如果它看到任何启用了 dhcp 的适配器为 no,那么它将在所有 nics 上设置 dns 服务器。我还需要它来检查是否未启用 dhcp 以及 nic 是否不是环回适配器。如果 dhcp enabled = no 和 ip address <> 127.0.0.1 然后设置 dns 服务器地址。

这是我目前拥有的...

@echo off
SET _DHCP=Maybe
For /f "tokens=2 delims=:" %%a in ('netsh interface ip show addresses ^|FIND "DHCP Enabled"') DO SET _DHCP=%%a
IF "%_DHCP%"=="No" Then

 @echo off
 set dnsserver=192.168.25.200
 set dnsserver2=192.168.25.201
 for /f "tokens=1,2,3*" %%i in ('netsh interface show interface') do (
 if %%i EQU Enabled (
 rem echo change "%%l" : %dnsserver%
 netsh interface ipv4 set dnsserver name="%%l" static %dnsserver% both
 netsh interface ipv4 add dnsserver name="%%l" %dnsserver2% index=2
 )
)

如果我能弄清楚如何在获取 dhcp 状态的同时提取 IP 地址,我认为它会满足我的需求。

谢谢您的帮助。

标签: batch-filestaticdhcp

解决方案


推荐阅读