Hi,

Linksys provides an windoze application that can read the Linksys BEFSR
series of
Home routers log files. Here is a perl script that shows how the logs can be
read.
( Try to maintain parity with our MS Brethern). Linksys told me that the log
info
Was broadcast on port 255 if a particular machine was not picked to get the
Log file. I could not pick up the broadcast so I specified a fixed IP for my
mac.

I simply run the program in a Terminal window, a slicker approach would be
to
Start the program at boot time and write the results to a file, you could
then
łtail ­f˛ the file and see the results.

#!/usr/bin/perl -w
#
# Read the incoming/outgoing log files for
#  the Linksys BEFSR family of routers.
#
#  Jerry LeVan ([EMAIL PROTECTED])
#  May 9, 2002
#  Mac OS X 10.1.4
#
#  Invoke By:  sudo linksys.pl
#
use strict;
use Socket;
use Sys::Hostname;

my ( $count, $hisiaddr, $hispaddr,
     $host, $iaddr, $paddr, $port, $proto,
     $message );

# Standard Boiler Plate for a socket...
$iaddr = gethostbyname(hostname());
$proto = getprotobyname('udp');
$port =   getservbyname('snmptrap', 'udp');
$paddr = sockaddr_in($port, $iaddr);
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto)   || die "socket: $!";
bind(SOCKET, $paddr)                          || die "bind: $!";

while (1) {
  ($hispaddr = recv(SOCKET, $message, 2048, 0)) || die "recv: $!";
# &dump($message);
  &format($message);
# print "\n\n"; 
}

# Got the appropiate offsets here by using dump
sub format {
  my ($string) = @_;
  my ($tmp, $info, $length);

  $tmp = substr($string,71,2);  # length of message here
  $tmp = "\0\0".$tmp;
  $length = unpack "N", $tmp;   # convert to a number
  $info = substr($string,73,$length);
  print timestamp()," ",$info;
}

sub dump  {
 my ($string) = @_;
my ( @nums,@chars,$rowindex,@tmpn,@tmpc,$i);

 #print "In dump strlen = ", length($string) ,"\n";
 # convert file to a array o' bytes
 @nums = unpack "C*", $string;

 # convert file to an array o' chars
 @chars = map { chr } @nums;

 # replace non printable characters by "."
 @chars = map { if(ord($_) < 32 || ord($_) > 127){ $_='.' }else {$_}}
@chars;

 # display stuff
 $rowindex = 0;
 
 while (  $#nums >= 0 ) {
   printf "%5x: ", $rowindex;   # print row number
   @tmpn = splice @nums,0,16;  # break off at most 16 elements
   @tmpc = splice @chars,0,16;  # ditto for chars.

   if( $#nums >= 0) { # not the last row...
           printf "%02x " x 16, @tmpn;  # print the hex values
           print "   " ;               # three spaces
           print @tmpc ;               # now the characters
           print "\n";                 # end of line
   }
   else {  # ugly last row processing
           for $i (0..$#tmpn) {
              printf "%02x ", $tmpn[$i];
           }
           
           for $i ($#tmpn+1..16) { # remainder of row of blanks
              print "   ";
           }
           print @tmpc ,"\n";
        }
   $rowindex = $rowindex + 16;
  }
 }

sub timestamp {
    my @months =("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
                "Oct","Nov","Dec");
    my 
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    $year = $year -100;
    sprintf("[%02u/%s/%02u:%02u:%02u:%02u]",$mday,$months[$mon],$year,
            $hour,$min,$sec);
}


Reply via email to