OA0 = Omni AI 0
OA0 是一个探索 AI 的论坛
现在注册
已注册用户请  登录
OA0  ›  技能包  ›  printer:通过 macOS 上的 CUPS 管理打印机

printer:通过 macOS 上的 CUPS 管理打印机

 
  deep ·  2026-02-06 09:06:02 · 3 次点击  · 0 条评论  

名称: printer
描述: 通过 macOS 上的 CUPS 管理打印机(发现、添加、打印、队列、状态、唤醒)。
元数据: {"clawdbot":{"emoji":"🖨️","os":["darwin"],"requires":{"bins":["lp","lpstat","lpadmin"]}}}


打印机 (CUPS)

使用 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

添加打印机(免驱动 IPP Everywhere)

# 推荐:免驱动队列
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 代理。

通过 SNMP 查看墨粉余量

需要先安装 brew install net-snmp

snmpwalk -v2c -c public PRINTER_IP 1.3.6.1.2.1.43.11.1.1

注意:打印机可能已禁用 SNMP。请检查远程 UI 设置。

远程 UI(Web 界面)

大多数网络打印机在 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 来发现
  • 睡眠设置最好通过打印机的远程 UI 配置
  • 自动睡眠(1 分钟)可保持服务活跃——打印任务会自动唤醒打印机
  • 如果打印机完全无响应(IPP 端口关闭、HTTP 超时),很可能处于深度睡眠或已关机。请通知用户物理检查/唤醒打印机。
3 次点击  ∙  0 人收藏  
登录后收藏  
目前尚无回复
0 条回复
About   ·   Help   ·    
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
Developed with Cursor