① 我做了个自动换ip的批处理,可是运行完之后怎么就出现:“配置的dns服务器不正确或不存在”呢
其实没问题,原因是命令设置dns时默认是验证dns有效性,由于你是批处理,前面ip设置正确后电脑反应没有那么快不能马上上网,此时设置dns后在见证dns有效性时基本还在未通状态,所以会有这个提示。
解决办法一(推荐)加上validate=no直接不验证dns通不通。
netsh interface ip set dnsservers %NAME% static %DNS1% validate=no
办法二 不显示这句话的回显(掩耳盗铃法,其实还是验证了)
netsh interface ip set dnsservers %NAME% static %DNS1% >nul
办法三
设置ip后延迟几秒再执行设置dns的语句(如果非要判断dns是否有效的话!)
写的这么具体不采纳一下???
② 我有一批服务器更改了网络环境,需要批量修改IP。现在需要写一个批量远程修改IP的批处理脚本。请问如何写
ChangeIP "192.168.1.2", "255.255.255.0", "192.168.1.1", "192.168.1.1", "192.168.2.1"
'Function ChangeIP (IP As String,NM As String,GW As String,MDNS As String,SDNS As String) As String
Function ChangeIP (IP,NM,GW,MDNS,SDNS) '其中IP,NM,GW,MDNS,SDNS为字符串类型
'返回值说明:返回一个设置的中文说明.
Dim strComputer,objWMIService,colNetAdapters,strIPAddress,strSubnetMask
Dim strGateway,strGatewaymetric,strDNS,objNetAdapter,errEnable,errGateways,errDNS
strComputer = "."
Set objWMIService = GetObject( "winmgmts:\\" & strComputer & "\root\cimv2" )
Set colNetAdapters = objWMIService.ExecQuery( "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE ")
strIPAddress = Array(IP) 'ip地址
strSubnetMask = Array(NM) '子网掩码
strGateway = Array(GW) '网关
strDNS = Array(MDNS,SDNS) '主DNS各备DNS
strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS)
If errEnable = 0 And errGateways = 0 And errDNS = 0 Then
ChangeIP = "设置成功 "
Else
If errEnable = 0 Then
ChangeIP = "IP地址和子网掩码设置成功, "
Else
ChangeIP = "IP地址或子网掩码设置失败, "
End If
If errGateways = 0 Then
ChangeIP = ChangeIP & "默认网关设置成功, "
Else
ChangeIP = ChangeIP & "默认网关设置失败, "
End If
If errDNS = 0 Then
ChangeIP = ChangeIP & "DNS设置成功 "
Else
ChangeIP = ChangeIP & "DNS设置失败 "
End If
End If
Next
End Function