#!/usr/local/bin/perl
#
# Use Net::Telnet to connect to a list of hosts.
#
# -p port        connect to 'port' (defaults to 23)
# -t secs        set timeout to 'secs' (defaults to 10)
# -l login	 string to look for (default 'ogin')
#
# Arguments are "-h host"
#
# Jim Trocki, trockij@transmeta.com
#
# $Id: telnet.plugin,v 1.2.2.1 2002/07/22 15:21:49 paul-cs Exp $
#
#    Copyright (C) 1998, Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Modified by Paul van Tilburg. Speedup needed for Checkservice...
# (removed multiple host support and changed default values)

use Net::Telnet;
use Getopt::Std;

getopts ("l:p:t:h:");

$TIMEOUT  = $opt_t || 5;
$Port     = $opt_p || 23;
$Host	  = $opt_h || "localhost";
$Login    = $opt_l || "ogin";

exit &tryTELNET($Host, $Port, $Login);

sub tryTELNET {
  my ($Server, $Port, $Login) = @_;
  my $ServerOK = 1;
  
  eval {
    local $SIG{ALRM} = sub { die "Timeout Alarm" };
    alarm $TIMEOUT;
    
    my $telnet = new Net::Telnet->new();
    $telnet->open(Host => $Server, Port => $Port);
		 
    my $ok = $telnet->waitfor(String => $Login);
    if (defined($ok)) { $ServerOK = 0; }

    $telnet->close();
  };

  if ($EVAL_ERROR and ($EVAL_ERROR =~ /Timeout Alarm/)) {
    return 2;
  }

  return $ServerOK;
}


syntax highlighted by Code2HTML, v. 0.9.1