名称: printer
描述: 通过 macOS 上的 CUPS 管理打印机(发现、添加、打印、队列、状态、唤醒)。
元数据: {"clawdbot":{"emoji":"🖨️","os":["darwin"],"requires":{"bins":["lp","lpstat","lpadmin"]}}}
使用 macOS 内置的 CUPS 命令控制打印机,无需额外 CLI 工具。
# 网络打印机 (Bonjour/AirPrint)
dns-sd -B _ipp._tcp . 2>/dev/null & sleep 3; kill $! 2>/dev/null
# 获取打印机详细信息(主机、端口、资源路径)
dns-sd -L "打印机名称" _ipp._tcp . 2>/dev/null & sleep 3; kill $! 2>/dev/null
# CUPS 原生发现
lpstat -e # 可用的网络目标
lpinfo --include-schemes dnssd -v # dnssd 后端
# IPP 发现
ippfind --timeout 5
# 推荐:免驱动队列
lpadmin -p MyPrinter -E -v "ipp://printer.local:631/ipp/print" -m everywhere
# 设为默认打印机
lpadmin -d MyPrinter
# 启用 SNMP 耗材报告(墨粉余量)
sudo lpadmin -p MyPrinter -o cupsSNMPSupplies=true
lp filename.pdf # 使用默认打印机
lp -d MyPrinter filename.pdf # 指定打印机
lp -d MyPrinter -n 2 file.pdf # 打印 2 份
lp -d MyPrinter -o sides=two-sided-long-edge file.pdf # 双面打印
lp -d MyPrinter -o media=letter file.pdf
lp -d MyPrinter -o ColorModel=Gray file.pdf # 灰度打印
# 直接打印文本
echo "Hello World" | lp -d MyPrinter
# 检查状态
lpstat -p MyPrinter # 打印机状态
lpstat -o MyPrinter # 队列中的任务
lpstat -t # 全部信息
lpq -P MyPrinter # BSD 风格队列视图
# 取消任务
cancel JOB_ID
cancel -a MyPrinter # 取消所有任务
# 启用/禁用
cupsenable MyPrinter # 恢复打印
cupsdisable MyPrinter # 暂停打印机
cupsaccept MyPrinter # 接受新任务
cupsreject MyPrinter # 拒绝新任务
# 列出打印机的可用选项
lpoptions -p MyPrinter -l
# 设置用户默认选项
lpoptions -p MyPrinter -o sides=two-sided-long-edge
# 设置服务器端默认选项
sudo lpadmin -p MyPrinter -o sides-default=two-sided-long-edge
# IPP 状态查询(详细)
ipptool -t ipp://PRINTER_IP/ipp/print get-printer-attributes.test
# 筛选关键信息
ipptool -t ipp://PRINTER_IP/ipp/print get-printer-attributes.test \
| grep -iE 'printer-state|marker|supply|media|error'
# IPP 探测(通常可唤醒打印机)
ipptool -q -T 5 ipp://PRINTER_IP/ipp/print get-printer-attributes.test
# HTTP 探测(唤醒 Web UI 栈)
curl -s -m 5 http://PRINTER_IP/ >/dev/null
# TCP 连接测试
nc -zw2 PRINTER_IP 631
# 每 5 分钟轮询一次(前台运行)
ipptool -q -T 3 -i 300 ipp://PRINTER_IP/ipp/print get-printer-attributes.test
如需持久保活,请创建 launchd 代理。
需要先安装 brew install net-snmp:
snmpwalk -v2c -c public PRINTER_IP 1.3.6.1.2.1.43.11.1.1
注意:打印机可能已禁用 SNMP。请检查远程 UI 设置。
大多数网络打印机在 http://PRINTER_IP/ 提供 Web UI,可用于:
- 睡眠/定时器设置(设置 > 定时器设置 > 自动睡眠时间)
- 网络协议配置(启用/禁用 IPP、SNMP、原始 9100 端口)
- 耗材状态查看
# 打印机卡住/禁用?重新启用它
cupsenable MyPrinter
# 检查设备 URI
lpstat -v MyPrinter
# 移除并重新添加打印机
lpadmin -x MyPrinter
lpadmin -p MyPrinter -E -v "ipp://..." -m everywhere
# CUPS 错误日志
tail -f /var/log/cups/error_log
ipp:// 或 ipps:// URI,而非原始 9100 端口或 LPD-m everywhere 会根据打印机的 IPP 能力自动配置lpoptions -l 来发现