#!/usr/bin/perl # # 20020404 Nereu - versao para ePN # use strict; use Socket; use Crypt::TripleDES; use vars qw($line $hostname $command $command_name $timeout $key $answer $x $port $iaddr $paddr $proto $response $commandoutput $errorcode $crypt_on $debug $code $msg $s $M $H $d $m $y $wd $yd $isd); $SIG{'ALRM'} = 'handler'; &GetArgv; &Connect; &Execute; ### sub handler { &MyExit(3,"timeout(${timeout}s)!"); } ### sub write_trace { $msg = shift; ($s,$M,$H,$d,$m,$y,$wd,$yd,$isd)=localtime(time); $y+=1900; $m+=1; printf(TRACE "%04d-%02d-%02d:%02d:%02d:%02d | %05d | %s\n",$y,$m,$d,$H,$M,$s,$$,$msg); } ### sub MyExit { $code = shift; $msg = shift; if ( $debug ) { write_trace($msg); } print "check_nclient: $msg\n"; exit($code); } ### sub GetArgv { my $i; my $j; $hostname=""; $command=""; $timeout = 80; $port = 999; $crypt_on = 1; $debug = 0; $key = "Eh segredo! Shhh!"; for($i=0;$i<=$#ARGV;$i++) { if ( $ARGV[$i] eq "-h" ) { $hostname=$ARGV[++$i]; } elsif ( $ARGV[$i] eq "-C" ) { if ( $ARGV[++$i] eq "off" ) { $crypt_on=0; } } elsif ( $ARGV[$i] eq "-t" ) { $timeout=$ARGV[++$i]; } elsif ( $ARGV[$i] eq "-p" ) { $port=$ARGV[++$i]; } elsif ( $ARGV[$i] eq "-d" ) { $debug=1; } elsif ( $ARGV[$i] eq "-c" ) { $command_name = $ARGV[++$i]; $command_name =~ s/ //g; for($j=$i;$j<=$#ARGV;$j++) { $command .= "$ARGV[$j] "; } last; } } if ( $debug ) { open(TRACE,">>/usr/local/netsaint/plugins/trace/${hostname}_${command_name}.trc") || &MyExit(3,"I canīt write trace file(${hostname}_${command_name}.trc)!"); write_trace("-- BEGIN -- check_nclient"); } if ( $hostname eq "" || $command eq "" ) { &MyExit(3,"Sintaxe error: check_nclient <-h hostname> [-p ] [-t ] [-C[rypt] ] <-c command>"); exit(3); } } ### sub Connect { if ( $debug ) { write_trace("Connect begin"); } if ( $debug ) { write_trace("getservbyname"); } if ($port =~ /\D/) { $port = getservbyname($port, 'tcp'); } &MyExit(3,"Cannot get port($port)") unless $port; if ( $debug ) { write_trace("inet_atom(gethostbyname etc) sockaddr_in"); } $iaddr = inet_aton($hostname) or &MyExit(3,"No Host \"$hostname\""); $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) or &MyExit(3,"Socket failed: \"$!\""); if ( $debug ) { write_trace("connect"); } connect(SOCK, $paddr) or &MyExit(3,"Connect failed: \"$!\""); select(SOCK); $| = 1; select(STDOUT); $| = 1; if ( $debug ) { write_trace("Connect end"); } } ### sub Execute { my $cipher; $response=""; if ( $debug ) { write_trace("Execute begin"); } if ( $crypt_on ) { $cipher = new Crypt::TripleDES; } alarm($timeout); for ($x = 0; $answer = ; $x++) { if ($x == 0) { if ( $debug ) { write_trace("get banner"); } unless ($answer =~ /nclient/) { die("This is not a nclient! (Banner was wrong)\r\n"); } if ( $debug ) { write_trace("send command"); } if ( $crypt_on ) { $command = unpack("H*", $cipher->encrypt3($command, $key)); } print(SOCK $command . "\r\n"); } elsif ($x == 1) { if ( $debug ) { write_trace("reading answer"); } $answer =~ s/\r\n$//; if ( $crypt_on ) { $response = $cipher->decrypt3(pack("H*", $answer), $key); print SOCK unpack("H*", $cipher->encrypt3("exit", $key)) ."\r\n"; } else { if ( $debug ) { write_trace("send exit"); } $response=$answer; print(SOCK "exit\r\n"); } } } if ( $response ne "" ) { ($commandoutput, $errorcode) = split(/ \| /, $response); $commandoutput =~ s/^OP: //; $errorcode =~ s/^RC: //; $errorcode = $errorcode >> 8; if ( $debug ) { write_trace("Execute end msg=[$commandoutput] code=[$errorcode]"); write_trace("-- END -- check_nclient"); } print "$commandoutput\n"; exit ( ( $errorcode < 0 || $errorcode > 4 ) ? 3 : $errorcode ); } else { print "no answer from client!\n" ; exit(3); } } ### End