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.
If you like the content on this site, please support it by using this link to order from Amazon. You know you were going to go there and buy stuff anyhow so why not help me pay the hosting bill.
Hey, thanks for the post 🙂
The sed command didn’t work for me (maybe some character format problem). But it works great using unix2dos file | nc x.x.x.x 9100
I just retested it. Still works perfect for me. I’m on a mac though and some programs like sed are slightly different. I don’t have unix2dos but if that works it’s certainly much easier than all the sed stuff.