#!/usr/bin/perl -w use strict; use IO::Handle; use Socket; use Sys::Hostname; use Getopt::Std; STDERR->autoflush(1); STDOUT->autoflush(1); # i: NetID; t: timeout use vars qw($opt_d $opt_i $opt_h $opt_H $opt_p $opt_t); getopts("dhH:p:i:t:"); our $Usage = < [-p ] [-t ] -i NetID EOUSAGE (print $Usage and exit 3) if $opt_h; if (!defined($opt_H) or !defined($opt_i)) { print $Usage; exit 3; } our $debug = $opt_d; our $NetID = $opt_i; our $ThatHost = $opt_H; our $ThatPort = defined($opt_p) ? $opt_p : 80; our $TIMEOUT = defined($opt_t) ? $opt_t : 10; our @Result; our $QRY = < $NetID End_Of_Query our $LengthOfQRY = length($QRY); our $ThisHost = hostname(); our ($AfInet, $SockStream, $SockAddr) = (AF_INET, SOCK_STREAM, 'S n a4 x8'); our ($Name, $Aliases, $Proto) = getprotobyname('tcp'); our ($Type, $Length) = (undef, undef); our @ThisAddr = (); our @ThatAddr = (); ($Name, $Aliases, $Type, $Length, @ThisAddr) = gethostbyname($ThisHost); ($Name, $Aliases, $Type, $Length, @ThatAddr) = gethostbyname($ThatHost); our $This = pack ($SockAddr, $AfInet, 0, $ThisAddr[0]); our $That = pack ($SockAddr, $AfInet, $ThatPort, $ThatAddr[0]); if (socket(SOCKET, $AfInet, $SockStream, $Proto)) { print STDOUT "TCP socket created\n" if $debug; } else { print "CRITICAL: Cannot connect to $ThatHost:$ThatPort\n"; exit 2; } our $Error = ''; $SIG{ALRM} = sub { print "CRITICAL: Timeout connecting to $ThatHost:$ThatPort\n"; exit 2; }; alarm($TIMEOUT) if $TIMEOUT > 0; if (connect(SOCKET, $That)) { printf STDOUT "Connected to port %u of %s\n", $ThatPort, $ThatHost if $debug; } else { print "CRITICAL: Cannot connect to $ThatHost:$ThatPort\n"; exit 2; } alarm(0); $SIG{ALRM} = 'DEFAULT'; SOCKET->autoflush(1); our $HTTPandQRY = < 0; print STDOUT $HTTPandQRY if $debug; print SOCKET $HTTPandQRY; my $clength = 0; while () { print STDOUT if $debug; chop;chop; push @Result, $_; if (m/Content-Length: (\d+)/) { $clength = $1; print "content length $clength\n" if $debug; } last if $_ eq ''; } print "Got header\n" if $debug; if ($Result[0] !~ /HTTP\/.*200 OK/) { print "CRITICAL: NETID query returned $Result[0]\n"; exit 2; } my $buf; read(SOCKET, $buf, $clength); print "$buf\n" if $debug; alarm(0); $SIG{ALRM} = 'DEFAULT'; close SOCKET; if ($buf =~ /(true|false)<\/IsNetIDAvailableResult>/) { print "OK: NetID query succeeded ($NetID: $1)\n"; exit 0; } print "CRITICAL: NetID query failed\n"; exit 2;