Re: Compare IP addresses
From: Richard Mueller [MVP] (rlmueller-NOSPAM_at_ameritech.NOSPAM.net)
Date: 04/21/04
- Next message: Al Dunbar [MS-MVP]: "Re: registry change HKCU"
- Previous message: TDM: "Re: VBS and TCP port"
- In reply to: Grant: "Re: Compare IP addresses"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 21 Apr 2004 09:15:32 -0500
Hi,
You cannot type variables when you declare them, like you do in VB.
Everything is "Variant" in VBScript, until runtime when the variables are
used (values are stored). When you assigned 220 to a variable, that forced
it to be Integer. You can use the TypeName function to troubleshoot these
things.
Even though I cannot type variables, I generally use "Option Explicit" and
declare all variables, since it makes troubleshooting much easier.
-- Richard Microsoft MVP Scripting and ADSI HilltopLab web site - http://www.rlmueller.net -- "Grant" <gpsnett@hotmail.com> wrote in message news:up8CVH2JEHA.3920@TK2MSFTNGP10.phx.gbl... > 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 > >> > >> > > > > > >
- Next message: Al Dunbar [MS-MVP]: "Re: registry change HKCU"
- Previous message: TDM: "Re: VBS and TCP port"
- In reply to: Grant: "Re: Compare IP addresses"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|