Page 1 of 2 12>
Topic Options
#187670 - 2008-05-15 05:24 PM Assign printers based on IP address of client?
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Good morning, I'm sorry if I'm doing this wrong but i've been searching the forums for a while and haven't found a solution for this and i'm not sure the best way to do it.
First I'm trying to assign printers to some of my remote sites which connect over a VPN the network is 192.168.X.X I'd like to script mapping printers if you ip address is 192.168.10.X at the .10 you'd get printer \\server\printer1
if your ip is 192.168.16.X you'd get \\server\printer3. I searched the UDFs and didn't see anything that i could figure out. I'm a newb/getting better. but this is above my head. Any suggestions would be appreciated.

Long time reader/first time poster \:\)

Top
#187672 - 2008-05-15 05:49 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Hi PenguinMasta, and Welcome to the board.

Here is a script that I used for a local bank with multiple sites, you will need to modify it for your specific needs but all is included in the zip file to get you started.

 Code:
;region Setup Variables
$WTS = CreateObject("WTSManager.Shell")

If Not $WTS
  $nul= MessageBox("WTSManager.Shell Not Initiated. This Script Will Now Close.","Error",16)
  Exit
EndIf

$WTSMyUserName = $WTS.MyUserName
$WTSMyIPAddress = $WTS.MyIPAddress

;? $WTSMyIPAddress

$Octets = Split($WTSMyIPAddress,'.')

$ThirdOctet = $Octets[2]
$FourthOctet = $Octets[3]

;1. Place the Wkix32.exe file in the 'C:\Windows\System32' directory of each Citrix server.
;   (use Wkix32.exe to execute this script instead of kix32.exe 
;    and you will Not have a DOS Box show up every time it runs.)

;2. Place the WTSManager.dll file in the 'C:\Windows\System32' directory of each Citrix server.

;3. Place the register.exe file in the 'C:\' directory of each Citrix server.

;4. Logon to each Citrix server as Administrator and from the command prompt execute register.exe
;   (You should see this message)
;
;   (c) 2000 Almaz Sharipov. http://vlab.hypermart.net
;   WTSManager.Shell class is registered succesfully!

;5. PLace this script in the appropriate place to be executed apon user logon And you are done.
;endregion

;Milford:		10.71.155.1 	  –   10.71.155.63
;NONE		10.71.155.64 	  –   10.71.155.95
;Beaver Crossing:	10.71.155.96     –   10.71.155.127
;Weeping Water:	10.71.155.128   –   10.71.155.191
;NONE		10.71.155.192   –   10.71.155.223
;Panama:		10.71.155.224   –   10.71.155.254
; 
;Firth:			10.71.156.1       –   10.71.156.31
;Palmyra:		10.71.156.32 	  –   10.71.156.63
;Wilber:		10.71.156.64     –   10.71.156.127
;Superior:		10.71.156.128   –   10.71.156.191
;Jansen:		10.71.156.192   –   10.71.156.223
;Lawrence:		10.71.156.224   –   10.71.156.254

Select
	Case $ThirdOctet = 155 And ($FourthOctet >= 1 And $FourthOctet <= 63)		;Milford:  10.71.155.1	  –  10.71.155.63
		$Location = "Milford"
	Case $ThirdOctet = 155 And ($FourthOctet >= 64 And $FourthOctet <= 95)		;NONE:   10.71.155.64  –  10.71.155.95
		$Location = "None"
	Case $ThirdOctet = 155 And ($FourthOctet >= 96 And $FourthOctet <= 127)		;Beaver Crossing:   10.71.155.96  –  10.71.155.127
		$Location = "Beaver Crossing"
	Case $ThirdOctet = 155 And ($FourthOctet >= 128 And $FourthOctet <= 191)	;Weeping Water:  10.71.155.128  –  10.71.155.191
		$Location = "Weeping Water"
	Case $ThirdOctet = 155 And ($FourthOctet >= 192 And $FourthOctet <= 223)	;NONE:  10.71.155.192  –  10.71.155.223
		$Location = "None"
	Case $ThirdOctet = 155 And ($FourthOctet >= 224 And $FourthOctet <= 254)	;Panama:  10.71.155.224  –  10.71.155.254
		$Location = "Panama"
	Case $ThirdOctet = 156 And ($FourthOctet >= 1 And $FourthOctet <= 31)		;Firth:  10.71.156.1  –  10.71.156.31
		$Location = "Firth"
	Case $ThirdOctet = 156 And ($FourthOctet >= 32 And $FourthOctet <= 63)		;Palmyra:  10.71.156.32  –  10.71.156.63
		$Location = "Palmyra"
	Case $ThirdOctet = 156 And ($FourthOctet >= 64 And $FourthOctet <= 127)		;Wilber:  10.71.156.64  –  10.71.156.127
		$Location = "Wilber"
	Case $ThirdOctet = 156 And ($FourthOctet >= 128 And $FourthOctet <= 191)	;Superior:  10.71.156.128  –  10.71.156.191
		$Location = "Superior"
	Case $ThirdOctet = 156 And ($FourthOctet >= 192 And $FourthOctet <= 223)	;Jansen:  10.71.156.192  –  10.71.156.223
		$Location = "Jansen"
	Case $ThirdOctet = 156 And ($FourthOctet >= 224 And $FourthOctet <= 254)	;Lawrence:  10.71.156.224  –  10.71.156.254
		$Location = "Lawrence"
EndSelect

;? $Location

Select
	Case $Location = "Milford"
;		Set "ZLOCALDIR=P:\ITI\PTW\LOCAL\"+$WTSMyUserName
;		Set "ZSHAREDIR=P:\ITI\PTW\milford"  ;(This will change with the locations)
	Case $Location = "Beaver Crossing"
		;(Modify this for "Beaver Crossing")
	Case $Location = "Weeping Water"
		;(Modify this for "Weeping Water")
	Case $Location = "Panama"
		;(Modify this for "Panama")
	Case $Location = "Firth"
		;(Modify this for "Firth")
	Case $Location = "Palmyra"
		;(Modify this for "Palmyra")
	Case $Location = "Wilber"
		;(Modify this for "Wilber")
	Case $Location = "Superior"
		;(Modify this for "Superior")
	Case $Location = "Jansen"
		;(Modify this for "Jansen")
	Case $Location = "Lawrence"
		;(Modify this for "Lawrence")
EndSelect



Attachments
Bank Logon Script.zip (815 downloads)
Description:


_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#187673 - 2008-05-15 07:25 PM Re: Assign printers based on IP address of client? [Re: Benny69]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Here's a simple solution.. it uses two arrays (which can be loaded from config files in production).

For testing - run once as is, then once after placing your network in the $Networks array, just to see the difference in processing.

There are lots of ways to define the $Networks and $Printers arrays - INI files via ReadProfileString, reading a file, etc. I'll leave that up to you.

Glenn

 Code:
Break On

$Networks = '10.1.0.0/24','10.18.22.0/24','10.64.61.0/24'
$Printers = '\\server\printer1','\\server\printer2','\\server\printer3'
$Mapped = 0

$MyIP = Join(Split(@IPADDRESS0, ' '), '')

For $I = 0 to UBound($Networks)
  If InSubnet($MyIP, $Networks[$I])
    'My ip address (' $MyIP ') exists in network $I (' $Networks[$I] ')' ?
    'Mapping printer ' $Printers[$I] ?
    $Mapped = 1
  EndIf
Next

If Not $Mapped
  'No printers were mapped!' ?
EndIf

; InSubnet UDF available from http://wws.innotechcg.com w/ full header info
Function InSubnet($_IPAddr, $_Network)

  Dim $_aAdd				; array of address values
  Dim $_Mask				; Mask value
  Dim $_Hosts				; hosts in subnet

  ; Convert the supplied IP address to a double value representing the decimal address
  $_aAdd = Split($_IPAddr, '.')
  $_IPAddr = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)

  ; Convert the network from a w.x.y.z/mask format to a decimal value
  ; this is the starting network address value
  $_Network = Split($_Network, '/')
  $_Mask = Val($_Network[1])
  $_aAdd = Split($_Network[0], '.')
  $_Network = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)

  ; Set the number of hosts in the defined network
  $_Hosts = 1.0
  For $_I = 31 to $_Mask Step -1
    $_Hosts = ($_Hosts * 2.0)
  Next

  ; return the value 
  $InSubnet = 0
  If $_IPAddr >= $_Network And $_IPAddr < ($_Network + $_Hosts)
    $InSubnet = 1
  EndIf

  Exit 0

EndFunction
_________________________
Actually I am a Rocket Scientist! \:D

Top
#187678 - 2008-05-15 08:50 PM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Thanks, I'll take a look at these and see if i can get them to work. I love Kix because you can do so much with it. I don't want to get this topic side tracked but I figured I'll as one more question. Is there anything you can do to optimize the load time for the logon script? I've noticed that it takes a while for my script to run for the remote sites. Locally I feel the time is fine.

Thanks again for the code above and I'll give it a try tonight/tomorrow and let you know how it worked out.

Top
#187680 - 2008-05-15 09:56 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Depends on what your scripts are doing.

I had my login script maintain the login configuration in an INI file. The script took nearly 90 seconds to run on a 256K frame-relay link. By caching the file on the local disk, the login script time to run dropped to 12 seconds with a cached file and 16 seconds if the file was missing or outdated.

To cache it locally, I simply A) check for a local copy - download if not present. B) if present, compare the file times and download if the local copy is older. The login script always references the local copy - a HUGE performance gain. 12-16 seconds to map an average of 7 of 20 shared drives, display 2 messages, and run 3 external programs over a 256K link is not bad, considering everything loads from the netlogon share.

The other think I did to improve performance is to determine up front what shares I'd be mapping - load all share parameters into an array, compare security (group and OU membership) and remove those definitions that failed. What I had left, I simply mapped with no further checks.

My script, with documentation and sample config file, is available from my web site if you want to take a look. I've used it at well over 100 client sites without script changes anywhere - all done via the config file.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#187682 - 2008-05-15 10:49 PM Re: Assign printers based on IP address of client? [Re: Benny69]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Benny69 - Do i have to have the file WTSManager.dll on all of the computers? I keep getting "WTSManager.Shell Not Initiated. This Script Will Now Close."
Thanks,
Billy.

P.S. is there anywhere(I've looked but not found) that shows simple real life examples of how some of the commands work. I'm not a programmer and just getting started with VB for some simple admin stuff but I'm getting into stuff that would be nice to actually understand how looping works or like in Benny's script using select case was kinda neat seeing a basic example of how it worked was nice.
Thanks again for all of the help.

Top
#187684 - 2008-05-15 11:05 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
the script that i showed was written for a Citrix environment but if you were running the logon script on PCs then yes you would need to register the dll on any box that ran the script.

as far as examples, that is why this board is here, all you need to do is ask. in addition here is a Link that will give you info on each command.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#187712 - 2008-05-16 09:41 PM Re: Assign printers based on IP address of client? [Re: Benny69]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Now for some newberific questions. Benny's code is the simplist but i don't think i can do it because of having to register that dll on every computer. I know i can push it out with kix but I'm not sure how to register it with people who aren't admins of their computers. So... If someone could show me how to get an ip address look at the third octet (192.168.(15).X can't remember if the 15 is the 3rd or if the 168 is the 3rd, I need the 15) and run logic based on that, I'd buy you a beer if you lived near me :). With Glenn's script hehe i got lost. that might help you to see what LVL newb you're dealing with.
thanks again.

Top
#187713 - 2008-05-16 11:36 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Huh??? Did you actually try the script? Copy my script and paste it into "test.kix" - then run it. DO NOT make any changes.. It will tell you that no printers were mapped.

Next, change one of the entries on the third line to represent YOUR local subnet. IE: if your IP is 192.168.15.17, and your mask is 255.255.255.0, then the line
$Networks = '10.1.0.0/24','10.18.22.0/24','10.64.61.0/24'
becomes
$Networks = '10.1.0.0/24','192.168.15.0/24','10.64.61.0/24'
(I picked the second network arbitrarily)

Run the script again - it should say \\server\printer2 was mapped. That's because the function that it called determined that 192.168.15.17 is in the 192.168.15.0 network - it matched the first 3 octets.

Basically, $Networks is a list of each network addresses and mask in CIDR format. $Printers is a list of printer shares. You need to create the list of networks that match your environment, and create the Printers list in the same sequence.

The way my script works is it determines the IP address of the client where the script is running. It then loops through the list of networks, comparing the local IP with the network/mask values. This is how you determine if an IP address is in a network.

You need to understand basic network/netmask concepts if you expect to make any of this work. The CIDR format represnts the number of bits in the netmask. As I said in my first post, it's a simple solution requiring no logic on your part - just define the array of networks and printer shares, and add the code to actually perform the printer mapping. (basically - you need to replace these lines:
 Code:
    'My ip address (' $MyIP ') exists in network $I (' $Networks[$I] ')' ?
    'Mapping printer ' $Printers[$I] ?

with the commands to map the printer defined by $Printers[$I].

Try it and see if the example works for you. If it does, we can work on making the list of networks/printers easier to manage.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#187732 - 2008-05-19 04:43 PM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Sorry I just got over welmed with your script in my eye's there is alot going on there. I'll give it a try your example helped due to my beginner coder status if I don't understand the code after examining for a while and i get lost I give up on it and look for something else. Nothing against anyone it's just intimidating. for example with your example above

 Code:
'My ip address (' $MyIP ') exists in network $I (' $Networks[$I] ')' ?
    'Mapping printer ' $Printers[$I] ?


I don't understand the "'" what does that do. $myip is variable right? which would be defined before it's used same with $I when you get to (' $networks[$I] ')
I assume this works like a math equation that the [] happens first then () but what are the ' doing in side the ()? also the ? at the end of the example what is it doing? doesn't the ? work like this
 Code:
 ? "echo/display a statement here!" 


I'm having a lot of fun learning this on my own it's just now i'm getting to stuff past my into to vb6.0 (lol) that i took in my college classes.

Top
#187736 - 2008-05-19 09:24 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
In KiXtart ? is the equivalent of carriage return/line feed not a PRINT statement

So the line before the ? is shown on the screen and then the cursor moves down one line.

The quote marks are to seperate the string of text from the variables

Using the first line 'My ip address (' $MyIP ') exists in network $I (' $Networks[$I] ')' ?

Would result on screen like this

My ip address (192.168.1.25) exists in network 3 (192.168.1.0)
_________________________
Today is the tomorrow you worried about yesterday.

Top
#187738 - 2008-05-19 10:45 PM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Glenn, I like the way that script runs I haven't figured out how to pull a list of printers with it because it looks like you'll have to stick to the single $networks and $printers veriables. Would something like this work (i can't get it to so i'm asking hehe)

 Code:
For $I = 0 to UBound($Networks)
  If InSubnet($MyIP, $Networks[$I])
    'My ip address (' $MyIP ') exists in network $I (' $Networks[$I] ')' ?
	select
		case $network = 192.168.15.0/24
		$location = "site A"
		case $network = 192.168.16.0/24
		$location = "site B"
	endselect
    'Mapping printer ' ?
	select
	     case $Location = "Site A"
		@ldrive + "\scripts\siteaprnt.kix"
			? "Added Site A Printers"
		endif
		;Or any style of adding a printer that you recommend
		case @location = "Site B"
		IF ADDPRINTERCONNECTION ("\\server\printer2") = 0 
			? "Added Printer2"
		endif
	$Mapped = 1
  EndIf
Next

If Not $Mapped
  'No printers were mapped!' ?
EndIf


I'll go with any way you suggest to do the printers I'm just using what little i know.

thanks again.

Top
#187740 - 2008-05-20 03:16 AM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
What you propose above is inflexible, and requires hard coding relative to your environment. This is rarely a good thing. The use of external data (arrays in my earlier example) eliminate those shortcomings. For example..

With two arrays (or a single, two-dimensional array) you create a relationship between a network and a printer to be used in that network. The computer has an IP address, and the InSubnet UDF basically answers the question "Is my IP address in this network?" If the answer is "yes", the printer paired with that network is mapped.

In the example script, there are 2 arrays (Network and Printer) with 3 elements each. The script enumerates (works its way through) each of the elements, asking "is my address in THIS network?" and maps the printer when the answer is yes. This sets the "Mapped" variable (flag) to 1 (true) so that we can later issue a warning that no printers were mapped.

I'm guessing you're having a hard time visualizing the relationship between the Network and Printer arrays.. Create an Excel spreadsheet - place the numbers 0-2 in column A, the network/mask values in column B, and the printer UNC paths in column C. When the script enumerates this list, it's like highlighting Row 1 (element 0), asking "is my ip in the range defined in column B? If so, map the printer defined in column C. Get it?

Here's an alternative - a bit more flexible.. It uses an INI file to define the mappings - each line in the INI file contains a complete reference:
 Code:
SiteName=network/mask,\\server\Printer

The script and a sample INI file (using the same example networks as before) follow:
 Code:
Break On
; Uses an INI file for subnet/printer mapping
; The INI file in this example is called NetPrint.ini, and uses one section
; called MAPPINGS. Each value consists of a comma-delimited pair of data
; representing the network/mask and printer share UNC in the format
; sitename=w.x.y.z/mask,\\server\printer


$Mapped = 0					; value showing that printer was not mapped

$MyIP = Join(Split(@IPADDRESS0, ' '), '')	; Get the local IP address, removing spaces

; Load an array from the INI file
$Mappings = EnumIni('.\netprint.ini', 'MAPPINGS')

For Each $SiteName in $Mappings

  ; create a 2-element array by splitting the data at the comma
  ; element 0 contains the network/mask, and element 1 contains the printer UNC
  $NetPrint = Split(ReadProfileString('.\netprint.ini', 'MAPPINGS', $SiteName), ',')

  ; see if the local IP is inside the network
  If InSubnet($MyIP, $NetPrint[0])

    ; these print statements are for diagnostic/testing
    'My ip address (' $MyIP ') exists in the ' $SiteName ' site (' $NetPrint[0] ')' ?
    'Mapping printer ' $NetPrint[1] ?

    ; make the printer connection
    AddPrinterConnection($NetPrint[1])

    ; show that at least 1 printer mapped successfully
    If Not @ERROR
      $Mapped = 1
    EndIF

  EndIF

Next


If Not $Mapped
  'No printers were mapped!' ?
EndIf





; do not modify anything after this line!!!

;; 
;;====================================================================== 
;; 
;;FUNCTION       InSubnet() 
;; 
;;ACTION         Determines if a specific IP address is in a subnet 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0 / 2008/01/20 
;; 
;;SYNTAX         InSubnet(Address, Network) 
;; 
;;PARAMETERS     Address - REQUIRED, IP address in dotted-decimal (w.x.y.z) 
;; 
;;		 Network - REQUIRED, Network in CIDR format (w.x.y.z/mask) 
;; 
;;REMARKS         
;; 
;;RETURNS        1 if ip address is in the defined subnet, 0 otherwise 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3, Vista, X64 
;; 
;;EXAMPLES       If InSubnet('192.168.12.243', '192.168.8.0/21') 
;;		   'Is in subnet!' ? 
;;		 EndIf 
; 
Function InSubnet($_IPAddr, $_Network)
 
  Dim $_aAdd				; array of address values 
  Dim $_Mask				; Mask value 
  Dim $_Hosts				; hosts in subnet 
 
  ; Convert the supplied IP address to a double value representing the decimal address 
  $_aAdd = Split($_IPAddr, '.')
  $_IPAddr = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)
 
  ; Convert the network from a w.x.y.z/mask format to a decimal value 
  ; this is the starting network address value 
  $_Network = Split($_Network, '/')
  $_Mask = Val($_Network[1])
  $_aAdd = Split($_Network[0], '.')
  $_Network = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)
 
  ; Set the number of hosts in the defined network 
  $_Hosts = 1.0
  For $_I = 31 to $_Mask Step -1
    $_Hosts = ($_Hosts * 2.0)
  Next
 
  ; return the value  
  $InSubnet = 0
  If $_IPAddr >= $_Network And $_IPAddr < ($_Network + $_Hosts)
    $InSubnet = 1
  EndIf
 
  Exit 0
 
EndFunction
 



;; 
;;====================================================================== 
;; 
;;FUNCTION       EnumIni() 
;; 
;;ACTION         Enumerates sections or keys of an INI file 
;; 
;;AUTHOR         Glenn Barnas  
;; 
;;VERSION        2.0 
;; 
;;DATE CREATED   2003/11/17 
;; 
;;DATE MODIFIED  2004/10/16 
;; 
;;SYNTAX         EnumIni(File [, Section]) 
;; 
;;PARAMETERS     File - 	REQUIRED, path/name of INI file to examine 
;; 
;;               Section -	OPTIONAL, Section name to parse 
;; 
;;REMARKS        Returns an array containing the sections in an INI file, or 
;;               an array of key names in a specified section. Errors are returned 
;;               for non-existant files or INI file reads. If the specified file 
;;               contains no sections, or the specified section contains no keys, 
;;               the UDF exits with error 13 and returns a null array. Thus, a For-Each loop 
;;               will properly perform no iterations if no data is returned. 
;; 
;;               CAUTION - Error 13 is returned for empty files, or nonexistant sections. 
;;                         This is not necessarily a "failure". 
;; 
;;RETURNS        Array of sections or keys in a section 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    Kix 4.2+, NT4, W2K, WXP, W2K3 
;; 
;;EXAMPLES       $Sections = EnumIni('.\config.ini') 
;;               $Keys = EnumIni('.\config.ini', 'Common') 
; 
Function EnumIni($_fSrcFile, OPTIONAL $_fSectName)
 
  Dim $_fSectList
 
  ; die if the file doesn't exist 
  If Exist($_fSrcFile) = 0
    Exit 2
  EndIf
 
  ; Get the list of sections or keys 
  $_fSectList = ReadProfileString($_fSrcFile, $_fSectName, '')
  ; Return if error occurred 
  If @ERROR
    Exit @ERROR
  EndIf
 
  ; If len is >0, return an array of sections 
  ; If len is 0, either no sections or keys exist, or an invalid section name was specified. Return nothing. 
  If Len($_fSectList) > 0
    $EnumIni = Split(Left($_fSectList,len($_fSectList)-1), Chr(10))
    Exit 0
  EndIF
 
  ; return an error here for value not found (no sections or no keys in section) 
  Exit 13
 
EndFunction 


Here's a sample ini file:
 Code:
[MAPPINGS]
sitename1=10.1.0.0/24,\\server\printer1
sitename2=10.18.22.0/24,\\server\printer2
sitename3=10.64.61.0/24,\\server\printer3


Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#187741 - 2008-05-20 03:17 AM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Hey Benny!!

Can you please break the long lines in your post?

Thanks!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#187757 - 2008-05-20 08:13 PM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Thanks Glenn, I get how the array is matching up, my problem is i have multiple printers for each site and this is where i'm not able to visualize how the array works... If (going to the excel example) 0 in column A matches network in column B then map the printer in C. In excel i can add more then one printer in the cell but with kix i'm not sure how tell the array that network b has 3 printers to map and network c has 2. thats why i was looking at the select case example. its' the only way i know how to say if in X network map X printers. This is a good thing for me to learn since i have no idea how to join statments I'm assuming it's going to use a comma or && to say run this command && this one as well.
Top
#187758 - 2008-05-20 08:52 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
ok - that's important info!

There's two things to try - one may not require any changes to the code.

1. Create a network/mask,printer for each printer at a site. You'll need unique site-names in my last example. ie:
Site1_printer1=10.32.61.0/24,\\server1\printer1
Site1_printer2=10.32.61.0/24,\\server1\printer2
Site2=10.32.65.0/24,\\server2\printer

This keeps the relationship of network to printer 1:1, even though you can have the same network defined several times. Also, note that the third entry is simply "site2" since there is only one printer. I'd probably stay consistent in the names, though.. The sitenames must be unique to be properly parsed in the ini file.

2. If you use the first example, with two arrays, your Printers array can be something like
 Code:
$Printers='\\server\printer', '\\server2\printer1;\\server2\printer2', '\\server3\printer'


Note that the second element has two printers separated by a semicolon. Thus, you could check for a ";" in the printers string, split the data on the ";", and process each element. The delimiter charachter is arbitrary - I used the ";" because it mirrors the delimiter in path strings and is easily recognized as such. See the code snippet below for an example.

 Code:
$MyIP = Join(Split(@IPADDRESS0, ' '), '')

For $I = 0 to UBound($Networks)
  If InSubnet($MyIP, $Networks[$I])
    'My ip address (' $MyIP ') exists in network $I (' $Networks[$I] ')' ?

    ; if there's a ";" in the printers data, there's more than one to map
    If InStr($Printers[$I], ';')
      $ManyPrinters = Split($Printers[$I], ';')
      For Each $Printer in $ManyPrinters
        'Mapping printer ' $Printer ?
        $Mapped = 1
        AddPrinterConnection($Printer)
      Next
    Else	; just map the single printer
      'Mapping printer ' $Printers[$I] ?
      $Mapped = 1
      AddPrinterConnection($Printer[$I])
    EndIf

  EndIf
Next


Be aware that I haven't tested these code snippets since I'm on break from a training class and time is limited. They should give you a good shove in the right direction, though.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#187760 - 2008-05-20 09:15 PM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Thanks, Glenn I'll play with it. I can usually figure this stuff out if i can get a push in the right direction. I don't know how much time i have today to address this but I'll give it a go tonight or tomorrow morning.

thanks again.

Top
#187837 - 2008-05-23 08:43 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
PenguinMasta Offline
Fresh Scripter

Registered: 2008-05-15
Posts: 9
Glenn, the above worked i have just one question, how can i drop it down a line instead of going on so i can do something like this (i know the coding is wrong it's just for an example).

 Code:
$Networks = '192.168.18.0/24' ,'192.168.0.0/24','10.64.61.0/24'
$Printers='\\server\Site1 - HP Color Laserjet 3600n', &
'\\server1\Site2toshiba232;\\server\site2ricohafi', & 
'\\server3\printer'
$Mapped = 0

something like that so i can group the printers together the sites i can do as one long string thats not a problem. I just noticed after adding a couple of printer names that it became a long line. let me know what you think.

thanks,
billy

Top
#187838 - 2008-05-23 09:34 PM Re: Assign printers based on IP address of client? [Re: PenguinMasta]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Do just what you did and drop the "&". Be sure to end each line that you want to continue with the comma.
 Code:
$Printers = 'Site1 device',
'site 2 UNC',
 :
'site 37 UNC',
'last site unc'              ; no comma here - terminates the $Printers array

Kix is "free-form" and will freely read it's syntax from one line to another.

Since this is going to get complex quickly, you should move in the direction of the INI file, or some external data structure. Of course, since the pair of arrays is working for you, proceed with that and get it all tested.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#191833 - 2009-01-19 04:50 PM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
lejeje Offline
Just in Town

Registered: 2009-01-19
Posts: 1
Hallo, Here my change version of the script from Glenn, But I can't get it to work in my domain. My domain consist of 3 subnet 1 main office and 2 branch, all the printers are installed on the print server witch is on the main serverpark at the main branch. But none of the pc's are getting the printers installed. Can someone help. maybe I'm overlooking an error.
 Code:
$Networks = '10.50.0.0/22','10.50.20.0/22','10.50.24.0/22'
$Printers = '\\GL050-PRN-001\OLA-Printer001','\\GL050-PRN-001\OLA-Printer002','\\GL050-PRN-001\OLA-Printer003'
$Mapped = 0

$MyIP = Join(Split(@IPADDRESS0, ' '), '')

For $I = 0 to UBound($Networks)
  If InSubnet($MyIP, $Networks[$I])
    'My ip address (' $MyIP ') exists in network $I (' $Networks[$I] ')' ?
    'Mapping printer ' $Printers[$I] ?
    $Mapped = 1
  EndIf
Next

If Not $Mapped
  'No printers were mapped!' ?
EndIf

Function InSubnet($_IPAddr, $_Network)

  Dim $_aAdd				; array of address values
  Dim $_Mask				; Mask value
  Dim $_Hosts				; hosts in subnet

  ; Convert the supplied IP address to a double value representing the decimal address
  $_aAdd = Split($_IPAddr, '.')
  $_IPAddr = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)

  ; Convert the network from a w.x.y.z/mask format to a decimal value
  ; this is the starting network address value
  $_Network = Split($_Network, '/')
  $_Mask = Val($_Network[1])
  $_aAdd = Split($_Network[0], '.')
  $_Network = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)

  ; Set the number of hosts in the defined network
  $_Hosts = 1.0
  For $_I = 31 to $_Mask Step -1
    $_Hosts = ($_Hosts * 2.0)
  Next

  ; return the value 
  $InSubnet = 0
  If $_IPAddr >= $_Network And $_IPAddr < ($_Network + $_Hosts)
    $InSubnet = 1
  EndIf



EndFunction


Edited by NTDOC (2009-01-19 09:00 PM)
Edit Reason: Added CODE TAGS

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 128 anonymous users online.
Newest Members
SERoyalty, mytar, Gabriel, Alex_Evos, Dansen
17869 Registered Users

Generated in 0.075 seconds in which 0.026 seconds were spent on a total of 16 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org