Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

>> 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 myLongparse = 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.ValEnd Function
Stuart

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

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

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 IfMichael J

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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |