return to first page linux journal archive
keywordscontents

Listing 2. Perl Script for Pull Method

#!/usr/bin/perl
$host="monitored.one";     # host to log to
                            # (machine to monitor)
$file="var/log/imp-logs";  # logging file on
                            # remote machine
$login="wallace"; # login name on remote machine
$ssh_opts="-q -C -o 'FallBackToRsh=no'";
                            # options to ssh
#$ssh_opts="-q +C";        # better for ssh2
$cmd="tail --follow=name --retry";     
                       # the tail command executed
#$cmd="tail --follow"; # this could be
                             # used with old tails
$local_log="pulllog-log";    # local log for
                                        # failures
sub open_remote {            # open the connection
  open(SSH,"|ssh $ssh_opts -l $login $host
    ".'"'."$cmd $file".'"');"
  select(SSH);
  $|=1;              # make it unbuffered
}
if ($local_log) {       # if local logging enabled
  open(LOG,">>$local_log");  # open log file
  select(LOG);
  $|=1;
}
select STDOUT;
$SIG{'PIPE'}='IGNORE';
open_remote();   # try to establish the connection
while (1) {
  while (<SSH>) {
      print STDOUT;     # print on our side
  }
  print LOG "remote end was dead at " .
    `date` if ($local_log);
  close(SSH);           # if error occured
  open_remote();    # try to open connection again
}