V

2012-12-05
munin plugin for HP USB printers

When I got my printer I wanted to have a possibility to see how much ink was left in the cartridges. After reading the source of the printer driver and googeling I found undocprint.org. A simple perl script for testing showed that these commands also work on my HP CP 1215:

#!/usr/bin/perl -w
#
use strict;
my $out = "";

open DEV, '+<', "/dev/usb/lp0"
  or die "$0: failed to open device: $!\n";

select DEV; $|=1; select STDOUT;

print DEV "\e\%-12345X\@PJL\r\n";
print DEV "\@PJL ",uc join(" ", @ARGV),"\r\n";
print DEV "\e\%-12345X";
sleep 1;

$SIG{'ALRM'} = sub { warn "# sigalrm\n"; print $out; exit; };
alarm 10;

my $char = getc DEV unless eof DEV;
while (defined $char and $char ne "\f") {
    $out .= $char;
    last if eof DEV;
    $char = getc DEV;
}
alarm 0;

close DEV;
$out =~ s/\r\n/\n/msg;
print $out;
# perl ./pjl-cmd.pl info supplies
@PJL INFO SUPPLIES
CARTRIDGE = BLACK
Type = HP
PartNumber = 10752
ReorderPN = CB540A
PercentRemaining = 92
PagesRemaining = 2037
LowReached = 0
SerialNumber = 303369266
PageCount = 163
InstallDate = 0
LastUseDate = 0
Manufacture_Date = 20071122
SuppliesStatus = 1
CartridgeOutOverrideActive = 0
Capacity_Units = 4
Total_Capacity = 2827594000
AvgAreaCvrg = 5
AvgAreaCvrg1 = 90
AvgAreaCvrg2 = 3
AvgAreaCvrg3 = 4
AvgAreaCvrg4 = 2
AvgAreaCvrg5 = 1

CARTRIDGE = CYAN
Type = HP
PartNumber = 10755
ReorderPN = CB541A
PercentRemaining = 25
PagesRemaining = 79
LowReached = 0
SerialNumber = 1091895559
PageCount = 239
InstallDate = 0
LastUseDate = 0
Manufacture_Date = 20080619
SuppliesStatus = 1
CartridgeOutOverrideActive = 0
Capacity_Units = 4
Total_Capacity = 1058999250
AvgAreaCvrg = 16
AvgAreaCvrg1 = 100
AvgAreaCvrg2 = 0
AvgAreaCvrg3 = 0
AvgAreaCvrg4 = 0
AvgAreaCvrg5 = 0

CARTRIDGE = YELLOW
Type = HP
PartNumber = 10753
ReorderPN = CB542A
PercentRemaining = 23
PagesRemaining = 71
LowReached = 0
SerialNumber = 1665728774
PageCount = 240
InstallDate = 0
LastUseDate = 0
Manufacture_Date = 20080619
SuppliesStatus = 1
CartridgeOutOverrideActive = 0
Capacity_Units = 4
Total_Capacity = 943475250
AvgAreaCvrg = 15
AvgAreaCvrg1 = 100
AvgAreaCvrg2 = 0
AvgAreaCvrg3 = 0
AvgAreaCvrg4 = 0
AvgAreaCvrg5 = 0

CARTRIDGE = MAGENTA
Type = HP
PartNumber = 10754
ReorderPN = CB543A
PercentRemaining = 24
PagesRemaining = 75
LowReached = 0
SerialNumber = 1668088071
PageCount = 239
InstallDate = 0
LastUseDate = 0
Manufacture_Date = 20080619
SuppliesStatus = 1
CartridgeOutOverrideActive = 0
Capacity_Units = 4
Total_Capacity = 894006750
AvgAreaCvrg = 14
AvgAreaCvrg1 = 100
AvgAreaCvrg2 = 0
AvgAreaCvrg3 = 0
AvgAreaCvrg4 = 0
AvgAreaCvrg5 = 0

With these infos a plugin for munin was easy: hp_usb_printer_. The resulting graph looks like this:

HP CP 1215 Graph

Permalink | munin, perl.
2012-12-23
Searching for files on an sftp- / scp-only server

While converting old sh/bash scripts which handled files with user supplied file names, I had to delete the files also on a server which is only accessible via sftp and scp. A quick check with Net::SSH2 showed that it could easily be done in perl:


#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SSH2;

my $ssh_key = "$ENV{HOME}/.ssh/id_rsa";
my $ssh = Net::SSH2->new();
$ssh->connect(shift @ARGV)
    or die "$0: failed to connect: $!\n";
$ssh->auth_publickey($ENV{USER}, "$ssh_key.pub", $ssh_key)
    or die "$0: auth failed...\n";
my $sftp = $ssh->sftp()
    or die "$0: failed to request sftp subsystem: ",($ssh->error)[2],"\n";
foreach (@ARGV) {
    next unless $sftp->stat($_);
    $sftp->unlink($_)
        or warn "$0: failed to unlink '$_': ",($ssh->error)[2],"\n";
}
$ssh->disconnect();

A few versions later I had a script which can search for files on an sftp-server like find can do it on a local file system: sfind

Permalink | perl, sftp, ssh.
$HOME
Archives

Tags