Printing through an SSH server to LP

In some situations direct access to a printer is restricted (when JetDirect access to port 9100 is blocked). Typically the goal is that everyone prints to a CUPS server for accountability. However, remote printing does not work well and one solution is to SSH a PDF to a server and then lp the file to print the file.

Instead we can simulate a JetDirect port on localhost:9100 and configure any local printer to print there using postscript. This port can be forwarded to the ssh server where a script will process the raw postscript. First, set up a postscript printer to print on the JetDirect port to localhost. Then set up your ssh client to forward your request to port 9100 to the local port 19100 on the server (it can be any port number) by adding the following to your ~/.ssh/config file.

===~/.ssh/config=====================
Host ssh.server
User          username
LocalForward 9100 127.0.0.1:19100
===~/.ssh/config=====================

With the above script when a request to port 9100 is made on your laptop there will be a request to port 19100 on the server. Then add the following file on the SSH server. You can run this script to accept a print job from port 9100 and save it to the file spoolprint.temp. Then we can pass the raw postscript to lp so it can process the raw data.

===print.sh=====================
nc -v -l -p 19100 > spoolprint.temp
lp -o raw spoolprint.temp
===print.sh=====================

Note:
To prevent errors the print.sh script should be run only when you want to print.

Also, because the lp executable is run by you CUPS will account the print job to you.