|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
VBScript IP Address within range
|
Original Message
|
Name: ldmak90
Date: September 23, 2006 at 16:40:35 Pacific
Subject: VBScript IP Address within rangeOS: XP and 2KCPU/Ram: 2GigsModel/Manufacturer: Dell Xps |
Comment: How can I determine if an IP Address is within a certain range of IP Addresses in a VBScript? If I know a range like 192.168.10.1 - 192.168.10.50 Can I grab an IP Address using VBscript, assign it to a variable and then perform some actions if that IP Address falls within that range? Problem I am having is that I get "expected then" because when I assign an IP Address to a variable it doesn't like anything passed the first decimal point (second octet of the IP). So, IF strIPAddress >= 192.168.10.1 And strIPAddress <= 192.168.10.50 THEN do some stuff here... isn't working... gives me the expected THEN error. What am I missing ?? Thanks very much and I hope I explained it right... I'm not much of a scripter. LD
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: StuartS
Date: September 23, 2006 at 16:58:57 Pacific
|
Reply: (edit)>> it doesn't like anything passed the first decimal point << Of course it doesn't. A number can only ever have one decimal point. More than one and there is something wrong. The only way to get a dotted IP address into a variable is as as string. Then you will be doing a string comparison which wont work. The easiest way is to compare IP addresses is to convert the IP dress from a string into a long integer then do the comparision. An IP address is just an unusual representaion of a long integer, that is a 32 byte number split into four groups of 8 bytes. The following code will do that. Public Function IP2Long(IP As String) As Long ReDim parse(0 To 3) As String Dim B As myBytes Dim L As myLong parse = Split(IP, ".") B.B4 = Val(parse(0)) B.B3 = Val(parse(1)) B.B2 = Val(parse(2)) B.B1 = Val(parse(3)) LSet L = B IP2Long = L.Val End Function Stuart
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: StuartS
Date: September 23, 2006 at 17:14:57 Pacific
|
Reply: (edit)Just to add on emore thing, when you convert the IP address into a long integer, some bumbers will apear as negative numbers. That is becasue VB do not have an unsigned integer and number greater than 2,147,483,647 will appear negative. A 32 bit integer has a maximum value of 4,294,967,295 but -2,147,483,648 to 2,147,483,647 when signed. If that makes no sence to you dont worry about it. It doesn't matter in this instance as you are only doing a comparison. It would only be a problem if you were doing mathematical calculations on the number. Stuart
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: ldmak90
Date: September 23, 2006 at 18:07:07 Pacific
|
Reply: (edit)I do greatly appreciate the time you spent thinking about this issue of mine... and I do have a better understanding now of why what I'm trying won't work... but I am not familiar with any of the code you posted. I'm guessing it's not actually vbscript but probably VB?? Can you or anyone else offer similar assistance in the form of VBscript? Thanks again, LD
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Michael J (by mjdamato)
Date: September 25, 2006 at 08:52:27 Pacific
|
Reply: (edit)Here is another way to approach this. Just enter your IP addresses as strings and then use the split command to create an array of the four octets. Then you can compare each octet individually: -------- SAMPLE CODE ---------- 'Set the values to test RangeMin = "192.168.10.1" RangeMax = "192.168.10.50" TestIP = "192.168.10.55" 'Split the above values into arrays MinAry = Split(RangeMin, ".", -1, 1) MaxAry = Split(RangeMax, ".", -1, 1) IPAry = Split(TestIP, ".", -1, 1)
'Test the octets error = "" If IPAry(0) < MinAry(0) OR IPAry(0) > MaxAry(0) Then error = "First octect out of range" ElseIF IPAry(1) < MinAry(1) OR IPAry(1) > MaxAry(1) Then error = "Second octect out of range" & IPAry(1) & ":" & MaxAry(1) ElseIF IPAry(2) < MinAry(2) OR IPAry(2) > MaxAry(2) Then error = "Third octect out of range" ElseIF IPAry(3) < MinAry(3) OR IPAry(3) > MaxAry(3) Then error = "Fourth octect out of range" End If
'Display the results If error<>"" Then MsgBox error, 16, "Processing Complete" Else MsgBox "The IP is within range", 1, "Processing Complete" End If Michael J
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: Michael J (by mjdamato)
Date: September 25, 2006 at 08:54:07 Pacific
|
Reply: (edit)Whoops, forget the additional content on that first error message. I just put it in for debugging. But, you get the idea. You could get much more advanced with that code. Michael J
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Programming Forum Home
|
|
|