Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatically generate printer lists at login Printers
Managing hundreds of Macs and almost that many printers can prove to be difficult. Here is the method we use at the school where I work. It makes updating units after a new printer is installed a snap. This method works with 10.3 and 10.4, but I haven't tested 10.2. We use a LoginHook to call the script that will add the printers. Here is an example of how to add a LoginHook. In Terminal, type:
sudo defaults write com.apple.loginwindow LoginHook /bin/loginscript.sh
Any script you put in /bin called loginscript.sh will run every time a user logs in. It runs as root, too, so you can have it do all kinds of cool stuff. Now on to the script (I've also included an uncommented version at the end).

Here's the generic, commented script. The first part of the script will delete all of the current printers. We do this because we might need to read a printer with new info. UNIX allows multiple queues with the same name, which can get confusing. The list of printers is stored in /etc/printcap. We'll parse /etc/printcap with sed to create a script to delete the current printers. sed is a UNIX command line tool for substituting text, among other things. We'll use lpadmin -x "Printer Name" to delete the printers. Now all of the printers have been deleted

Adding a printer with lpadmin is easy. The flags I use are:
  • -p "Printer name"
  • -L "location"
  • -E enabled
  • -v device name (IP Address in this case)
  • -P "Printer Description"
Here is an example:
/usr/sbin/lpadmin -p G-Wing-HP -L G104 -E -v \
 lpd://192.168.2.2 -P \
 "/Library/Printers/PPDs/Contents/Resources/en.lproj/HP\ LaserJet 4050 Series.gz"
Set the default printer like this
lpoptions - d G-Wing-HP
You can add as many as you'd like, just make sure you add the printer before you try to set it as the default. You can also add via ipp instead of lpd. We don't use AppleTalk any more, but you can add AppleTalk printers with a line like this:
lpadmin -p Printer_Display_Name -E -v \
pap://Zone/Printer-Name-With-Dashes/LaserWriter -P /Path/To/PPD
Once the script is running at login, you can update it with Apple Remote Desktop, or by editing the script remotely via ssh. The script resets the queues at every login, so any 'stuck' print jobs are cleared up by a logout/login. New printers can be added and are immediately available after login. You can add multiple printers with the same IP address and different PPDs for custom jobs.

We use this scripting as the basis for a Platypus app that will change printers on demand. If a teacher with a laptop visits a different building, they need only open an app in the Dock and they get the printers for that building. When they return, they can open the app for the original building, and the printers are restored. When imaging, we use a variation on this that sets printers via hostname. It gets a little trickier, but I can post this if anyone is interested.

Here's an uncommented version of the script from above.
    •    
  • Currently 2.00 / 5
  You rated: 1 / 5 (7 votes cast)
 
[15,835 views]  

Automatically generate printer lists at login | 16 comments | Create New Account
Click here to return to the 'Automatically generate printer lists at login' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Careful with that Axe, Eugene
Authored by: exel on Jul 31, '06 07:55:59AM

Just a minor point, the /tmp directory can be written to by anyone. If your machine is a multi user system, this script creates several security problems. If another user manages to create a /tmp/printer.final with 755 permissions, this hint's script will silently fail at removing it and may allow a malicious user to execute commands with _your_ user privileges.



[ Reply to This | # ]
Careful with that Axe, Eugene
Authored by: AtomicMonkey on Jul 31, '06 08:38:30AM

The script I use cleans up before and after. So this is not an issue. Running as root, a loginhoook creates a file that cannot be overwritten.



[ Reply to This | # ]
Careful with that Axe, Eugene
Authored by: exel on Jul 31, '06 03:08:33PM

Ah, it wasn't clear form context that the script ran as root. I'd say you're still leaving some space for race conditions. If another user runs a script that constantly tries to create a file called /tmp/printer.final with some nefarious code in it, through the magic of multitasking he has a good chance of getting it executed _after_ your login script performs the rm -f on /tmp/print* but _before_ it got a chance to output to /tmp/printer.final (the window is even quite large because you are spending time sedding over the old printcap). Any unwanted commands in this temp-file will be executed as the root user.

Avoid /tmp for this kind of thing, or use mktemp, that's what it's there for.



[ Reply to This | # ]
Careful with that Axe, Eugene
Authored by: AtomicMonkey on Jul 31, '06 08:55:01AM

I see what you are talking about. You mean the Platypus script. Like I said this is just the basis for that script. The Platypus script uses /Users/$USERNAME/Library/tmp to do it's business.

USERNAME=`whoami`

test ! -e /Users/$USERNAME/Library/tmp && mkdir /Users/$USERNAME/Library/tmp

Then just use that temp directory in place of /tmp.

You're right, I should have mentioned that.



[ Reply to This | # ]
Careful with that Axe, Eugene
Authored by: Trunkmonkey on Jul 31, '06 02:28:46PM

Eugene? I told you never to call me that! The name's Plague. Mr. The Plague.



[ Reply to This | # ]
Automatically generate printer lists at login
Authored by: lewsmind on Jul 31, '06 08:17:07AM

I've been looking for a way to do this. Adding the same printers back to multiple machines is a big pain. Thanks!



[ Reply to This | # ]
Automatically generate printer lists at login
Authored by: gr0k on Jul 31, '06 08:39:42AM

Why not just create a cups print server? All new printers will immediately show up on all clients on the same lan. If its on a different subnet, you can configure cups to poll other print servers.



[ Reply to This | # ]
Automatically generate printer lists at login
Authored by: AtomicMonkey on Jul 31, '06 08:59:49AM

We have almost 100 printer in this building. Every teacher doesn't want every printer. Not to mention every printer from every building they visit. In a smaller workgroup environment a CUPS server setup would be ideal, because I wouldn't have to maintain a script at all.



[ Reply to This | # ]
Automatically generate printer lists at login
Authored by: fab23 on Jul 31, '06 11:54:45AM
Users do not need to directly see all the 100 CUPS printers. They can just select the printers they want to use from the "shared printers" menu. These printers then will stay in the short list of printers they see first.

See our readme about Printing with CUPS (MacOS X) for example. Even if the pictures are in German and from OS X 10.3 (10.4 looks similar). Currently we have around 130 printers in CUPS, see the printer overview for the complete list. ;)

bye
Fabian

[ Reply to This | # ]

Automatically generate printer lists at login
Authored by: klktrk on Jul 31, '06 09:18:38AM

I wouldn't put anything customized in /bin, since that is a directory Apple can overwrite at will during an update.

Much better to put something like that in either
/usr/local/bin
or
/usr/bin
or, the Apple Way™
/Library/Scripts



[ Reply to This | # ]
Automatically generate printer lists at login
Authored by: AtomicMonkey on Jul 31, '06 08:09:10PM

I see what you are saying, but for my situation it is best to have a virtually hands off setup. The less the users are forced to configure something (like adding their own printers) the better off I am.

We have a lot of users who are just now transitioning from the Chooser. They are not ready for the Printer Setup Utility. I give them exactly the printers they need for the building/wing they are in. If they move to a different wing they automatically get new printers on the machine in that wing. If they are mobile they have to learn to click the app in the dock for new printers. It is easy on the users, so it is easy on me.



[ Reply to This | # ]
Automatically generate printer lists at login
Authored by: jpbjpbjpbjpb on Jul 31, '06 08:40:22PM

Could you post your platypus script, too?



[ Reply to This | # ]
what's the point...use workgroup manager
Authored by: montalvd on Aug 01, '06 06:13:57AM

apple osx server's workgroup manager lets you manage printer lists in multi user environments. no need for hacks.

don

---
don montalvo, nyc



[ Reply to This | # ]
what's the point...use workgroup manager
Authored by: Beorn on Aug 01, '06 10:54:41AM

The problem with using Workgroup Manager (in my experience at least) is that it is horribly broken and dysfunctional. It's gotten better with each revision, but in my labs (~40 Tiger G5's, dual 2.3 GHz Xserve G5 on Tiger, gigabit throughout, and 8 high-end output devices) we've tried several times to implement a WM-based printing solution, and it's caused nothing but trouble every time. I definitely plan to check this hint out as soon as possible, as it will make for a good band-aid until Apple fixes the Workgroup Manager system properly.

---
~ Beorn



[ Reply to This | # ]
what's the point...use workgroup manager
Authored by: bdog on Aug 01, '06 11:54:15AM

Yes, managed printers have so many problems. Stay tuned for my upcoming hint regarding these issues!



[ Reply to This | # ]
Automatically generate printer lists at login
Authored by: wll on Aug 08, '06 09:33:46PM

I ran into this posting from a search to delete all the CUPS printers defined on the system. I think that I can simplify your shell script.

Try: "cat /etc/printcap | grep -v '^#' | cut -d'|' -f1 | xargs lpadmin -x" which will do the same as your script. The idea is to list everything in the /etc/printcap file (the cat), delete any comment lines (the grep), get only the print queue name (the cut), and finally execute "lpadmin -x" command on each of the resulting print queue names.

Granted, this single line has a lot of potential gotcha's. For example, I would be worried that a print queue name could contain space characters or tabs, and I would want to enclose these names in quote marks. I am also assuming that the contents of the /etc/printcap file is extremely simple which may be a very poor assumption. But it does show that all of your individual lines can be simplified to a single command line.



[ Reply to This | # ]