#!/usr/bin/perl 

# Script-name: mtachecker.pl
#
# A variation on hostcheck.pl.  Actually a mass port checker.
# Will take a list (newline seperated) of IPs and connect to 
# them and spit out the header it gets back on connect. 
#
# There used to be a whole wack of Mail Transport Agents I'd 
# have to check for this company when an alarm went off.  Our 
# monitoring system at the time would on occasion fire off false
# alarms.  Checking them all was fairly time consuming.  

use Getopt::Std;
use IO::Socket; 

sub usage {

  die print <<EOF;
Usage: mtachecker.pl -f <hostfile> -p <port>
    
-f = hostfile, newline seperated.  Comment out entries with '#'
-p = port, default 25.

EOF
	      
}

getopt("fp");

my $file = $opt_f; 
my $port = $opt_p || "25";

usage unless ($file); 

parse_file($file); 

sub parse_file() { 

  my ($file) = @_; 

  open(FP, $file);

  @list = <FP>; 

  foreach $host (@list) { 
    chomp($host);
    unless ($host =~ /^#/) { connect_mta($host, $port); }
  }
  
}

sub connect_mta() { 

  ($host, $port) = @_; 
  
  my $socket = IO::Socket::INET->new(
				     Timeout => 1,
				     PeerAddr => $host,
				     PeerPort => $port,
				     Proto => "tcp",
				     Type => SOCK_STREAM
				    ) or warn "$host:$port = No connection to $host:$port established -- $@\n";

  if ($socket) {
    print $socket '';
    $answer = <$socket>; 
    print "\n".$host.":$port = ".$answer; 
    close($socket) or warn "Socket to $host:$port not properly terminated.";
  }

}

# That's it. 
