I have a situation where there are any number of servers and any number of users. I am attempting to write a program that will allow any user to connect to any server and view selected logfile information. Pretty simple idea, here's the catch though. All I can use is what comes with perl 5.00503 and I cannot load any additional modules. Also, no servers of any kind can exist on the target machines.

Expect is not loaded on all of the users stations (SUN Solaris) so it is out of the question. What I have been trying to do is use open3 to telnet to the target machines, tail the log file specified by the user, print that output to a file opened by the program then exit the telnet session and move on to the next machine.

All in all, it works ok, but it is pretty ugly and rough. The target machines all have different information presented to the user upon login so standardizing the process is kind of out of the question.

Any ideas, changes or suggestions would be appreciated.

Here is an little sample code to illustrate what I am doing. Please go easy on the code, it is not 100% as it is in flux between different ideas I have, but it does give the general idea.

#!/usr/bin/perl -w use strict; use POSIX; use IPC::Open3; use IO qw( Select Handle ); $|++; my $host = "deneb"; my $login = "username"; my $pword = "password"; my $logfil = "/var/log/syslog"; my $cmd = "telnet"; my $count = 0; my ($infh,$outfh,$errfh); my $pid; my $sel = new IO::Select; open (OUTFILE, ">deneb.log"); OUTFILE->autoflush(1); eval { $pid = open3($infh,$outfh,$errfh, "$cmd $host" ) }; die $@ if $@; $sel->add($outfh,$errfh); while (my @ready = $sel->can_read){ foreach my $fh (@ready) { my $line; my $len = sysread $fh, $line, 4096; if (not defined $len) { die "Error reading from child: $!\n"; } elsif ($len == 0){ $sel->remove($fh); }else { if ($fh == $outfh){ if ($count == 0){ print "$line\n"; print $infh "$login\n"; print $infh "$pword\n"; print $infh "tail $logfil"; $count = 1; }elsif ($count == 1){ print $infh "\n"; $count = 2; }elsif ($count == 2){ print OUTFILE $line; sleep 1; $count = 3; print $infh "exit\n"; }else {exit}; }elsif ($fh == $errfh){ print "From STDERR: $errfh\n"; }else { print "Should never be here!!\n"; } } } }

In reply to grabbing logfile excerpts from remote machines by gnu@perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.