WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Network Related

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Check If IP Address Is in Subnet

 Keywords: ip address subnet local valid range scope

Question:

I need a way to determine what AD Site a given IP is in. Example: AD Site Subnet may be: 10.10.0.0/21

However, that site may have many local subnets (10.10.0.0/24, 10.10.1.0/28, 10.10.2.0/23, etc).

This local subnets won't match up exactly with a AD Site subnet. I need to somehow determine if a given IP is valid within the larger scope.

10.10.3.1 is valid within 10.10.0.0/21.
Does anyone know of any code robust enough to tell me if a given IP is valid within a given scope?

Answer:

At the most basic level, an IP address consists of 32 bits of binary information, and the subnet mask represents the # of bits counting from left to right that are considered to be part of the network portion of the address and not part of the host portion of the address. I would think that as long as you can identify the AD site IP network address & the # of subnet mask bits associated with it, it would be trivial to compare any IP address with it to determine if it is part of the same subnet. All you have to do is preserve the left-hand most # of subnet mask bits from the IP address and compare with the site's IP network address for equality.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; UDF_GetIPNetwork
; Version 1.0
;
; Decription: Uses binary and to compare the IP address and its subnet mask to determine the IP network.
;
; Dependencies:
;
; Global Variables:
;
; Inputs: vstrIPAddress
;         versSubnet
;
; Outputs: IP Network
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#DefineFunction UDF_GetIPNetwork(vstrIPAddress,vstrSubnet)

   ; Turn the IP address and subnet mask into arrays
   varrIP = Arrayize(vstrIPAddress,".")
   varrSubnet = Arrayize(vstrSubnet,".")
   vstrNetwork = ""
   ; use binary anding to determine the network
   For vintCounter = 0 To 3
      vstrNetwork = ItemInsert((varrIP[vintCounter] & varrSubnet[vintCounter]), -1,vstrNetwork,".")
   Next
   Return(vstrNetwork)

#EndFunction

More

There is absolutely nothing wrong with a string based approach but here is an integer based solution. (It has been lightly tested so bugs are offered at no extra charge.)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Convert dotted IP to integer
;;
;; Parameter: strIP - dot delimited IP Address
;;
;; Notes: Needs additional IP address validation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#DefineFunction MakeIntIp(strIp)
   If ItemCount(strIp, '.') != 4 Then Return 0

   nIp = 0
   For i = 1 To 4
      nIp = nIp << 8
      nIp = nIp + ItemExtract(i,strIp,'.')
   Next

   Return nIp
 #EndFunction

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create a net mask by brute force.
;;
;; Parameter: nSigBits - number of most
;;            significant bits in network part
;;            of IP addresses.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#DefineFunction MakeNetMask(nSigBits)
   If nSigBits >= 32 Then Return -1
   If nSigBits <= 0  Then Return 0

   nMask = 0
   For i = 1 To nSigBits
      nMask = nMask<<1
      nMask = nMask + 1
   Next
   nMask = nMask<<(32-nSigBits)

   Return nMask
 #EndFunction


AD_SubnetLength = 27
nMask = MakeNetMask(AD_SubnetLength)

AD_Decimal = "10.10.8.0"
AD_Binary  = MakeIntIp(AD_Decimal)
AD_Network = AD_Binary & nMask

Office_Decimal = "10.10.10.254"
Office_Binary  = MakeIntIp(Office_Decimal)
Office_Network = Office_Binary & nMask

If AD_Network == Office_Network
   Message("Success", "They Match")
Else
   Message("Failure", "They Do NOT Match")
EndIf

Article ID:   W18375
Filename:   Check If IP Address Is in Subnet.txt
File Created: 2011:03:28:08:06:58
Last Updated: 2011:03:28:08:06:58