如何保护在主机中打开的端口?

时间:2013-11-08 17:05:37

标签: perl

我们的目标是从远程(中央)服务器上/下监控服务器。为此,我们使用下面的(Client_port.pl)脚本在目标主机中打开一个端口。在服务器上,我们有其他脚本来监控此端口,以确保服务器已启动且端口已启动。两台服务器都不在同一网络上,因此我们需要打开全球客户端端口。我们如何确保客户端端口免受安全漏洞和其他可能的有害问题的影响?

Client_port.pl

use IO::Socket::INET;

$server = IO::Socket::INET->new(LocalPort => $server_port,
                                Type      => SOCK_STREAM,
                                Reuse     => 1,
                                Listen    => 10 )   # or SOMAXCONN
    or die "Couldn't be a tcp server on port $server_port: $!\n";

while ($client = $server->accept()) {
    # $client is the new connection
}

close($server);

Server_port_listener.pl

use strict;
use Socket;

# set time until connection attempt times out
my $timeout = 3;

if ($#ARGV != 1) {
  print "usage: is_tcp_port_listening hostname portnumber\n";
  exit 2;
}

my $hostname = $ARGV[0];
my $portnumber = $ARGV[1];
my $host = shift || $hostname;
my $port = shift || $portnumber;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);

socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";

eval {
  local $SIG{ALRM} = sub { die "timeout" };
  alarm($timeout);
  connect(SOCKET, $paddr) || error();
  alarm(0);
};

if ($@) {
  close SOCKET || die "close: $!";
  print "$hostname is NOT listening on tcp port $portnumber.\n";
  exit 1;
}
else {
  close SOCKET || die "close: $!";
  print "$hostname is listening on tcp port $portnumber.\n";
  exit 0;
}

0 个答案:

没有答案