# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # FILE: sendlog.prl # USAGE: perl sendlog.prl (ipaddr) "(message)" (severity) (priority) # A simple Perl program demonstrating one way to send a syslog message # to a syslog receiver. The program can easily be modified by Perl # programmers for specific applications. # The user must specify an IP address as the first argument, followed # by the message content. The severity and facility arguments follow, # and are integer numbers, where the $severity ranges from 0=emergency # to 7=debug, and the $facility ranges from 0=kernel to 23=local7. # EXAMPLE: perl sendlog.prl 127.0.0.1 "Test message" 6 23 # The above example sends "Test Message" to the local host, with "info" # severity and the "local7" facility. # This code has been placed in the public domain by: # CorreLog, Inc. # http://www.CorreLog.com # Rights to use and modify this program for any reason and purpose is # hereby granted to any and all parties. CorreLog, Inc. accepts # no liabilities and makes no claims about any use of this program, # or any adaptation of this program, whatsoever. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # use Socket; $ipaddr = $ARGV[0]; $message = $ARGV[1]; $severity = $ARGV[2]; $facility = $ARGV[3]; if (! defined($severity)) { $severity = 7; # Debug } if (! defined($facility)) { $facility = 1; # User } # Create a UDP socket, and a UDP connection: $proto = getprotobyname('udp') or die "No UDP protocol available. Verify network configuration:"; socket (S, AF_INET, SOCK_DGRAM, $proto ) or die "Cannot create socket. Verify network is properly configured:"; # This will fail if the address or port number is not specified. $n_addr = inet_aton($ipaddr) or die "Improper IP address argument specified:"; $s_addr = sockaddr_in(514, $n_addr); # This will fail if the address or port number is invalid. connect (S, $s_addr ) or die "Connect failed. Verify IP address argument:"; $priority = $facility << 3; $priority = $priority | $severity; print S "<$priority>$message"; close(S); # End of File