vbsで異なるセグメントのIPを自動的に切り替える
2250 ワード
開発とテスト環境は異なるセグメントにあり、IPを変更する必要があり、手作業で修正するのは面倒です.その後、vbsスクリプトが見つかり、自動切り替えが実現されました.
IPセグメントがそれぞれ192.168.30である場合.*192.168.41.*
2つのファイルを別々に追加するTurn 30.vbsとTurn 41.vbs、内容は以下の通りです.
strComputer = "."Set objWMIService = GetObject("winmgmts://"& strComputer & "/root/cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array("192.168.30.99") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.30.254") strDNS = Array("192.168.30.254") strGatewayMetric = Array(1) strDNS2 = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS=objNetAdapter.SetDNSServerSearchOrder(strDNS,strDNS2) If errEnable = 0 Then WScript.Echo "The IP address has been changed to "&strIPAddress(0) Else WScript.Echo "The IP address could not be changed."End If Next
strComputer = "."Set objWMIService = GetObject("winmgmts://"& strComputer & "/root/cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array("192.168.41.52") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.41.1") strDNS = Array("192.168.41.254") strGatewayMetric = Array(1) strDNS2 = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS=objNetAdapter.SetDNSServerSearchOrder(strDNS,strDNS2) If errEnable = 0 Then WScript.Echo "The IP address has been changed to "&strIPAddress(0) Else WScript.Echo "The IP address could not be changed."End If Next
使用方法:Turn 30を直接ダブルクリックする.vbsまたはTurn 41.vbsで実行できます.参考サイト:http://www.microsoft.com/china/technet/community/scriptcenter/network/scrnet01.mspx
IPセグメントがそれぞれ192.168.30である場合.*192.168.41.*
2つのファイルを別々に追加するTurn 30.vbsとTurn 41.vbs、内容は以下の通りです.
strComputer = "."Set objWMIService = GetObject("winmgmts://"& strComputer & "/root/cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array("192.168.30.99") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.30.254") strDNS = Array("192.168.30.254") strGatewayMetric = Array(1) strDNS2 = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS=objNetAdapter.SetDNSServerSearchOrder(strDNS,strDNS2) If errEnable = 0 Then WScript.Echo "The IP address has been changed to "&strIPAddress(0) Else WScript.Echo "The IP address could not be changed."End If Next
strComputer = "."Set objWMIService = GetObject("winmgmts://"& strComputer & "/root/cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array("192.168.41.52") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.41.1") strDNS = Array("192.168.41.254") strGatewayMetric = Array(1) strDNS2 = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS=objNetAdapter.SetDNSServerSearchOrder(strDNS,strDNS2) If errEnable = 0 Then WScript.Echo "The IP address has been changed to "&strIPAddress(0) Else WScript.Echo "The IP address could not be changed."End If Next
使用方法:Turn 30を直接ダブルクリックする.vbsまたはTurn 41.vbsで実行できます.参考サイト:http://www.microsoft.com/china/technet/community/scriptcenter/network/scrnet01.mspx
: CSDN , 。
! [email protected]