Saturday Nov 07, 2009

I wanted to check whether our business critical web server is up in every 6 hours.

First I thought I run ping every 6 hours via 'cron'. But I want to ping more often once server is detected down and until it comes back up.

So, I came up with this self registering 'at' script. This begins pinging the server in incremental backoff intervals once it's detected down. Starts at 1 minute, and then 2 minutes. 3,4,5... If you replace "+1" wit "*2", it will do exponential backoff. 1,2,4,8,16...
$ cat ~/misc/myServerPing.at
# THISFILE should be full path or relative from $HOME
# Run this in bash by ". {this file}"
THISFILE=misc/myServerPing.at
INTERVAL=1

curl --silent --connect-timeout 8 http://ourserver.sun.com | grep "Our critical page" > /dev/null

if [ $? -ne 0 ]; then
  date | mailx -s "Failed ping to ourserver" my.mail.address@sun.com
  sed "s/^\(INTERVAL=\)1$/\\1$(($INTERVAL+1))/" $THISFILE | at now + $INTERVAL minutes > /dev/null 2>&1
else
  at now + 360 minutes < $THISFILE > /dev/null 2>&1
fi

Sunday Oct 04, 2009

先日UQ WiMAXの以下の端末が届いたので公式にはサポートされないWindows 2003でドライバーがインストールでき通信できるか試してみました。

UQ WiMAX | WiMAX USB TYPE UD01NA
# 製 品 名    UD01NA
# 製 造 元    NECアクセステクニカ株式会社
今のところ問題なく動いています。ドライバーをインストールために以下の変更をしましたが本当に必要だったかどうか定かではありません。
$ diff $SYSTEMDRIVE/DRV/UD01NA/drxvi314.inf{.orig,}
10c10
< signature="$CHICAGO$"
---
> signature="$Windows NT$"

Friday Aug 21, 2009

Windows has an interesting way of starting debugger on process inception.

greggm's WebLog : Inside 'Image File Execution Options' debugging
There are times that you need to debug the startup code for an application, but something else is launching the application.

To do this on Solaris/Linux is another interesting matter. Can dtrace be used? Or do we still use the classic way of replacing binary file with shell scripts? I'll leave it to experts for now.

Back to Windows.. This feature can be used in various good & bad way(e.g. virus) other than debugging. Today, I used this feature to pass an option to java.exe.

There are many applications that spawns "java.exe" and there often is no way to pass java flags. So, I think it's not only me who ever wished injecting java option dynamically, like -XMX. I'll probably write another blog after polishing this script and elaborate how I did for java.exe.

For today, I'll show a sample with "cmd.exe". With "/T:fc" option, cmd.exe shows up on red on white. I know this is a very lame example. But I think this is much safer to try than java.exe example. So..., here goes. Save this as *.vbs file and go ahead with registry editor. To test, run "cmd /k dir" from Windows menu, with system() call in perl, no matter how.

'ToDo: There should be 2 run mode.
'  1. Run from command line with .exe name and injection string passed in as argument.
'     Create registry key(*.exe) and create "Debugger" value under it.
'  2. Run via IFEO. This part is complete.
'
Option Explicit

Dim oShell, cmdLine, i
Set oShell = WScript.CreateObject("WScript.Shell")

'cmdLine = "cmd.exe /K " & WScript.Arguments( 1 ) & " "  'For those programs interacting with stdin/stdout
cmdLine = WScript.Arguments( 1 ) & " "
cmdLine = cmdLine & WScript.Arguments( 0 ) & " "
For i = 2 to WScript.Arguments.Length-1
  cmdLine = cmdLine & WScript.Arguments(i) & " "
Next

oShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cmd.exe\Debugger"
oShell.Run cmdLine,,true   'setting bWaitOnReturn flag to "false" is dangerous!
oShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cmd.exe\Debugger", _
                "cscript //NoLogo " & WScript.ScriptFullName & " /T:fc"
WScript.Quit

Friday Aug 07, 2009

Is it only in Japan? Is it because many of us office workers are not allowed to install tools like IrfanView or Gimp? Don't we all prefer to get image as *.png, *.jpg or in HTML mail?

Anyway, if you get image in newer MSOffice document format , such as *.xlsx *.docx *.pptx (as opposed to xls,doc,ppt), you may be lucky. These are just zip files, like OpenOffice files are.

This bash function worked to view all images in Excel file without cluttering my home directory. Tested on Solaris10 and Windows(Cygwin+X). This requires ImageMagick's display command.
imginxlsx () 
{ 
    unzip -l $1 | cut -c29- | grep -i --color '\.png$' | while read image; do
        unzip -p $1 $image | display -geometry +0+0 png:-;
    done
}

Sunday Jul 05, 2009

How many times have you sent e-mail prematurely on Thunderbird? It's got "Ctrl+Return" shortcut key assigned for "Send Now". It will pop up confirmation dialogue so people like me don't send empty mail. Good... But..., in the dialogue, the default focus is on "Send" button. So, careless people like me will hit 'Return' or 'Space' and send an empty mail anyway.

I tend to use Control key more than an average guy so "Ctrl+Return Return" sequence happens often. So, I decided to change Thunderbird behaviour. I thought I might need to edit Thunderbird binary file. But a little googling found this plugin.

Keyconfig extension - MozillaZine Knowledge Base
The Keyconfig extension (available here) allows you to configure keyboard shortcuts. It works with Firefox, Thunderbird, and (probably) Mozilla Suite/SeaMonkey.

Here's how I downloaded this plugin. I did this because somehow my Firefox couldn't handle this URL well.
curl http://mozilla.dorando.at/keyconfig.xpi > /tmp/keyconfig.xpi

This plugin will add GUI interface for key shortcut config. I just removed the "Ctrl+Return" shortcut. This added one line to prefs.js file under Profiles folder.
user_pref("keyconfig.main.key_send", "!][][");

Now, I'm happy I'm empty e-mail free :)

Saturday Jun 20, 2009

I had been using Adobe Reader 8 to read and print out PDF files. I usually print 2 pages per sheet and I want to use all the paper real estate to make it readable. I want NO white space on paper. Usually, "Fit to Printable Area" option works OK. But for PDF files I downloaded today(actually, one of our product document), this didn't work. About 50% of the papar was blank.

So, I googled and learned that free Adobe Reader *might be* crippled in print out capability.
Print a pdf at larger than intended size? - Scot's Newsletter Forums
Without buying the full Adobe Professional version, I don't know any "hack" methods around that.

I googled further and found free PDF viewer which gives users free hand on print out scaling.
PDF Viewer - Software To Open A PDF File - PDF Editing Software
The FREE PDF-XChange Viewer offers all the feature's you would expect such as support of all PDF file formats from Version 1.0 all the way to the latest 1.7 (Acrobat 8)

Last but most important, you may accuse me of printing out PDF to paper in the first place. Yeah, I know. There is  AmazonKindle. But I doubt this is worth the money outside of US. There are other similar devices from SONY and one from UK company and another from German company.


Thursday May 21, 2009

I recently downloaded bunch of StarOffice presentations. I view these on 2nd display and I want them to autoplay. These are XML files. So, I thought I could write XSLT but I'm not good at it. Instead, I thought I could use xml diff tool to apply the change to one file to all other files in batch.



Here are the xml diff/patch tools I tried. The bottom line is: I couldn't make none of these achieve what I want.

  1. xmldiff/xmlrev
    a python tool that figures out the differences between two similar XML files
    It turned out that xmlrev hasn't implemented 'INSERT' yet.
  2. Microsoft XmlDiff
    patch tool checks checksum of original file. So, the patch can be applied to only 1 file.
  3. diffxml - XML Diff and Patch Utilities
    This project aims to provide Open Source XML diff and patch utilities which operate on the hierarchical structure of XML documents.
    This worked for some files but didn't work for files which have a little different structure.
  4. Perl: xupdate - search.cpan.org
    xupdate - Process XUpdate commands over an XML document
    This page explains well. But it didn't work on OpenDocument xml.
  5. another Python tool: 4suite.org
    command-line tool for performing XUpdates on XML documents
    I used the same xupdate output from #1.

So, what will I do next? I'm considering these.
  1. OpenOffice API or macro
  2. Disregard the xml aspect of content.xml file and process it by perl
  3. Use OpenOffice XML tool from here and write XSL
    Xml - OpenOffice.org Wiki
  4. These are the Wiki pages of the OpenOffice.org XML Project

Sunday May 10, 2009

Ever wanted to know which process is using the network bandwidth most? I googled and learned that, on Linux,  iftop seems to be able to tell which port is the busiest. But 'iftop' doesn't seems to be compilable on cygwin. So, I googled more and found several similar Windows GUI apps. But they are all shareware and they are overkill for my purpose anyway. So, I wrote a perl to parse tcpdump output to aggregate by local ports.

On Windows, it requires 'windump'.
WinDump: tcpdump for Windows
WinDump is the Windows version of tcpdump, the command line network analyzer for UNIX. WinDump is fully compatible with tcpdump

The output will look like this.
$ perl /c/e/tmp/active-port.pl 5 1000
962 packets     other, e.g.) arp                57850   bytes.
10 packets      220.14.81.26:1275               4526    bytes.
10 packets      220.14.81.26:1068               906     bytes.
10 packets      220.14.81.26:1256               570     bytes.
4 packets       220.14.81.26:1273               228     bytes.
2 packets       220.14.81.26:21         128     bytes.
1 packets       220.14.81.26:1026               636     bytes.
1 packets       other, e.g.)UDP broadcast               590     bytes.

Here's perl script. Borrowed code from here and here.
#!/usr/bin/perl -w
#
# Parse tcpdump and say which ports are more active
# Usage example: perl active-port.pl '\\Device\\NPF_{55899BD0-EC52-4066-ABA7-53849ACAA19A}' 100 | sort -t $'\t' -k 3nr
use strict;

if ($#ARGV != 1) {
    die "$0: Need exactly two arguments. Interface name/# and packet count to capture.\n";
}
my $tcpdump = "tcpdump";
my $index=0, my @local_ipaddrs, my $address;

if (defined $ENV{OS} && $ENV{OS} =~ "Windows_NT") {
    use Sys::Hostname;
    $tcpdump = "windump";
    my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname hostname;
    foreach $address (@addrs) {
        my ($a,$b,$c,$d) = unpack('C4',$address);
        $local_ipaddrs[$index++] = "$a.$b.$c.$d";
    }
} else {
    require IO::Interface::Simple;
    my @interfaces = IO::Interface::Simple->interfaces;
  loop: for my $if (@interfaces) {
      next loop if ( !defined $if->address );
      $local_ipaddrs[$index++] = $if->address;
  }
}

open(TCPDUMP, "$tcpdump -i $ARGV[0] -c $ARGV[1] -nntqep 2>/dev/null |");
eof(TCPDUMP) && die "$0: Could not run $tcpdump: $?\n";
my %activity, my %bytes;
while (<TCPDUMP>) {
    my ($link_level_length, $rest) = ($_ =~ /^[0-9a-f:]{17}\s>\s[0-9a-f:]{17},\s\S+,\slength\s(\d+):\s(.*)/);
    if (my ($addrport1,$addrport2) = ($rest =~ /(\d+\.\d+\.\d+\.\d+\.\d+)\s>\s(\d+\.\d+\.\d+\.\d+\.\d+):/)) {
        my $local;
      local_ipaddrses: foreach $address (@local_ipaddrs) {
          if ($addrport1 =~ /^$address\./) {
              $addrport1 =~ s/(.*)\./$1:/;
              $activity{$addrport1}++; $bytes{$addrport1}+=$link_level_length;
              $local = "true";
              last local_ipaddrses;
          }
      }
      local_ipaddrses: foreach $address (@local_ipaddrs) {
          if ($addrport2 =~ /^$address\./) {
              $addrport2 =~ s/(.*)\./$1:/;
              $activity{$addrport2}++; $bytes{$addrport2}+=$link_level_length;
              $local = "true";
              last local_ipaddrses;
          }
      }
        if (!defined $local) {
            $activity{"other, e.g.)UDP broadcast"}++; $bytes{"other, e.g.)UDP broadcast"}+=$link_level_length;
        }
    }
    else {
        $activity{"other, e.g.) arp"}++; $bytes{"other, e.g.) arp"}+=$link_level_length;
    }
}

foreach my $addr_n_port ( sort { $activity{$b} <=> $activity{$a} }  keys %activity) {
    print "$activity{$addr_n_port} packets\t$addr_n_port\t\t$bytes{$addr_n_port}\tbytes.\n";
}
exit 0;

This blog copyright 2009 by Katsumi Inoue