1

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

客户端端口.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);

服务器端口监听器.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;
}
4

0 回答 0