Sunday, September 13, 2009

DIY Centralized Printer Management

We currently use Windows servers as our backend.  With 10.4 we basically used MCX settings to push out printer management.  This was broken with 10.5.4 so we had to come up with a home brew solution.  We already use a PHP/SQL solution that controls our imaging solution, so adding printers to that was not that difficult.  In addition, we use GroupLogic ExtremeZ-IP software to dish out our file and printer shares.
No matter where you get your printer list from, you can use the following code to pull printer info, and process the changes.  We will be using the Widget from GroupLogic, they call it a "Zidget".  Contained in the Zidget is an application called "zidgethelper" which can process the URL's of the printer share and install it on the local computer.  If you copy this application, and put it in your /Applications directory, then you can use it to also process URL's from a webpage (this means you can have a web page with a map, and click on locations to install a printer from there...really cool). EZIP also downloads PPD files if needed from the server.
This script is a login script and does the following...
  1. Pulls down a printer list from a web server
  2. Pulls down default printer from a web server
  3. Looks for existing printer list, if none, make a blank
  4. Compare New printer list to Old printer list
  5. Remove any printers that need to be removed
  6. Add any printers that need to be added
  7. set the default for the logged in user
The beauty in this is that is will only add/remove printers that you specify, leaving user added printers alone.  If there are any problems, delete all printers and delete the com.company.printers file.  Once the script runs again, all printers will be re-added.  FYI, we pass several items to the login script.  Some of those items are the logged in user name, Platform UUID (for ByHost settings) and more.  You will see a reference to those variable at the end of the script.
__________ 8< snip ____________
oldprinters="/Library/Preferences/com.company.printers"
newprinters="/tmp/.newprinters"
printers_url="http://www.company.com/printerlist.txt"
default_url="http://www.company.com/default.txt"
# Check for exising printers.txt file, if none, make one
echo "Checking for org.lmsd.printers"
if [ ! -e $oldprinters ];then
echo "Creating com.company.printers"
touch $oldprinters
chown root:wheel $oldprinters
chmod 700 $oldprinters
fi
# cleaning up and creating blank newprinters and defaultprinter files
rm -f /tmp/.newprinters
touch /tmp/.newprinters
rm -f /tmp/.newprinters
touch /tmp/.defaultprinter
# Now we have an existing existing printers.txt, check for server
echo "checking for server"
curl -s --fail -o /tmp/.newprinters $printers_url
RESULT=$?
if [ $RESULT -eq 6 ] ; then
echo "Server not availble or Network Interfaces off"
else
if [ $RESULT -eq 22 ] ; then
echo "Server there, no printers file"
else
if [ $RESULT -eq 0 ] ; then
echo "Found Server and File moving forward"
# We found server, continue with pulling printers and remove existing
# LPD printers and install new ones using ZidgetHelper
echo "Checking for changed printers"
        touch /tmp/.newprinters
diff -q $oldprinters $newprinters >/dev/null
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo "Assigned printers have changed, setting up new printers"
echo "removing printers no longer assigned"
remove=`diff -w $oldprinters $newprinters | awk '/
for printer in $remove
do
lpadmin -x $printer
done
echo "adding newly assigned printers"
add=`diff -w $oldprinters $newprinters | awk '/>/ {print $2}'`
for printer in $add
do
/Library/Widgets/Zidget.wdgt/ShellScripts/ZidgetHelper.app/Contents/MacOS/ZidgetHelper $printer
done
mv -f $newprinters $oldprinters
chown root:wheel $oldprinters
chmod 700 $oldprinters
rm -f /tmp/.defaultprinter
else echo "Assigned printers are the same"
fi
echo "Setting default printer if needed"
curl -s --fail -o /tmp/.defaultprinter $default_url
if [ -e /tmp/.defaultprinter ];then
echo "Setting default printer to `cat /tmp/.defaultprinter` for ${1}"
su ${1} -l -c "touch /Users/${1}/Library/Preferences/ByHost/com.apple.print.PrintingPrefs.$PLATFORM_UUID.plist"
su ${1} -l -c "defaults -currentHost write com.apple.print.PrintingPrefs UseLastPrinterAsCurrentPrinter -bool False"
su ${1} -l -c "lpoptions -d `cat /tmp/.defaultprinter` >/dev/null"
rm -f /tmp/.defaultprinter
fi
fi
fi
fi
exit 0