端口健康状况检测程序
目前常见的系统监控软件如nagios zabbix对端口的检测一般限于能正常建立TCP连接,完成3次握手;有时服务异常会导致端口开放,但不能正常提供服务;此脚本能很好的解决这个问题
原理是访问端口,看输出,
或者访问端口后,执行命令,看输出是否正常,
如发现什么问题,请论坛中和我联系! 转载请注明出自 linuxeden.com
使用方法:
./portcheck.pl -c my.conf -o my.err
my.err做为输出文件,只记录出错的IP
ort。
脚本文件:
复制内容到剪贴板
代码:
#!/usr/bin/perl
#
# port check -- A simple perl-script for checking ports on remote/local hosts.
# 2006-01-06 by Roc.Ken
################################################
use warnings;
use strict;
use IO::File;
use Getopt::Std;
use IO::Socket qw /CRLF/;
my @config;
my $hostname;
my $port;
my $protocol;
my $input_value;
my $server_return;
my $return_value;
my $good;
my %options;
getopts('h:c:o', \%options);
if ($options{h} or !$options{c}) {
die <<END
$0 notice:
USAGE: $0 -c CONFIG.conf -o ERROR.err
-c to specify a configration file,this is a MUST.
-o to specify a output file to log errors.
END
}
$options{o} ||="/dev/null";
open(ERR,">",$options{o}) or die "Cannot not open $options{o}";
my $filehndl = new IO::File;
if ($filehndl->open("< $options{c}") || die "Configfile $options{c} not found!")
{
@config = <$filehndl>;
}
print "*--ADDRESS----PROTOCOL--PORT-----OK--*\n";
foreach my $i (@config)
{
$good = 0;
next if $hostname =~ /^#/;
($hostname, $port, $protocol, $input_value, $return_value) = split(/:/, $i);
chomp($input_value);
chomp($return_value);
my $remote = IO::Socket::INET->new(
Proto => $protocol,
PeerAddr => $hostname,
PeerPort => $port,
) or $good = 1;
if ( $good )
{
print substr($hostname, 0, 15) . "\t$protocol\t" . substr($port, 0, 10) . "\t";
print " NO" . "\n";
print ERR "$hostname:$port unreachable\n";
next;
}
local $SIG{CHLD}=sub {return 0};
my $child = fork();
die unless defined $child;
if ($child)
{
print $remote $input_value,CRLF,CRLF;
sleep;
}else{
$server_return=<$remote>;
if ( $server_return =~ /$return_value/ )
{
print substr($hostname, 0, 15) . "\t$protocol\t " . substr($port, 0, 10) . "\t";
print "YES\n";
} else {
($server_return, undef) = split(/\r/, $server_return);
print substr($hostname, 0, 15) . "\t$protocol\t ". substr($port, 0, 10) . "\t";
print "N/A\n";
print ERR "please check $hostname:$port\n";
}
exit 0;
}
close($remote);
}
$filehndl->close;
close(ERR);
复制内容到剪贴板
代码:
########################################################
# Syntax of the configration:
#
# IP:PORT:Protocol:INPUT:OUTPUT
#
# Details:
# IP: ip-address or hostname of the dest hosts
# PORT: port to monitor
# PROTOCOL: tcp or udp
# INPUT: after connected,the command to execute; or keep blank.
# eg: 202.108.209.77:22:tcp::SSH
# OUTPUT: after connected(or after executing the command if supplied),
# the pattern in the FIRST line of the outputs to match.
#
########################################################
202.108.209.77:80:tcp:get:HTTP
202.108.209.177:80:tcp:get:HTTP
202.108.209.77:22:tcp::SSH
202.108.209.177:22:tcp::SSH