#!/usr/bin/perl # # printerSetup.pl # Runs via the login hook for a user login to MacOS X # # Author: Michael Thole # Version: 0.3 (5/20/03) # # Tasks: # - Determine machine's ip address # - Use it to figure out which printers we should add # - We also add several universal printers # - Cancel existing print jobs # - Delete exisitng printers # - Add the printers we decided apon # - Set the default printer # - default printer is the 1st printer added to the $printers string # # Version: 0.4 5/26/2004 - Tom Johnson (palantir) # - gave full path for lpadmin so that other users with different environment variables could use the script # # Version 0.4 10/11/2004 - Tom Johnson (palantir) # - the name of the script is now printerSetup.replaceme - the web server it # is on does not like it when the name ends in .pl You will need to # rename the file so it ends in .pl before it will work properly. # $printerMappingsPath="/Users/nfsadmin/Documents/printerMappings.txt"; $printerDefinitionsPath="/Users/nfsadmin/Documents/printerDefinitions.txt"; %printersToAdd = (); %addPrinterCommands = (); $defaultPrinter; ### Subroutine declarations #### sub addressInRange ($$) { ($address,$range)=@_; @ipBlocks=split(/\./,$address); @rangeBlocks=split(/\./,$range); foreach $n (3,2,1,0) { if(split(/\-/,$rangeBlocks[$n]) == 2) { ($lowBound,$highBound)=split(/\-/,$rangeBlocks[$n]); if($ipBlocks[$n] < $lowBound || $ipBlocks[$n] > $highBound) { return 0; } } else { if($rangeBlocks[$n] != "*" && $ipBlocks[$n] != $rangeBlocks[$n]) { return 0; } } } return 1; } sub trim($) { $_=($_[0]); s/^ |\t|\n//; return $_; } sub addPrinters(@) { foreach $printer (@_) { $p=trim($printer); $printersToAdd{($p)}=true; if(!$defaultPrinter) { $defaultPrinter = $p; } } } ### Procedural code begins #### # grab our ip address @addy=split(/ /,`/sbin/ifconfig | /usr/bin/grep broadcast`); $ip_address=$addy[1]; if(split(/\./,$ip_address) != 4) { die("Couldn't determine machine's ip address!"); } # read in the raw data from the printer mappings and definitions files open(DATA, $printerMappingsPath) || die("Could not open printer mappings!"); @mapData=; close(DATA); open(DATA, $printerDefinitionsPath) || die("Could not open printer definitions!"); @defData=; close(DATA); # make our hash table of printer and their defining commmands foreach $line (@defData) { chop($line); ($line)=split(/\#/,$line); ($name,$command)=split(/ |\t/,$line, 2); $command=trim($command); $addPrinterCommands{($name)}=$command; } # decide which printers we need, based on our ip address and the printer definitions foreach $line (@mapData) { chop($line); ($line)=split(/\#/,$line); ($range,$printer_str)=split(/ |\t/,$line,2); $printer_str=trim($printer_str); @printer_list=split(/ /,$printer_str); if($range && @printer_list) { if(addressInRange($ip_address,$range)) { print "Host ($ip_address) belongs in $range - add @printer_list\n"; addPrinters(@printer_list); } } } # cancel current tasks print "canceling all printer tasks...\n"; $result=`cancel -a`; # remove exiting printers @curPrinterList=split(/\n/,`lpstat -p | grep printer | awk '{ print \$2 }'`); foreach $p (@curPrinterList) { print "removing $p\n"; $result=`/usr/sbin/lpadmin -x $p` } # create and enable printers from our %printersToAdd hash foreach my $key ( keys %printersToAdd ) { my $value = $printersToAdd{$key}; if($value == true) { $command=$addPrinterCommands{$key}; if($command) { print "$key: $command\n"; `$command`; } else { print "Error: No printer command defined for '$key'\n"; } } } # set the default printer, depending on which lab... if($defaultPrinter) { print("default printer: $defaultPrinter\n"); `/usr/sbin/lpadmin -d $defaultPrinter`; } else { print("Error: No default printer defined"); }