Linux

是否有命令獲取現有列印機的型號/驅動程序/ppd 以用於“lpadmin -m”?

  • August 1, 2021

我正在設置一台伺服器的 CUPS,使其具有與另一台 CUPS 伺服器相同的列印機集。我通常住在 CLI 中,它是一長串列印機,所以我想知道這可以做得多麼簡單。查看命令的文件,我發現我可以像這樣添加列印機:

lpadmin -p $printer_name -E -v $printer_location -m $printer_model

我可以得到$printer_names 和$printer_locations :

lpstat -v

但我似乎找不到可以告訴我$printer_model每台列印機的命令。我很想通過抓取來破解它http://localhost:631curl獲得人類可讀的模型,然後匹配lpinfo -m以得到適合的東西lpadmin -m,但我希望有一些更傳統的東西。

沒有獲取已安裝列印機驅動程序的正常命令。

這是基於 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是一個可以安裝的 ruby​​ gem gem install cupsffi

您可能能夠從 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

引用自:https://unix.stackexchange.com/questions/662565