用vbs自动切换不同网段的IP
开发和测试环境处于不同的网段,经常需要更改IP,手工修改非常麻烦。后来找到了一个vbs小脚本,实现了自动切换。
假如IP段分别为192.168.30.*和192.168.41.*
分别新添两个文件Turn30.vbs和Turn41.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")
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)
strGatewayMetric = Array(1)
strDNS2 = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
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
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")
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
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)strGatewayMetric = 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
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
使用方法:直接双击Turn30.vbs或Turn41.vbs即可执行。
参考网站:
http://www.microsoft.com/china/technet/community/scriptcenter/network/scrnet01.mspx
分类:
JS/VBScript
, Tips
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)