① 我做了個自動換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