10

Inside of a script I send a certain file to print using the lpr command on an user-selected printer.

The list of the printers, from which the user selects the one to use, I get using the lpstat -a command and then considering everything before the first space as the name of the printer.

This works but relies heavily on the specific output format of the lpstat -a command. I'm afraid that in the future (or now on different distributions) if the output format changes it will break the script.

So, is there a way to get the names of all the installed printers in a way that is safer than the one I'm using?

1

CUPS is (partly) GPL-licensed, so you could just grab the source of lpstat(1) and create your own little (GPL-licensed) give-my-list-of-printers-program that will never change the way it outputs data.

(1) It's in cups-XX/cups/systemv/lpstat.c, I just don't understand the versions numbers there.

2
  • This is a good solution, but unfortunately the "script" is actually a Java applet, i cannot change the target system or install new stuff on it. It's related to this question: stackoverflow.com/questions/13743738/… And i thought that using lpr instead was a good idea – Enoon Dec 9 '12 at 20:15
  • Hmm, there's CUPS4J, which might or might not be an option... it can "get printers objects from a CUPS server that can be used to print and monitor jobs" – sr_ Dec 10 '12 at 8:12
4

I also need a list of printers and had the same concern. Following the above suggestion I checked the source of lpstat.c . I was expecting it to be a suid so it could read "/etc/cups/printers.conf" . I now realise it does not do this.

Instead it sends a HTTP POST request to localhost:631 where cupsd runs. The request is of the form Content-Type: application/ipp and things such as requested-attributes auth-info-required . The reason not to be more specific is that it uses a non-standard encoding (see: http://en.wikipedia.org/wiki/Internet_Printing_Protocol) ... so I suspect actually parsing the output of lpstat -a is actually cleaner.

3

It's been six years since the original question, I'm using Cups 2.2.7 And still 'lpstat -a' returns the name of the printer in the first word. So I think it is safe to assume that it will continue as the right way to get a list of installed printers.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.