我正在将一台服务器的 CUPS 设置为与另一台 CUPS 服务器具有同一组打印机。我通常使用 CLI,它是一个很长的打印机列表,所以我想知道这可以变得多么简单。查看命令的文档,我发现我可以像这样添加打印机:
lpadmin -p $printer_name -E -v $printer_location -m $printer_model
我可以通过以下方式获得$printer_name
s 和$printer_location
s:
lpstat -v
$printer_model
但我似乎找不到可以告诉我每台打印机的命令。我很想通过刮擦http://localhost:631
来curl
获得人类可读的模型,然后匹配来lpinfo -m
获得适合的东西lpadmin -m
,但我希望有一些更传统的东西。
答案1
您也许可以从 ppd 文件/etc/cups/ppd/
和 (受保护的)文件中 grep 一些内容/etc/cups/printers.conf
,但是 cups 库有一个 Python 接口pycups
,可以与服务器通信以获取信息。例如,
#!/usr/bin/python3
import cups
conn = cups.Connection()
printers = conn.getPrinters()
for printer in printers:
p = printers[printer]
print(printer, p['printer-make-and-model'])
返回的打印机字典包含条目:printer-make-and-model
printer-is-shared
printer-type
device-uri
printer-location
printer-uri-supported
printer-state-message
printer-info
printer-state-reasons
printer-state
。我pycups
在rpm包里找到了python3-cups
。
答案2
没有用于获取已安装打印机的驱动程序的常规命令。
以下是基于 meuh 使用库的建议列出lpadmin -m
可接受的 ppd 规范(相对路径drv:///
和/或URI)的解决方案:gutenprint*://
# hpcups and gutenprint put their version in the model names. That means
# that if you install a printer then update the drivers, the model of the
# printer won't match with those available listed by `lpinfo -m`. So, we'll
# strip the version.
strip_versions() {
sed -E 's/(, hpcups|- CUPS\+Gutenprint).*//'
}
# Using @ as a custom field delimiter since model names have whitespace.
# `join` is to match the printers' model names with those of the available
# drivers.
join -t@ -j2 -o 1.1,2.1 \
<(
ruby -r cupsffi -e '
CupsPrinter.get_all_printer_attrs.each do |name, attrs|
puts "#{name}@#{attrs["printer-make-and-model"]}"
end
' | strip_versions | sort -t@ -k2
) \
<(lpinfo -m | sed 's/ /@/' | strip_versions | sort -t@ -k2) |
# We may get multiple ppd specs per printer. For a few printers, I'm
# getting a relative path from the system model directory and either a
# drv:/// uri or a gutenprint.5.3:// URI. We'll filter out all but the
# first per printer.
awk -F@ '!a[$1]++' |
# Change the @ column delimiter for whitespace and align columns.
column -ts@
cupsffi
是一个可以安装的红宝石宝石gem install cupsffi
。