'IP address changes on the network.
'A script to switch the net adapters between static and dhcp ip configuration.
'MAR-2008
If WScript.Arguments.Count <> 1 Then
Usage
WScript.Quit(-1)
End If
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE")
strArgument = WScript.Arguments(0)
Select Case strArgument
Case "static"
goStatic
ToggleIEProxy("disable")
Case "dhcp"
goDhcp
ToggleIEProxy("enable")
Case Else
Usage
WScript.Quit(-1)
End Select
WScript.Quit(1)
Sub goStatic()
strIPAddressBroadcom = Array("192.168.1.21")
'strIPAddressBroadcom = Array(WScript.Arguments(0))
strIPAddressWireless = Array("192.168.1.20")
'strIPAddressWireless = Array(WScript.Arguments(1))
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)
strDNSServers = Array("192.168.1.1")
For Each objNetAdapter In colNetAdapters
If objNetAdapter.Index=8 Then
errEnable = objNetAdapter.EnableStatic(strIPAddressBroadcom,strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway,strGatewayMetric)
'This line removes all DNS servers
errDNS = objNetAdapter.SetDNSServerSearchOrder()
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
If errEnable=0 Then
WScript.StdOut.WriteLine "[*] The IP address of the " & objNetAdapter.Description & " with index " & objNetAdapter.Index & " has been changed."
Else
WScript.StdOut.WriteLine "[!] The IP address could not be changed."
End If
End If
If objNetAdapter.Index=9 Then
errEnable = objNetAdapter.EnableStatic(strIPAddressWireless,strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway,strGatewayMetric)
'This line removes all DNS servers
errDNS = objNetAdapter.SetDNSServerSearchOrder()
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
If errEnable=0 Then
WScript.StdOut.WriteLine "[*] The IP address of the " & objNetAdapter.Description & " with index " & objNetAdapter.Index & " has been changed."
Else
WScript.StdOut.WriteLine "[!] The IP address could not be changed."
End If
End If
Next
End Sub
Sub goDhcp()
For Each objNetAdapter In colNetAdapters
If objNetAdapter.Index=8 Then
errEnable = objNetAdapter.EnableDHCP()
If errEnable=0 Then
WScript.StdOut.WriteLine "[*] DHCP on " & objNetAdapter.Description & " with index " & objNetAdapter.Index & " has been enabled."
Else
WScript.StdOut.WriteLine "[!] DHCP could not be enabled."
End If
WScript.StdOut.WriteLine "[+] Setting DNS."
errDNS = objNetAdapter.SetDNSServerSearchOrder()
errDNS = objNetAdapter.SetDynamicDNSRegistration
WScript.StdOut.WriteLine "[+] DNS set."
errRenew = objNetAdapter.RenewDHCPLease
WScript.StdOut.WriteLine "[+] Renew called."
If errRenew=0 Then
WScript.StdOut.WriteLine "[*] DHCP lease renewed."
Else
WScript.StdOut.WriteLine "[!] DHCP lease could not be renwed. Error number: " & Err.number & ". Error description: " & Err.description & "."
End If
End If
If ObjNetAdapter.Index=9 Then
errEnable = objNetAdapter.EnableDHCP()
If errEnable=0 Then
WScript.StdOut.WriteLine "[*] DHCP on " & objNetAdapter.Description & " with index " & objNetAdapter.Index & " has been enabled."
Else
WScript.StdOut.WriteLine "[!] DHCP could not be enabled."
End If
WScript.StdOut.WriteLine "[+] Setting DNS."
errDNS = objNetAdapter.SetDNSServerSearchOrder()
errDNS = objNetAdapter.SetDynamicDNSRegistration
WScript.StdOut.WriteLine "[+] DNS set."
errRenew = objNetAdapter.RenewDHCPLease
WScript.StdOut.WriteLine "[+] Renew called."
If errRenew=0 Then
WScript.StdOut.WriteLine "[*] DHCP lease renewed."
Else
WScript.StdOut.WriteLine "[!] DHCP lease could not be renwed. Error number: " & Err.number & ". Error description: " & Err.description & "."
End If
End If
Next
End Sub
Sub ToggleIEProxy(status)
Const HKEY_CURRENT_USER = &H80000001
Dim dwValue
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"
strValueName = "ProxyEnable"
If (Not StrComp(status,"disable")) Then
dwValue = 0
End If
If (Not StrComp(status,"enable")) Then
dwValue = 1
End If
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
End Sub
Sub Usage()
WScript.StdOut.WriteLine "[+] Usage: " & WScript.FullName & " " & WScript.ScriptName & "
End Sub
'EOS