Get list of installed printers

32,290

Solution 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.

Solution 2

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.

Solution 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.

Share:
32,290

Related videos on Youtube

Enoon
Author by

Enoon

Updated on September 18, 2022

Comments

  • Enoon
    Enoon over 1 year

    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?

  • Enoon
    Enoon over 11 years
    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
  • sr_
    sr_ over 11 years
    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"
  • Powerhouse6666
    Powerhouse6666 over 3 years
    Confirmed 'lpstat -a' returns printers