| 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | # Configuration stuff |
| 4 | # Timeout in seconds |
| 5 | $mytimeout=10; |
| 6 | |
| 7 | # Random nonsense messages |
| 8 | @nonsensemessages = ( |
| 9 | # |20 |40 |
| 10 | "FEED ME WITH LITTLE PUPPIES", |
| 11 | "All your base are belong to us.", |
| 12 | "You're printing way too much.", |
| 13 | "Paper jam? How does that taste?", |
| 14 | "Zum Weiterdrucken Muenze einwerfen", |
| 15 | "Initiating robot uprising", |
| 16 | "E: You have exceededyour print quota" |
| 17 | ); |
| 18 | |
| 19 | use IO::Socket; |
| 20 | $|=1; # Autoflush |
| 21 | |
| 22 | # Parameter 1: Socket |
| 23 | # Parameter 2: PJL status readback command |
| 24 | # Returns: printers reply, or undef. |
| 25 | sub sendpjlstatusreadback($$) { |
| 26 | my $sock = $_[0]; |
| 27 | my $cmd = $_[1]; |
| 28 | my $res = ''; my $ll; |
| 29 | print($sock "\033%-12345X\@PJL $cmd\n\033%-12345X"); |
| 30 | local $SIG{'ALRM'} = sub { |
| 31 | die("Status readback timed out after $mytimeout seconds.\n"); |
| 32 | }; |
| 33 | alarm($mytimeout); |
| 34 | while (sysread($sock, $ll, 1)) { |
| 35 | #printf("0x%02x ", ord(substr($ll, 0, 1))); |
| 36 | if ($ll eq "\x0C") { # "form feed" = official End of status. |
| 37 | alarm(0); |
| 38 | return $res; |
| 39 | } |
| 40 | #print("'$ll\n'"); |
| 41 | $res .= $ll; |
| 42 | } |
| 43 | print("Warning: status read ended with EOF or error\n"); |
| 44 | alarm(0); |
| 45 | return $res; |
| 46 | } |
| 47 | |
| 48 | # Parameter 1: Socket |
| 49 | # Parameter 2: PJL status readback command |
| 50 | # Parameter 3: 0 = No Open oder Close, 1 = Send "start of PJL command", |
| 51 | # 2 = Send "end of PJL commands", 3 = Send both |
| 52 | # Returns: nothing |
| 53 | sub sendpjlcommand($$$) { |
| 54 | my $sock = $_[0]; |
| 55 | my $cmd = $_[1]; |
| 56 | my $tosnd = ''; |
| 57 | if ($_[2] & 1) { |
| 58 | $tosnd .= "\033%-12345X"; |
| 59 | } |
| 60 | $tosnd .= "\@PJL $cmd\n"; |
| 61 | if ($_[2] & 2) { |
| 62 | $tosnd .= "\033%-12345X"; |
| 63 | } |
| 64 | print("Sending command: '$tosnd'\n"); |
| 65 | print($sock $tosnd); |
| 66 | } |
| 67 | |
| 68 | if (@ARGV != 1) { |
| 69 | print("Syntax: $0 [printerip]\n"); |
| 70 | exit(1); |
| 71 | } |
| 72 | $printerad = $ARGV[0]; |
| 73 | my $SOCKET = new IO::Socket::INET( |
| 74 | PeerAddr => $printerad, |
| 75 | PeerPort => '9100', # 9100 = jetdirect |
| 76 | Proto => 'tcp', |
| 77 | ); |
| 78 | unless ($SOCKET) { |
| 79 | print("Failed to connect to printer $printerad Port 9100: $!.\n"); |
| 80 | exit(1); |
| 81 | } |
| 82 | print("Connected.\n"); |
| 83 | binmode($SOCKET, ':raw'); |
| 84 | print(sendpjlstatusreadback($SOCKET, "INFO STATUS")); |
| 85 | print(sendpjlstatusreadback($SOCKET, "INFO PAGECOUNT")); |
| 86 | # print($SOCKET "\033%-12345X\@PJL INFO STATUS\r\n\@PJL INFO PAGECOUNT\r\n\@PJL RDYMSG DISPLAY = \"test 2 3 4\"\033%-12345X\r\n"); |
| 87 | sendpjlcommand($SOCKET, 'RDYMSG DISPLAY = "'.$nonsensemessages[rand(@nonsensemessages)].'"', 3); |
| 88 | close($SOCKET); |