Use-case: Laptop (or pi) running debian-flavor linux with internet via wifi. Is connects by direct ethernet cable to older HP-Laserjet with port 9100 open but without IPP printing. The script makes port 9100 availble on wifi (via iptables and avahi-publish). Printer never can see internet, and cups is not needed at all, tho cups can be running, as long as printer sharing is off.
There are commercial devices to add wifi capability to ethernet-only laser printers, but I wanted a simpler way that was as automatic as possible: the less user intervention needed the better. I know cups can share printers, but while it seemed to work at first, sometimes it would not, with cups saying "Filter failed", or garbled printouts due to postcript ppd I think.
For example, when I connected my old HP LaserJet P2055dn by usb to my debian laptop, it automatically shared it (as I told cups to share printers. But cups, without asking, chose to use a postscript driver, which did not work very well. However, when I connect by ethernet cable instead of usb, cups apparently used avahi to get model, etc directly from printer, and automatically chose a trouble-free pcl ppd instead of trouble-prone postscript. This seemed to work ok at first, but later, jobs that worked before quit with cups saying "Filter failed".
So I came up with method below that does not require cups server at all, but any cups clients on wifi. like chromebooks, macos, etc, can see and use the printer with little to no setup: that is, without having to manually select model nor ppd/driver, etc.
lpinfo -m | grep -i "HP LaserJet P2055dn"
drv:///hpcups.drv/hp-laserjet_p2055dn-pcl3.ppd HP LaserJet p2055dn pcl3, hpcups 3.22.10
So, I needed a script to tell cups to use the pcl3 driver instead of postscript. While I was at it, I would rather the connection from the laptop to the printer be by ethernet cable instead of usb but still have printer automatically added and shared. The print.sh bash script at (gist.github.com/cyphunk/print.sh )[https://gist.github.com/cyphunk/a0d19a17a893cca2a54530459407737c] very nicely adds and deletes printers in a simple interactive tui. I adapted some of that script and think I was able to make it non-interactive for my particular use case.
Note: I chose to use self-assigned link-local 169.254.0.0/16 subnet on eth0 as I did NOT want my old HP laserjets to try to get on the internet and self-update. After some jet-direct firmware updates, the laserjet starts rejecting 3rd party non-oem toner carts. That is, per AI, auto-updating HP printer firmware (often called Dynamic Security) updates to block third-party or refilled toner cartridges, displaying errors like "Non-HP chip detected" or "Event Code: 10.00.46"... So, I ensure that our HP Laserjets are never able to see the internet, keeping them always on non-routed subnet. So I can continue to use 3rd-party toner cartridges which work just fine, cost much less. and probably better for environment as most are recycled.
Disable ipv6 on ethernet and in avahi.conf Ensure eth0 is link-local: it should have a self-assigned 169.254.x.x (assign one manually if need be) You will need avahi, hplip, and at least the first few cups packages below...
sudo apt install avahi-daemon avahi-utils
apt list | grep avahi ## on my debian currently has these versions...
avahi-daemon/now 0.8-10+deb12u1 amd64 [installed,local]
avahi-utils/now 0.8-10+deb12u1 amd64 [installed,local]
sudo apt install printer-driver-hpcups ## this is hplip pkg of HP printer drivers on debian
### if also using non-hp printers, install appropriate .deb from those seen by
apt list | grep "printer-driver"
###
### Do `sudo apt install` for pkgs below (not sure all are needed) from `apt list | grep cup`
cups-browsed/now 1.28.17-3+deb12u2 amd64 [installed,local]
cups-client/now 2.4.2-3+deb12u9 amd64 [installed,local]
cups-common/now 2.4.2-3+deb12u9 all [installed,local]
cups-core-drivers/now 2.4.2-3+deb12u9 amd64 [installed,local]
cups-daemon/now 2.4.2-3+deb12u9 amd64 [installed,local]
cups-filters-core-drivers/now 1.28.17-3+deb12u2 amd64 [installed,local]
cups-filters/now 1.28.17-3+deb12u2 amd64 [installed,local]
cups-ipp-utils/now 2.4.2-3+deb12u9 amd64 [installed,local]
cups-pk-helper/now 0.2.6-1endless1bem7 amd64 [installed,local]
cups-ppdc/now 2.4.2-3+deb12u9 amd64 [installed,local]
cups-server-common/now 2.4.2-3+deb12u9 all [installed,local]
cups/now 2.4.2-3+deb12u9 amd64 [installed,local]
If /etc/rc.local is NOT executing then you need systemd to enable it, typically with commands below... See https://www.linuxbabe.com/linux-server/how-to-enable-etcrc-local-with-sy stemd
sudo systemctl enable rc-local
sudo nano /etc/systemd/system/rc-local.service
### then paste into the file the following lines
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
sudo touch /etc/rc.local
sudo chmod a+x /etc/rc.local
And add lines below to rc.local with sudo nano /etc/rc.local or your favorite text editor
# To follow success or failure of rc.local journalctl -b | grep rc.local
logger "File /etc/rc.local will try to remove all printers once cups has started"
# wait for cups to be running
# Note: On my laptop, dmest shows rc.local running after cups, so wait is just a few seconds at most
The rest of rc.local will appear here in next few days... mean while try the print.sh script to get an idea of what my rc.local will do.