Skip to content

Instantly share code, notes, and snippets.

@justincjahn
Created July 26, 2012 23:09
Show Gist options
  • Select an option

  • Save justincjahn/3185163 to your computer and use it in GitHub Desktop.

Select an option

Save justincjahn/3185163 to your computer and use it in GitHub Desktop.
Change LCD screen on an HP printer.
#!/usr/bin/env perl
use strict;
use IO::Socket;
#
# Check to see if an element is in a given array.
#
# @param string $strNeedle The string to search for.
# @param array @arrHaystack The array to look in. Defaults to @ARGV.
# @returns undef|int The index of the match.
#
sub inArray {
my ($strNeedle, @arrHaystack) = @_;
@arrHaystack = (!@arrHaystack) ? @ARGV : @arrHaystack;
# Loop through each element of the array
for (my $i = 0; $i < scalar(@arrHaystack); $i++) {
if ($strNeedle eq $arrHaystack[$i]) {
return $i;
}
}
# Nothing was found
return undef;
}
#
# Help block. If the
#
if (scalar(@ARGV) == 0 || defined(inArray("--help")) || defined(inArray("/?"))) {
print STDOUT <<EOF;
hpwnt.pl version 1.0.0
ustin '4sak3n 0ne' Jahn
<http://www.4sk.us>
This script comes with no warranty. This is free software and may be
redistributed in it's current or modified state. The original author(s)
must be given credit. Please see the GNU GPL for more information.
This script connect to a compatible HP JetDirect printer and changes the
ready message to the given string.
Usage: hpwnt.pl hostname message
Options
hostname A valid IP address.
message The new ready message.
Examples
hpwnt.pl 192.168.0.10 "Insert Coin"
EOF
exit;
}
#
# Make sure our arguments are correct.
#
if (defined($ARGV[2])) {
# They didn't put quotes on the second argument. Pop it off, and append to $ARGV[1].
my @arrString = @ARGV;
# The first element is our hostname or ip
shift @arrString;
$ARGV[1] = join("", @arrString);
}
#
# Make sure that we were given a valid hostname.
#
if (!($ARGV[0] =~ /^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$/)) {
print STDOUT "Invalid IP address given. Example: 192.168.1.1.\n";
exit;
}
#
# Try to connect to the printer.
#
my $jetDirect = IO::Socket::INET->new(PeerAddr => $ARGV[0],
PeerPort => "9100",
Proto => "tcp",
Type => SOCK_STREAM)
or die("Unable to connect to printer. Please verify IP address.");
my $strCommand = "\x1B%-12345X\@PJL RDYMSG DISPLAY=\"$ARGV[1]\"\r\n\x1B%-12345X\r\n";
print $jetDirect $strCommand;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment