I was listening to the pauldotcom.com security podcast the other day and heard then talking about netcat. This peaked my interests and got me thinking that I should be able to print text files from any Unix-based system on which netcat could be installed/compiled. At first I tried it the simple way:
cat file.txt | nc 10.10.10.201 9100
This did indeed send the file to the printer’s port 9100. There were two problems though. First off, the printer asked me if I wanted to print the buffer or wait until it was finished printing. Pushing the button on the printer made the file print but is not as convenient as it should be. This problem can be fixed with a “-w1” switch. This tells netcat to quit if the input stream is idle for more than 1 second.
cat file.txt | nc -w1 10.10.10.201 9100
This prints the text file almost instantaneously but there is another problem. All the text is stair stepped. As it turns out, Unix uses only newline character at the end of each line. In dos and apparently network laser printers, a newline and a carriage return are two separate things that need to be handled individually. A little bit of sed trickery is just the thing to fix this problem.
sed ‘s/$'”/`echo -e \\\r`/” file.txt | nc -w1 10.10.10.201 9100
Obviously you will need to put your own printer’s IP address there. This will probably only work with a decent networked laser printer. I’ve tested this with my Lexmark Optra T644 and it works great. Special thanks to rkdavis for helping sort out the sed bits.