Use vbs to automatically switch IP addresses of different network segments

Source: Internet
Author: User

The development and testing environments are in different CIDR blocks and IP address changes are often required. Manual modification is very troublesome. Later, a small vbs script was found to achieve automatic failover.

Suppose the IP segments are 192.168.30. * And 192.168.41. * respectively .*

Add two new files, turn30.vbs and turn41.vbs, respectively. The content is as follows:

Strcomputer =   " . "
Set Obw.miservice =   GetObject ( " Winmgmts :\\ "   & Strcomputer &   " \ Root \ cimv2 " )
Set Colnetadapters = Ob1_miservice. 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 " & Stripaddress ( 0 )
Else
Wscript. Echo
" The IP address cocould not be changed. "
End   If
Next  
Strcomputer =   " . "
Set Obw.miservice =   GetObject ( " Winmgmts :\\ "   & Strcomputer &   " \ Root \ cimv2 " )
Set Colnetadapters = Ob1_miservice. 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 " & Stripaddress ( 0 )
Else
Wscript. Echo
" The IP address cocould not be changed. "
End   If
Next

Usage: Double-click turn30.vbs or turn41.vbs.

Reference website:
Http://www.microsoft.com/china/technet/community/scriptcenter/network/scrnet01.mspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.