lufas
(Just in Town)
2014-10-31 01:58 PM
Printer based on Ip Range

Hei Guys,

i got one question. I wanna set my printers based on Ip Range with Kix.
I use the AD, where my users are set. So now their Printers are set over GPO.
But i wanna set them over Ip Ranges. So swichting the Place, the printer should be switched to the defined one of the range i'am choosing. Is there any command i can use?
Or how to write?

I am grateful for any response.

Lufas

ps. cant find anything with search function.


Mart
(KiX Supporter)
2014-10-31 02:09 PM
Re: Printer based on Ip Range

You could use the @IPADDRESS0 macro, check the contents and do what is needed based on the results. There are also various UDFs available in the UDF section that can check IP info.

Glenn BarnasAdministrator
(KiX Supporter)
2014-10-31 04:34 PM
Re: Printer based on Ip Range

Welcome KORG!

The InSubnet or IsInSubnet UDFs are well suited for this purpose.

If you don't want to roll your own, our Universal Login Script can control resources by AD Site, network subnet, and a dozen other methods. See the user manual here for complete info.

Glenn


lufas
(Just in Town)
2014-11-03 08:30 AM
Re: Printer based on Ip Range

Thanks Mart,

im kind of a newbie in Kix. So can you give me maybe an exmaple code.

Would be so helpful. Otherwise you can link me a topic. I cant find anything, maybe i just use the wrong wording to serach for.


Mart
(KiX Supporter)
2014-11-03 09:50 AM
Re: Printer based on Ip Range

There are more ways than one to get what you need. It all depends on personal preference and your setup.
Below are two examples. The first is a quick and dirty solution that should work and the second is as Glenn suggested using the IsInSubnet() UDF.

Be careful. The code below is untested.

 Code:
Select
	Case Left(@IPADDRESS0, 11) = "192.168.  1"
		$rc = AddPrinterConnection( "\\server\PrinterForIPRange01")
	Case Left(@IPADDRESS0, 11) = "192.168.  2"
		$rc = AddPrinterConnection( "\\server\PrinterForIPRange02")
EndSelect


 Code:
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


lufas
(Just in Town)
2014-11-03 09:58 AM
Re: Printer based on Ip Range

omg thank you so much ;\)
i will check if it is working.


Snickers
(Fresh Scripter)
2017-04-04 04:14 PM
Re: Printer based on Ip Range

Did you get it to work ?

The script i have does map a printer on the TS server for me (admin) but not when a student logs on.


Mart
(KiX Supporter)
2017-04-04 04:40 PM
Re: Printer based on Ip Range

Just to be sure what is going on, can you put

 Code:
?@error
?@serror


directly after trying to add a printer?

Sure there are ways to elevate permissions to get it done but for how long will that work? Until the next update by MS? Slightly tweaking the permissions on the TS might be enough or maybe there is a different cause.