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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to modify the ThirdOctet of the Ipadress in the registry ?

Status
Not open for further replies.

dickyboy37

Technical User
Joined
Jul 10, 2006
Messages
7
Location
FR
Hy,

New on the forum (from France), I seek to modify the thirdOctet byte of @ the IP in the base of register for a whole park information in XP.

If 16 become 4
If 17 becomes 5
If 18 becomes 6

Anybody can help me ?

Thank you in advance,
 
str3rdOctet = CStr("get this from the registry???")

Select Case str3rdOctet

Case "16"
str3rdOctect = "4"
Case "17"
str3rdOctect = "5"
Case "18"
str3rdOctect = "6"
End Select

'''do somethign with str3rdOctect.

not sure what you mean by changing the 3rd octet in the registry but there you go
 
I have a whole park of XP with IP addresses into 192.168.16. * 192.168.17.* 192.168.18.* that I must change into 192.168.4.* 192.168.5.* 192.168.6.*

It is our national center network which requires of us to standardize our IP addresses

Thanks for your script and Ok for the select case, but how to recover the third octet ?
 
Say you've got the IP address stored in a variable called strIPAddress (and the octets are divided by fullstops). You can split it into four parts using the SPLIT command.

Code:
arrSplitIPAddress = split(strIPAddress, ".")

Msg = "Octet 1 = " & arrSplitIPAddress(0)
MsgBox Msg

Msg = "Octet 2 = " & arrSplitIPAddress(1)
MsgBox Msg

Msg = "Octet 3 = " & arrSplitIPAddress(2)
MsgBox Msg

Msg = "Octet 4 = " & arrSplitIPAddress(3)
MsgBox Msg

'All together :
Msg = "IP Address = " & arrSplitIPAddress(0) & "." & arrSplitIPAddress(1) & "." & arrSplitIPAddress(2) & "." & arrSplitIPAddress(3) 
MsgBox Msg

JJ
 
Ok thank You for vour code.
Here my code. I manage well to post the 3 Byte, on the other hand the modification does not pass...
An idea? Thank you,

Code:
'On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then 
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
WScript.Echo IPConfig.IPAddress(i)

arrSplitIPAddress = split(IPConfig.IPAddress(i), ".")
   
Msg = "Octet 1 = " & arrSplitIPAddress(0)
MsgBox Msg

Msg = "Octet 2 = " & arrSplitIPAddress(1)
MsgBox Msg

Msg = "Octet 3 = " & arrSplitIPAddress(2)
MsgBox Msg

str3rdOctet = arrSplitIPAddress(2)
Select Case str3rdOctet
  Case "6"
     str3rdOctect = "1"
  Case "17"
     str3rdOctect = "5"
  Case "18"
     str3rdOctect = "6"
End Select
WScript.Echo str3rdOctet

Msg = "Octet 4 = " & arrSplitIPAddress(3)
MsgBox Msg

'Affiche Tous les Octets de l'@
Msg = "IP Address = " & arrSplitIPAddress(0) & "." & arrSplitIPAddress(1) & "." & arrSplitIPAddress(2) & "." & arrSplitIPAddress(3) 
MsgBox Msg
Next
End If
Next
 
Well, you can achieve the result you want without adding in the str3rdOctet variable but the main problem seemed to be that you put each CASE value in quote marks.

Here's my version without the extra variable and without the quote marks:
Code:
'On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
WScript.Echo IPConfig.IPAddress(i)

arrSplitIPAddress = split(IPConfig.IPAddress(i), ".")

Msg = "Octet 1 = " & arrSplitIPAddress(0)
MsgBox Msg

Msg = "Octet 2 = " & arrSplitIPAddress(1)
MsgBox Msg

Msg = "Octet 3 = " & arrSplitIPAddress(2)
MsgBox Msg

WScript.Echo "Before SELECT CASE The arrSplitIPAddress(2) = " & arrSplitIPAddress(2)
Select Case arrSplitIPAddress(2)
  Case 6
     arrSplitIPAddress(2) = "1"
  Case 17
     arrSplitIPAddress(2) = "5"
  Case 18
     arrSplitIPAddress(2) = "6"
End Select
WScript.Echo "After SELECT CASE The arrSplitIPAddress(2) = " & arrSplitIPAddress(2)

Msg = "Octet 4 = " & arrSplitIPAddress(3)
MsgBox Msg

'Affiche Tous les Octets de l'@
Msg = "New IP Address = " & arrSplitIPAddress(0) & "." & arrSplitIPAddress(1) & "." & arrSplitIPAddress(2) & "." & arrSplitIPAddress(3)
MsgBox Msg
Next
End If
Next

JJ
 
Ok thank your help JJ, I worked above this after midday, and I found !

I recover the 3 byte, I modify it and I insert it in the new address IP which I writes in the register. I benefitted from it to record the DNS, etc...

thank you still!


Code:
'On Error Resume Next

    ''*** Définit la machine sur laquelle on se connecte "." veut dire cette machine
    strComputer = "."

    ''*** Connexion WMI 
    	Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    ''*** R2cupère tous les paramètres de toutes les infaces réseauw ou l'Ip est Activée
	Set IPConfigSet = objWMIService.ExecQuery _
	("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

    ''*** Boucle pour chaque Interface
	For Each IPConfig in IPConfigSet
	If Not IsNull(IPConfig.IPAddress) Then 
	For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)

    ''*** On affiche l'@ IP				
	WScript.Echo IPConfig.IPAddress(i)
		
    ''*** On découpe chaque Octet de l'@ Ip et affectation dans une variable
	arrSplitIPAddress = split(IPConfig.IPAddress(i), ".")
   
	Msg = "Octet 1 = " & arrSplitIPAddress(0)
	strFirstOctet = arrSplitIPAddress(0)
	MsgBox Msg

	Msg = "Octet 2 = " & arrSplitIPAddress(1)
	strSecondOctet = arrSplitIPAddress(1)
	MsgBox Msg

	Msg = "Octet 3 = " & arrSplitIPAddress(2)
	strThirdOctet = arrSplitIPAddress(2)
	MsgBox Msg

   ''*** On rempace le 3ème Octet
	Select Case strThirdOctet
  	Case 1
     	strThirdOctet = 6
  	Case 16
     	strThirdOctet = 4
  	Case 17
     	strThirdOctet = 5
     	Case 18
     	strThirdOctet = 6
     	Case 19
     	strThirdOctet = 7
	End Select

	Msg = "Octet 3 modifié= " & strThirdOctet
	MsgBox Msg

	Msg = "Octet 4 = " & arrSplitIPAddress(3)
	MsgBox Msg
	strFourthOctet = arrSplitIPAddress(3)

   ''***Affiche Tous les Octets de l'@
	Msg = "4 Octets Modifiés de l'IP Address = " & strFirstOctet & "." & strSecondOctet & "." & strThirdOctet & "." & strFourthOctet
	MsgBox Msg

   ''***Définit la nouvelle @IP modifiée
	arrIPAddress = Array(strFirstOctet & "." & strSecondOctet & "." & strThirdOctet & "." & strFourthOctet)
	
   ''***Définit les paramètres réseaux supplémentaires
	arrSubnetMask = Array("255.255.248.0")
	arrDNSServers = Array("55.8.1.15", "55.225.55.4")
	strDNSDomain = "D15370100.cicoa.cnamts.fr"
	arrGateway = Array("55.7.4.1")

   ''***Ecriture de l'Ip  et du Mask
        intErrorEnableStatic = IPConfig.EnableStatic(arrIPAddress, arrSubnetMask)
        	
   ''*** Ecriture de la Gateay
        intErrorGateways = IPConfig.SetGateways(arrGateway)
        	
   ''*** Ecriture Orde de recherche DNS
        intErrorSetDNSServerSearchOrder = IPConfig.SetDNSServerSearchOrder(arrDNSServers)

   ''*** Ecriture suffixes DNS  
        intErrorSetDNSDomain = IPConfig.SetDNSDomain(strDNSDomain)        

Next
End If
Next
 
your str3rdOctect is built from the Split command, this is an implicit conversion to a string, therefore your Select Case statement should be in "", if you leave then 6 without the quotes you are asking vbscript to implicitly convert them,.,,which i dont think is best practice
 
I think you are going to find that not all NICs support the WMI Win32_NetworkAdapterConfiguration IPAddress.

I recently posted code to grab the IP from the registry:

thread329-1252028

It is a fairly easy matter to grab that value and rebuild the IP using a split command and replacing the 3rd Octet.
From there you will want to write the value back to the registry.

This was an interesting problem so I wrote the code for it:
Code:
'==========================================================================
'
' NAME: SubnetChanger.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYRIGHT (c) 2006 All Rights Reserved
' DATE  : 7/12/2006
'
' COMMENT: Changes the third octet in a static IP address.
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================
strComputer = "."
Const HKEY_LOCAL_MACHINE = &H80000002
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
".\root\default:StdRegProv")

Set WSHShell = wscript.CreateObject("Wscript.Shell")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

    
For Each subkey In arrSubKeys
    IPAddr = WSHShell.RegRead("HKLM\" & strKeyPath & subkey & "\IPAddress")
    For Each octetSTR In IPAddr
        If octetSTR <> "0.0.0.0" Then
            StaticIP = octetSTR
            ActiveInterface = subkey
        End If
    Next
Next


IPArray = Split(StaticIP,".")

Select Case IPArray(2)
	Case 1
         strThirdOctet = 6
    Case 16
         strThirdOctet = 4
    Case 17
         strThirdOctet = 5
    Case 18
         strThirdOctet = 6
    Case 19
         strThirdOctet = 7
End Select


If IPArray(2) = "0" Then 
 NewIP = IPArray(0) & "." & IPArray(1) & "."& strThirdOctet & "." & IPArray(3)
End If

iValues = Array(NewIP)
IPRegLocation = strKeyPath & ActiveInterface
oReg.SetMultiStringValue HKEY_LOCAL_MACHINE,IPRegLocation,_
    "IPAddress",iValues


Note the following:
1. If the NIC has more than 1 IP configured it will get wiped out by this script.
2. I recommend you simply switch to DHCP and stop messing around with static IPs.




I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Hy,

Thank you for your contribution but script does not go: - (
I have this message :

Ligne69
Types non correspondants
code : 80041005
Source: SWbemObjectX

On the other hand, my script goes, but on a machine with 2 @ IP, it replaces the first @ by the second.

Regards,
Gil
 
I am interested in what you have copied and pasted when you say it does not go. I have tested and verified my script and suspect a copy paste problem with fonts or word wrap.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
This is the code pasted.
Regards

Code:
'==========================================================================
'
' NAME: SubnetChanger.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYRIGHT (c) 2006 All Rights Reserved
' DATE  : 7/12/2006
'
' COMMENT: Changes the third octet in a static IP address.
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================
strComputer = "."
Const HKEY_LOCAL_MACHINE = &H80000002
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
".\root\default:StdRegProv")

Set WSHShell = wscript.CreateObject("Wscript.Shell")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

    
For Each subkey In arrSubKeys
    IPAddr = WSHShell.RegRead("HKLM\" & strKeyPath & subkey & "\IPAddress")
    For Each octetSTR In IPAddr
        If octetSTR <> "0.0.0.0" Then
            StaticIP = octetSTR
            ActiveInterface = subkey
        End If
    Next
Next


IPArray = Split(StaticIP,".")

Select Case IPArray(2)
    Case 1
         strThirdOctet = 6
    Case 16
         strThirdOctet = 4
    Case 17
         strThirdOctet = 5
    Case 18
         strThirdOctet = 6
    Case 19
         strThirdOctet = 7
End Select


If IPArray(2) = "0" Then 
 NewIP = IPArray(0) & "." & IPArray(1) & "."& strThirdOctet & "." & IPArray(3)
End If

iValues = Array(NewIP)
IPRegLocation = strKeyPath & ActiveInterface
oReg.SetMultiStringValue HKEY_LOCAL_MACHINE,IPRegLocation,_
    "IPAddress",iValues
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top