V

2013-01-01
Recovery booting a QNAP TS-419PII

While following the instructions of the recovery boot at the debian qnap pages I had the problem that the system would not load the provided image via TFTP. In tcpdump you could see that the system requested a slightly different file via DHCP:

12:47:44.935498 IP (tos 0x0, ttl 255, id 1, offset 0, flags [DF], proto UDP (17), length 329)
    10.7.7.21.68 > 255.255.255.255.67: [no cksum] BOOTP/DHCP, Request from 00:08:9b:cd:26:04, length 301, xid 0x9bcd5771, secs 12, Flags [none] (0x0000)
          Your-IP 10.7.7.21
          Server-IP 10.7.7.253
          Client-Ethernet-Address 00:08:9b:cd:26:04
          Vendor-rfc1048 Extensions
            Magic Cookie 0x63825363
            DHCP-Message Option 53, length 1: Request
            MSZ Option 57, length 2: 576
            Server-ID Option 54, length 4: 10.7.7.253
            Requested-IP Option 50, length 4: 10.7.7.21
            Vendor-Class Option 60, length 11: "F_TS-419P2+"
            Parameter-Request Option 55, length 5: 
              Subnet-Mask, Default-Gateway, Hostname, BS
              RP

While the instrunction lists the filename for these machines as "F_TS-419P2", this (newer?) revision requests "F_TS-419P2+".

Permalink | dhcp, linux, qnap.
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.
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-11-26
how NOT to use xargs

What happens in the following snippet if $TEMPFILE is empty?


#!/bin/sh

TEMPFILE=$( tempfile )
find /some/dir -type f -mtime +60 -print0 > $TEMPFILE

num=0
xargs -n 1 -0 ls -Q < ${TEMPFILE} | while read file; do
    num=$(( num + 1 ))
    if [ $num -gt 1000 ]; then
        break
    fi
    file=$( echo "$file" | sed -e 's/^"//; s/"$//; s/\\"/"/g' )
    mv "$file" /some/other/dir/
done

... it will move all files from the current directory to /some/other/dir/.

One workaround is to use the --no-run-if-empty switch for xargs:

  --no-run-if-empty
  -r     If the standard input does not contain any nonblanks, do not run
         the command.  Normally, the command is run once even if there is
         no input.  This option is a GNU extension.
  (GNU xargs(1))

... we decided to rewrite in perl :)

Permalink | shell.
2012-11-25
ssh draining stdin

We had a script which does some remote commands based on preconditions. It stopped processing the loop after the first time the condition was true:

#!/bin/sh
DATA="host1 1
host2 2
host3 3
host4 4
host5 5"

echo "$DATA" | while read host num; do
    if [ $(( num % 2 )) -eq 1 ]; then
        ssh $host "echo $num"
    fi
done
$ sh test1.txt 
1
$

I found that ssh was reading all the remaining data and the next read failed and the loop ended. When redirecting ssh's stdin from /dev/null everything worked as expected. As alternative to redirecting stdin you can also use the -n switch for ssh (i.e. run ssh -n $host "echo $num" ):

#!/bin/sh
DATA="host1 1
host2 2
host3 3
host4 4
host5 5"

echo "$DATA" | while read host num; do
    if [ $(( num % 2 )) -eq 1 ]; then
        ssh -n $host "echo $num"
    fi
done
$ sh test1.sh
1
3
5
$
Permalink | shell, ssh.

Created by Chronicle v4.4

$HOME
Archives

Tags