#! /usr/bin/perl -w use strict; use IO::Socket::INET; use IO::Select; use POSIX qw(EWOULDBLOCK EINTR); my $timeout = 180; my ($host, $port, $string) = @ARGV; my $too_late = time()+$timeout; my $socket = IO::Socket::INET->new(PeerAddr => "$host:$port", Blocking => 0) || die "Could not connect to $host:$port: $!\n"; # Write (also handles connect, since being connected is a writability event) $SIG{PIPE} = "IGNORE"; my $command = "EDMSFT|$string|\@\n"; my $select = IO::Select->new($socket); while ($command ne "") { my $left = $too_late - time(); if ($left <= 0) { print "TIMEDOUT\n"; exit; } next unless $select->can_write($left); if (defined(my $rc = syswrite($socket, $command))) { substr($command, 0, $rc, ""); } else { next if $! == EWOULDBLOCK || $! == EINTR; die "Error writing to $host:$port: $!\n"; } } # Read my $text = ""; while (1) { my $left = $too_late - time(); if ($left <= 0) { print "TIMEDOUT\n"; exit; } next unless $select->can_read($left); my $rc = sysread($socket, $text, 4096, length $text); if (!$rc) { last if defined $rc; # EOF next if $! == EWOULDBLOCK || $! == EINTR; die "Error reading from $host:$port: $!\n"; } } $text =~ /\Q$string\E/ || die "Unexpected answer from $host:$port: $text"; print "GOOD\n";