Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CHANGE INTERNET LAN SETTINGS

Status
Not open for further replies.

FISKO

Technical User
Aug 30, 2005
113
GB
Hi all
I wonder if it is possible to have a script on my desktop to switch bettween LAN settings?
ie: switch the "use a proxy server settings off or on dependant on whether I am on the works LAN or at home on my own system?
Any ideas?
 
the 'proxy' server settings are simply registry settings which you can manip using WshShell.REgREad/RegWrite
for determining which LAN you are on then i would opt for ipaddress to determine where you are? if not and you are using AD you could query which AD Site you are at??
based on string comparision you can then switch your IE settings
 
Here are a pair of functions that will return the IP subnet your system is currently on...

Code:
'==========================================================================
' NAME: GetIPSubnet.vbs
'
' AUTHOR: Paul Chapman, Halcyon Dreams
' DATE  : 1-31-2006
'
' COMMENT: Get correct subnet based on subnet mask.  WMI call only works On
'   Windows XP and higher.  Checks for configuration errors such as multiple
'   gateways or multiple IPs bound to one NIC.
'
'==========================================================================

Option Explicit

WScript.Echo GetIP

' This function returns the correct IP subnet of the system based on the
' subnet mask.  Function first filters out any disconnected NICs or NICs
' that don't have IP protocol enabled.  If the NIC doesn't have a Default
' gateway, it is ignored.  If there are no default gateways defined, Then
' the function quits with no value.  If configuration errors are found, 
' like multiple active gateways or multiple IP's on one NIC, then the 
' function quits with no value.
Function GetIP()
	Dim objWMI, objNICInfo, objIPInfo
	Dim strActiveAdapter, strIPInfo, strAddress, strGateway, strMask, strSubnet
	
	' Connect to WMI
	Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
	' Select adapters that are enabled
	Set objNICInfo = objWMI.ExecQuery("Select * from Win32_NetworkAdapter Where NetConnectionStatus = 2")
	strGateway = Empty
	
	For Each strActiveAdapter In objNICInfo
		' Connect to specific adapter by Index number
		Set objIPInfo = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where Index =" & strActiveAdapter.Index)
		
		' If IP is enabled, check for configuration errors, then get IPSubnet
		For Each strIPInfo In objIPInfo
			If strIPInfo.IPEnabled = True Then
				strAddress = Join(strIPInfo.IPAddress, ",")
				If IsNull(strIPInfo.DefaultIPGateway) Then
					' No Gateway Defined on NIC, try next Adapter
					Exit For
				Else
					strGateway = Join(strIPInfo.DefaultIPGateway, ",")
						If InStr(strGateway, ",") Then
							WScript.Echo "Multiple Gateways bound to NIC:" & VbCrLf & strActiveAdapter.Name
							Exit For
						End If
				End If
				strMask = Join(strIPInfo.IPSubnet, ",")
				If strSubnet = "" Then
					strSubnet = GetSubnet(strAddress, strMask)
				Else
					strSubnet = strSubnet & "," & GetSubnet(strAddress, strMask)
				End if
			End If
		Next
	Next
	
	If strGateway = "" Then
		WScript.Echo "No Gateways defined on any active NIC"
	End If
	
	' Check if multiple subnets were identified
	If InStr(strSubnet, ",") Then
		WScript.Echo "Network Misconfiguration! Multiple Default Gateways"
		Exit Function
	Else
		GetIP = strSubnet
	End If
End Function

' Get the subnet by "ANDing" the IP address and subnet mask
Function GetSubnet(Addr, Mask)
	Dim strNet, strNum
	Dim arrIP, arrMask

	' Quit function if multiple IP's are bound to one NIC
	If InStr(Addr, ",") <> 0 Then
		MsgBox("More than one IP is bound to network adapter: " & VbCrLf & strActiveAdapter.NetConnectionID)
		Exit Function
	Else
		arrIP = Split(Addr, ".")
	End If
	
	arrMask = Split(Mask, ".")

	For strNum = 0 To 3
		If strNet = "" Then
			strNet = arrIP(strNum) And arrMask(strNum)
		Else
			strNet = strNet & "." & (arrIP(strNum) And arrMask(strNum))
		End If
	Next
	
	GetSubnet = strNet

End Function

Good Luck!

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top