#!/usr/bin/perl -w

use strict;

use Net::SSLeay qw(sslcat);

my($opt_p,$opt_s,$opt_e);
my($socket, $response);
my (@detail, @failures);

use Getopt::Long;
Getopt::Long::Configure("bundling");

GetOptions("p=i" => \$opt_p, "port=i" => \$opt_p,
	   "s=s" => \$opt_s, "send=s" => \$opt_s,
           "e=s" => \$opt_e, "expect=s" => \$opt_e,);

foreach my $host (@ARGV) {
	if ($opt_s) { $response = sslcat($host, $opt_p, eval(qq/"$opt_s"/)); }
	else { $response = sslcat($host, $opt_p, "\n"); }
	if ($opt_e) {
	    if (!($response =~ /$opt_e/m)) {
			push @failures, $host;
			push @detail, "$host: Didn't match $opt_e\n";
	    } else {
			push @detail, "$host: $&\n";
	    }
	} else {
		if ($response) {
			chomp $response;
			push @detail, "$host: $response\n";
	    } else {
			push @failures, $host;
			push @detail, "$host: $!\n";
	    }
	}
}

print join(' ',@failures),"\n";
print join('',@detail);
exit 1 if @failures;
exit 0;
