Python Printer Peril
Posted on 2014-08-07 in misc • 1 min read
I remembered a while back that there was this little PERL utility to set the status message on HP printers using the printer's job language. I decided that I wanted to try to do that with Python, and this is what I came up with:
#!/usr/bin/env python
import socket
import sys
def sendcmd(MESSAGE, IP, PORT=9100):
"""Opens the socket to the printer and sends the strings"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP, PORT))
s.send("@PJL RDYMSG DISPLAY=\""+MESSAGE+"\"\n")
s.close()
if len(sys.argv) < 2 or len(sys.argv) > 3:
sys.exit('Usage: %s "MESSAGE" IPADDRESS [PORT]' % sys.argv[0])
elif len(sys.argv) == 3:
sendcmd(sys.argv[1], sys.argv[2])
else:
sendcmd(sys.argv[1], sys.argv[2], sys.argv[3])
This actually turned out to be pretty simple. There are really only 2 parts: The socket part that sends the commands, and the sys.argv stuff that grabs the command line options and passes it to the sendcmd()
function. I added an option to specify a port, but if none is specificed, it will default to 9100. So then all you need to do is:
./hpprint.py "Your crafty message" 192.168.0.1
Chat has been disabled for this post