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

System_Net
plus

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

Get IP Address Range

 Keywords: Calculate IP Address RangeNetmask dotNet .Net Byte Array ObjectClrType System.Byte ObjectType UI ARRAY GetAddressBytes

;***************************************************************************
;**   Get IP Address Range
;**
;** Purpose: Calculate the IP range using the IP address and the netmask
;** Inputs:
;**       Ipaddress
;**       Netmask: A netmask is a 32-bit mask used to divide an IP address into subnets and specify the networks available hosts.
;** Outputs:
;** Revisions:
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**       http://stackoverflow.com/questions/1470792/how-to-calculate-the-ip-range-when-the-ip-address-and-the-netmask-is-given
;** Developer: Deana Falk 2013.04.26
;***************************************************************************
If Version( )< '2013A'
   Pause("Notice", "Need 2013A or Newer Version of WInBatch")
   Exit
EndIf

#DefineFunction udfWILArray2ByteArray( wilarray )
    bytearray = ObjectClrType("System.Byte", ObjectType("array|ui1",wilarray))
    Return bytearray
#EndFunction

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Inputs
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sIp = '192.168.01.1' ; Ipaddress
nBits = 25           ; Netmask

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assembly into the WinBatch process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectClrOption("use","System.Net, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create a Byte Array to pass as a ctorParm1 to the IPAddress Class
aIp = Arrayize(sIp, ".") ; WIL Array
ctorParm1 = udfWILArray2ByteArray( aIp )
netSysNet = ObjectClrNew('System.Net.IPAddress',ctorParm1)

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Associate a Framework based type name with a value
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uint = ObjectClrNew( 'System.Int32' )
mask = ~(uint.MaxValue >> nBits)

; Convert the IP address to bytes.
ipBytes = netSysNet.GetAddressBytes()

; Reverse Order of Bytes
BitConverter = ObjectClrNew('System.BitConverter')
maskBytes = BitConverter.GetBytes(mask)
; Array reverse
nBytes = ArrInfo(maskBytes, 1)
maskReved = ArrDimension(nBytes)
nMax = nBytes-1
For i = 0 To nMax
    maskReved[i] = maskBytes[nMax-i]
Next

; Calculate the bytes of the start and end IP addresses.
startIPBytes = ArrDimension(nBytes)
endIPBytes = ArrDimension(nBytes)
For i = 0 To nMax
   startIPBytes[i] = ipBytes[i] & maskReved[i]
   endIPBytes[i] = ipBytes[i] | (~maskReved[i])
Next

ctorParm1 = udfWILArray2ByteArray( startIPBytes )
ipStart = ObjectClrNew('System.Net.IPAddress',ctorParm1)

ctorParm1 = udfWILArray2ByteArray( endIPBytes )
ipEnd = ObjectClrNew('System.Net.IPAddress',ctorParm1)

Message("Ip Range", "Starting IP: ":ipStart.ToString():@CRLF:"Ending IP: ":ipEnd.ToString())

Exit

Article ID:   W17833
Filename:   Get IP Address Range.txt
File Created: 2013:04:29:09:16:10
Last Updated: 2013:04:29:09:16:10