Select
Case IsInSubnet(@IPADDRESS0, "192.168.1.0", "255.255.255.0", 1) = "1"
$rc = AddPrinterConnection("\\server\PrinterForIPRange01")
Case IsInSubnet(@IPADDRESS0, "192.168.2.0", "255.255.255.0", 1) = "1"
$rc = AddPrinterConnection("\\server\PrinterForIPRange02")
EndSelect
;Do not change anything below this line
;it is a UDF and it comes ready for use
;=============================================
;FUNCTION IsInSubnet
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;ACTION Checks if a given IP address is in a specified subnet
;
;VERSION 2.2 (fixed a problem with CIDRs smaller than 10, added IP address validation)
; 2.1 (fixed a problem if only a single networkID/subnetmask combinations was
; provided as the second parameter)
; 2.0 (completely rewritten code, removed all dependencies, added support for
; array of network IDs and subnet masks, added return options, fully
; backward compatible with version 1.1)
; 1.1
;
;DATE CREATED 2001/12/03
;
;DATE MODIFIED 2003/08/18
;
;KIXTART 4.21+
;
;SYNTAX ISINSUBNET(IPADDRESS,NETWORKID [,SUBNETMASK, RETURN])
;
;PARAMETERS IPADDRESS
; Required string containing the IP number
;
; NETWORKID
; Required string containing a networkid or a string/array of network ID/subnet mask
;
; SUBNETMASK
; Optional string containing a subnet mask (octets or CIDR) if the network ID is a
; string. The subnetmask is not rquired when providing a string/array of
; network ID/subnet mask
;
; RETURN
; Optional return options when providing an array of network IDs/subnet masks
; 1 = IP address is in ANY of the provided networks
; 2 = IP address is in ALL of the provided networks
;
;REMARKS based on code provided in the KiXgolf MOAN() tournament on the KiXtart BBS
;
;DEPENDENCIES none
;
;RETURNS 1 if IP address is part of any/all networks, otherwise 0. Alternatively, if an array
; of networks is provided, it returns an array of 1/0 corresponding to the input array
; of networks.
;
;EXAMPLE $ip='10.10.10.1'
; $networkid='10.10.10.0'
; $subnetmask='255.255.255.0'
; $answer = isinsubnet($ip,$networkid,$subnetmask)
; ? 'Error = '+@ERROR+ ' - '+@SERROR
; ? 'IsInSubnet = '+$answer
; redim $networkid[2]
; $ip='10.10.10.1'
; $networkid[0]='10.10.10.0/24'
; $networkid[1]='10.10.10.0/27'
; $networkid[2]='10.10.0.0/255.255.0.0'
; $answer = isinsubnet($ip,$networkid)
; $answer = isinsubnet($ip,$networkid,,1)
; $answer = isinsubnet($ip,$networkid,,2)
;
;KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=81873
;
Function isinsubnet($ip, $network, optional $subnetmask, optional $bin)
Dim $a, $b, $c, $, $e, $f, $g, $h, $m[32]
; convert IP address to array
If VarType($ip) & 8192
Exit 87
EndIf
; check whether IP addresses are in correct format
If UBound(Split($ip, '.')) <> 3
Exit 87
EndIf
; check for valid IP address range
$a = Split($ip, '.')
For Each $b in $a
If $b <> 1 * $b Or 0 > $b Or 255 < $b
Exit 87
EndIf
Next
; check for network ID format and extract subnet masks if necessary
Select
Case VarType($network) & 8192
$subnetmask = $network
For $a = 0 to UBound($network)
$b = Split($network[$a], '/')
If UBound($b) <> 1
Exit 87
EndIf
If UBound(Split($b[0], '.')) <> 3
Exit 87
EndIf
$network[$a] = $b[0]
$subnetmask[$a] = $b[1]
Next
Case InStr($network, '/')
$network = Split($network, '/')
$subnetmask = Split($network[1], '')
$network = Split($network[0], '')
Case UBound(Split($network, '.')) <> 3
Exit 87
Case 1
$network = Split($network, '')
EndSelect
; convert subnetmask address to array
If Not (VarType($subnetmask) & 8192)
$subnetmask = Split($subnetmask, '')
EndIf
;Begin build mask array (Howard Bullock)
$f = 0, 128, 192, 224, 240, 248, 252, 254, 255
For $e = 0 to 32
Dim $h
$ = $e
For $g = 1 to 4
$h = '' + $h + IIf($ /8, '255', $f[$mod 8]) + IIf($g < 4, '.', '')
$ = IIf($ > 8, $ -8, 0)
Next
$m[$e] = $h
Next
;End build mask array
; convert CIDR addresses into octets
For $a = 0 to UBound($subnetmask)
$b = $subnetmask[$a]
If Not InStr($b, '.')
If 0 <= $b And 32 >= $b
$subnetmask[$a] = $m[$subnetmask[$a]]
Else
Exit 87
EndIf
EndIf
Next
; check whether IP & SubnetMask = NetworkID
$ = UBound($network)
ReDim $isinsubnet[$]
$b = Split($ip, '.')
For $c = 0 to $
$e = Split($network[$c], '.')
$f = Split($subnetmask[$c], '.')
$g = 0
For $h = 0 to 3
$g = $g + ((Val($b[$h]) & Val($f[$h])) = Val($e[$h]))
Next
$isinsubnet[$c] = ($g = 4) + $isinsubnet[$c]
Next
; convert array back to string value if only one networkid/subnetmask address provided
Select
Case $ = 0
$isinsubnet = $isinsubnet[0]
Case Val($bin) = 1
$isinsubnet = (AScan($isinsubnet, 1) > -1)
Case Val($bin) = 2
$isinsubnet = (InStr(Join($isinsubnet, ''), '0') = 0)
EndSelect
EndFunction