Re: Compare IP addresses

From: Grant (gpsnett_at_hotmail.com)
Date: 04/21/04


Date: Wed, 21 Apr 2004 15:30:58 +1000

Thanks for the advice. I didnt know about the split function - very useful,
Ill be using that again for sure. Also using a variable as a counter and one
of the limits in a loop - not good, wont do that again.

Some questions:
I tried declaring checkLocalIpAddress() as type boolean first time around
but the script didnt like that - that was the reason for setting the
function to a string value. I see in your code that you assigned it a
boolean value even though it wasnt declared as one (I guess I never assigned
it a string value so probably the same reasoning)

The arroctets, startoctet and endoctet werent declared either? I take it I
dont need to declare variables in script?

Thanks again for your help guys.
Cheers,
Grant

"Richard Mueller [MVP]" <rlmueller-NOSPAM@ameritech.NOSPAM.net> wrote in
message news:%2343w6W0JEHA.3040@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> This newsgroup seems a good place for this. A couple of problems with your
> script. First, IPConfig.IPAddress is a collection. You should reference
> items in the collection with the "i" variable. That is
> IPConfig.IPAddress(i)
>
> Next, the Right function fails if the last octet has less than 2 digits,
> although in this case it doesn't matter. Still, you might use the Split
> function to place the octets in an array. For example:
>
> arrOctets = Split(IPConfig.IPAddress(i), ".")
>
> Then, the last octet will be arrOctets(3)
>
> Next, the statement:
>
> For StartOctet = StartOctet To EndOctet
>
> won't work, as it reassigns the value for StartOctet. I would suggest
> something like:
>
> For j = StartOctet To EndOctet
>
> Next, when comparing j to LastIpOctet, the comparison fails because one is
> an integer and the other a string. You could convert LastIpOctet to an
> integer with
>
> LastIpOctet = CInt(arrOctets(3))
>
> Finally, I would assign the function values True or False (boolean values)
> rather the "true" or "false" (string values), but that's style perhaps.
>
> My version that worked for me follows:
>
> If CheckLocalIPAddress() = True Then
> Wscript.Echo "IP address in range"
> Else
> Wscript.Echo "IP address out of range"
> End If
>
> Function checkLocalIpAddress()
>
> StartOctet = 220
> EndOctet = 240
>
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:\\" _
> & strComputer & "\root\cimv2")
> Set IPConfigSet = objWMIService.ExecQuery _
> ("Select IPAddress 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)
> arrOctets = Split(IPConfig.IPAddress(i), ".")
> LastIpOctet = CInt(arrOctets(3))
> If (StartOctet <= LastIpOctet) And (LastIpOctet <= EndOctet) Then
> checkLocalIpAddress = True
> Exit Function
> End If
> Next
> End If
> Next
>
> checkLocalIpAddress = False
>
> end function
>
> --
> Richard
> Microsoft MVP Scripting and ADSI
> HilltopLab web site - http://www.rlmueller.net
> --
>
> "Grant" <gpsnett@hotmail.com> wrote in message
> news:OOuTiBzJEHA.1224@TK2MSFTNGP11.phx.gbl...
>> Hi,
>>
>> Im not sure if this is the right place to be posting this...if not Im
>> sure
>> Ill soon find out.
>>
>> How do I compare two numbers in script? I am getting the local IP address
>> and trying to grab the last octect but the script doesnt want to know
> about
>> the right$ function. The ultimate aim of the script is to get the local
>> IP
>> address - compare it to a certain range and if it falls in that certain
>> range then the rest of the script doesnt execute.
>>
>> Here is the part I am having trouble with - Im not sure if this is a good
>> way to compare IP addresses, could anyone give me any advice?
>>
>> ------------------------------------------------------
>> Function checkLocalIpAddress()
>> dim StartOctet
>> dim EndOctet
>> dim LastIpOctet
>>
>> StartOctet = 220
>> EndOctet = 240
>>
>> strComputer = "."
>> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
>> "\root\cimv2")
>> Set IPConfigSet = objWMIService.ExecQuery _
>> ("Select IPAddress 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)
>>
>> LastIpOctet = right(IPConfig.IPAddress, 3) 'Having issues
> with
>> this right finction
>>
>> for StartOctet = StartOctet to EndOctet
>> if LastIpOctet = StartOctet then
>> checkLocalIpAddress = "true"
>> exit function
>> end if
>> next
>>
>> Next
>> End If
>> Next
>>
>> checkLocalIpAddress = "false"
>>
>>
>> end function
>> ------------------------------------------------------
>>
>> Thank you,
>> Grant
>>
>>
>
>



Relevant Pages

  • Re: Compare IP addresses
    ... For StartOctet = StartOctet To EndOctet ... If CheckLocalIPAddress() = True Then ... For Each IPConfig in IPConfigSet ... > How do I compare two numbers in script? ...
    (microsoft.public.scripting.wsh)
  • Re: Compare IP addresses
    ... You cannot type variables when you declare them, ... > but the script didnt like that - that was the reason for setting the ... startoctet and endoctet werent declared either? ... >>> How do I compare two numbers in script? ...
    (microsoft.public.scripting.wsh)
  • Re: setting IP address without using control panel
    ... I tried a VBS script I downloaded from MS. ... Then IPCONFIG reported I had no adapter at all, ... the IPCONFIG still returned nothing. ... >> dhcp, or vice-versa. ...
    (microsoft.public.win32.programmer.networks)
  • Re: WMI and IPAddresses
    ... Try this copy of your script out ... but the startup script comes off the sysvol on the ... >>> For Each ipConfig in activeIPConfigSet ...
    (microsoft.public.windowsxp.wmi)
  • dhcp issue
    ... I was wondering if maybe somebody knew of a registry change or had a script ... handy to force a computer to perform the equivalent of a ipconfig /release ...
    (microsoft.public.win2000.networking)

Loading