#!/usr/bin/perl use IO::Socket; $port = 7654; ## This could really do anything you want... sub print_stuff { open in, "/proc/net/dev"; while(<in>) { print $client "$_" if /eth[01]:/ or /ppp[01]:/; } close in; } ## Run the farqin thing: $bn = `basename $0`; `killall -v -q -1 $bn`; if (fork) { print "Forkin away...\n"; exit 0; } else { $0 = "$bn [$port]"; &open_it; &listen_to_it; } sub open_it { $sock = new IO::Socket::INET ( LocalPort => "$port", Proto => tcp, Reuse => 1, Listen => 1 ) or die "Could not create socket: $!\n"; } sub listen_to_it { while($client = $sock->accept) { &print_stuff; shutdown $client, 2; } }
UPDATE (12/14/06): these days I would do this a bit differently, if I'd do it at all... the following is untested.
use strict; my $server = new MyMonitor( 8000 ); run $server; exit 0; package MyMonitor; use strict; use Net::Server; use base qw(Net::Server); 1; sub run { my $this = shift; $this->SUPER::run( port => $this->{port}, proto => $this->{proto} +); } sub new { my $class = shift; my $this = bless {}, $class; $this->{port} = shift || 3000; $this->{proto} = shift || 'tcp'; return $this; } sub process_request { my $this = shift; my $prop = $this->{server}; open IN, "/proc/net/dev" or die "problem opening procnetdev: $!"; if( $prop->{udp_true} ){ $prop->{client}->send($_, 0) while <IN>; close IN; return; } print $_ while <IN>; close IN; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: TCP monitor agent
by jrajg (Initiate) on Feb 11, 2001 at 08:00 UTC |