# mocksyslogclient.pl. # Simple test of mocksyslogserver.pl # Example: perl mocksyslogclient.pl localhost 9949 use strict; use warnings; use Socket; use IO::Socket::INET; use Getopt::Std (); # Plain socket version sub send_tcp_messages { my $host = shift; my $port = shift; my $delaysecs = shift; my @messages = @_; $host = inet_aton($host) or die "error: unknown host: $!"; socket( my $server, PF_INET, SOCK_STREAM, getprotobyname('tcp') ) or die "error: socket: $!"; my $dest_addr = sockaddr_in( $port, $host ); connect( $server, $dest_addr ) or die "error: connect: $!"; binmode($server); $server->autoflush(); my $nmessages = @messages; print "Send $nmessages messages\n"; my $idx = 0; for my $mess (@messages) { ++$idx; print "$idx: send message\n"; my $nmess = length($mess); my $nsent = send( $server, $mess, 0, $dest_addr ) or die "error: send '$mess': $!"; $nsent == $nmess or die "error: nsent ($nsent != $nmess)"; print "$idx: sent message ok\n"; if ( $idx < $nmessages && $delaysecs ) { print "$idx: delay $delaysecs secs\n"; sleep $delaysecs; } } close($server) or die "error: close: $!"; } # IO::Socket::INET version sub send_tcp_messages_inet { my $host = shift; my $port = shift; my $delaysecs = shift; my @messages = @_; # Creating object IO::Socket::INET internally creates socket, # binds, and connects to TCP server running on port. my $server = IO::Socket::INET->new( PeerHost => $host, PeerPort => $port, Proto => 'tcp', ) or die "error: socket new: $@\n"; my $nmessages = @messages; print "Send $nmessages messages\n"; my $idx = 0; for my $mess (@messages) { ++$idx; print "$idx: send message\n"; my $nmess = length($mess); my $nsent = $server->send($mess) or die "error: send '$mess': $!"; $nsent == $nmess or die "error: nsent ($nsent != $nmess)"; print "$idx: sent message ok\n"; if ( $idx < $nmessages && $delaysecs ) { print "$idx: delay $delaysecs secs\n"; sleep $delaysecs; } } $server->close() or die "error: close: $!"; } sub usage { print <<'DARLING_FASCIST_BULLY_BOY'; A mock syslog client. usage: mocksyslogclient -h host -p port [-d delaysecs] message... Examples: mocksyslogclient -h localhost -p 9949 "hello" Send "hello" message mocksyslogclient -h localhost -p 9949 "hello" "there" Send two messages mocksyslogclient -h localhost -p 9949 SERVER_PLEASE_QUIT Ask server to quit DARLING_FASCIST_BULLY_BOY exit 1; } *STDOUT->autoflush(); @ARGV or usage(); my %option = (); Getopt::Std::getopts( "h:p:d:", \%option ) or usage(); my $host; if ( $option{h} ) { $host = $option{h}; } $host or die "error: No -h host specified\n"; my $port; if ( $option{p} ) { $port = $option{p}; $port =~ /^\d+$/ or die "error: invalid -p: '$port'\n"; } my $delaysecs = 0; if ( $option{d} ) { $delaysecs = $option{d}; $delaysecs =~ /^\d+$/ or die "error: invalid -d: '$delaysecs'\n"; } @ARGV or usage(); my @messages = @ARGV; @messages or die "error: No messages specified\n"; print "host='$host' port='$port' delaysecs='$delaysecs'\n"; # send_tcp_messages_inet( $host, $port, $delaysecs, @messages ); send_tcp_messages( $host, $port, $delaysecs, @messages );