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

Compare IP Address to range of IP Addresses???

Status
Not open for further replies.

ldmak90

MIS
Sep 24, 2006
8
US
Hello everyone. I am obviously new here and also new to scripting. I am trying to accomplish changing DNS settings according to a computer's IP Address. The DNS server search order will be changed to certain servers depending on the range of the IP Address.

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 second octet of the IP Address (only allows one decimal in the value). So,

IF strIPAddress >= 192.168.10.1 And strIPAddress <= _ 192.168.10.50 THEN
do some stuff here...
Else
do some other stuff here...
End If

isn't working because it gives me the ' expected THEN ' error right at the second decimal point in the IP Address.

What am I missing ?? Any help would be greatly appreciated.

Thanks very much,

LD
 
You may use this function:
Code:
Public Function ip2num(ip)
Dim i, a, N
a = Split(ip, ".")
N = CDbl(0)
For i = 0 To UBound(a)
  N = N * 256 + a(i)
Next i
ip2num = N
End Function

And then:
If ip2num(strIPAddress) >= ip2num("192.168.10.1") _
And ip2num(strIPAddress) <= ip2num("192.168.10.50") Then
' do some stuff here...
Else
' do some other stuff here...
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hope this helps?!!!

You are a genius!! This was exactly what I needed and it's absolutely perfect. I just tested it. Except you had 'Next i' and I had to remove the 'i'. Otherwise, this seems to be working excellent and has helped me GREATLY.

I hope to one day return the favor to the users of this site.

Thanks very very much,

LD
 
Why are you not changing this via DHCP? Are you using static IPs? I would suggest switching to DHCP so you can more easily set such parameters.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
DHCP would be easy to change via script yes... however, we can not yet utilize DHCP across the entire production environment but require all computers to point to correct AD DNS servers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top