#!/usr/bin/perl -w


#############################################################################
## This machine-generated file was created Tue Jul  9 14:02:28 2002.
## It was built from the following files:
##   rpatc.p		(dated Sun Jul  7 21:34:38 2002)
## 
## Changes to this file will be lost when it is rebuilt
#############################################################################

#
# $Id: //pentools/main/rpat/rpatc.p#2 $
#
# written by :	Stephen J. Friedl
#               Software Consultant
#               Tustin, California USA
#               steve@unixwiz.net / www.unixwiz.net
#
#		=== This code is in the public domain ===
#
#	This little script sends the given "message" to the SNMP netstat
#	Daemon for processing. This message is just a string, and *this*
#	program has no understanding of what the messages mean: we just
#	assemble thing.
#
#	The message - however composed - is sent to the daemon via a single
#	UDP datagram.
#
#	Command line:
#
#	--help          show a small command summary
#
#	--dest=M:P      set the remote machine & port to the given values
#	                We don't do hostname lookups! Must use IP addresses!
#
#	It's followed by a message which being with the first non-dash
#	parameter, and it follows through the rest of the command line.
#	It's an error to have no message, and multiple words for the
#	message are all joined with spaces.
#

use strict;
use IO::Socket;

my $destmachine = "127.0.0.1";          # localhost
my $destport    = 1234;                 # destionation port

$0 =~ s{^.*/}{};			# dump leading path part

my $message = "";

foreach ( @ARGV )
{
	if ( $message )                 # .. done with cmd line params?
	{
		$message .= " $_";
	}
	elsif ( m/^--help/i )
	{
		print STDERR <<EOF;

usage: $0 [options] message...

  --help        show this help listing
  --dest=M:P    send to machine M / port P (default $destmachine:$destport)

EOF
		exit 1;
	}
	elsif ( m/^--dest=(.+):(\d+)$/i )               # --dest=MACHINE:PORT
	{
		$destmachine = $1;
		$destport    = $2;
	}
	elsif ( m/^-/ )
	{
		die "ERROR: {$_} is an invalid cmdline param\n";
	}
	else
	{
		$message = $_;
	}
}

# ------------------------------------------------------------------------
# SANITY CHECK
#

die "ERROR: missing message to send\n"	if not $message;
die "ERROR: no destination machine\n"   if not $destmachine;
die "ERROR: no destination port\n"      if not $destport;

# ------------------------------------------------------------------------
# bind to the UDP port
#

my $handle = IO::Socket::INET->new(Proto => 'udp')
	or die "ERROR: cannot create UDP socket [$!]\n";

my $ipaddr   = inet_aton($destmachine);
my $portaddr = sockaddr_in( $destport, $ipaddr );

die "bad IP address" if not $ipaddr;
die "bad port addr" if not $portaddr;

$handle->send($message, 0, $portaddr) or die "ERROR: cannot send";
