用vbs自動切換不同網段的IP

2021-08-24 22:40:05 字數 1827 閱讀 4134

開發和測試環境處於不同的網段,經常需要更改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") 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

使用方法:直接雙擊turn30.vbs或turn41.vbs即可執行。

參考**:

閘道器和本機IP不同網段的上網辦法

原貼 本機ip位址是 202.netmask是255.255.255.192 閘道器是 61.兩者不在同一網段。windows下貌似很容易解決,直接設好ip閘道器即可,路由表會自動設好,linux卻不行。可以這樣搞定 route add host 61.netmask 0.0.0.0 dev eth...

閘道器和本機IP不同網段的上網辦法

原貼 本機ip位址是 202.netmask是255.255.255.192 閘道器是 61.兩者不在同一網段。windows下貌似很容易解決,直接設好ip閘道器即可,路由表會自動設好,linux卻不行。可以這樣搞定 route add host 61.netmask 0.0.0.0 dev eth...

linux配置兩個不同網段的ip

首先需要確定的是,如果要讓linux配置兩個不同網段的ip,那你的伺服器要求是多網絡卡的,或者上游支援多個網段。那如何配置多個ip繫結呢,我們主要通過修改network配置檔案即可,下面簡單說下具體的操作流程,我以centos 7為例演示。1 檢視當前網絡卡資訊 我們通過 ifconfig 命令可以...