This commit is contained in:
garnier-quentin 2020-12-17 11:44:28 +01:00
parent 42d481edd0
commit bd9345d6d8
2 changed files with 30 additions and 19 deletions

View File

@ -32,12 +32,12 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'hostname:s' => { name => 'hostname' },
"hostname:s" => { name => 'hostname' }, 'port:s' => { name => 'port' },
"port:s" => { name => 'port', }, 'timeout:s' => { name => 'timeout', default => '3' }
"timeout:s" => { name => 'timeout', default => '3' }, });
});
return $self; return $self;
} }
@ -57,10 +57,16 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Need to specify '--hostname' option"); $self->{output}->add_option_msg(short_msg => "Need to specify '--hostname' option");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (!defined($self->{option_results}->{port})) {
if (!defined($self->{option_results}->{port}) || $self->{option_results}->{port} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify '--port' option"); $self->{output}->add_option_msg(short_msg => "Need to specify '--port' option");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->{option_results}->{port} = $self->{option_results}->{port} =~ /(\d+)/ ? $1 : -1;
if ($self->{option_results}->{port} < 0 || $self->{option_results}->{port} > 65535) {
$self->{output}->add_option_msg(short_msg => 'Illegal port number (allowed range: 1-65535)');
$self->{output}->option_exit();
}
} }
sub run { sub run {
@ -74,14 +80,15 @@ sub run {
my $read_set = new IO::Select(); my $read_set = new IO::Select();
$read_set->add($icmp_sock); $read_set->add($icmp_sock);
my $sock = IO::Socket::INET->new(PeerAddr => $self->{option_results}->{hostname}, my $sock = IO::Socket::INET->new(
PeerPort => $self->{option_results}->{port}, PeerAddr => $self->{option_results}->{hostname},
Proto => 'udp', PeerPort => $self->{option_results}->{port},
); Proto => 'udp',
);
$sock->send("Hello"); $sock->send("Hello");
close($sock); close($sock);
my ($new_readable) = IO::Select->select($read_set, undef, undef, $self->{option_results}->{timeout}); my ($new_readable) = IO::Select->select($read_set, undef, undef, $self->{option_results}->{timeout});
my $icmp_arrived = 0; my $icmp_arrived = 0;
foreach $sock (@$new_readable) { foreach $sock (@$new_readable) {
@ -93,11 +100,15 @@ sub run {
close($icmp_sock); close($icmp_sock);
if ($icmp_arrived == 1) { if ($icmp_arrived == 1) {
$self->{output}->output_add(severity => 'CRITICAL', $self->{output}->output_add(
short_msg => sprintf("Connection failed on port %s", $self->{option_results}->{port})); severity => 'CRITICAL',
short_msg => sprintf("Connection failed on port %s", $self->{option_results}->{port})
);
} else { } else {
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => sprintf("Connection success on port %s", $self->{option_results}->{port})); severity => 'OK',
short_msg => sprintf("Connection success on port %s", $self->{option_results}->{port})
);
} }
$self->{output}->display(); $self->{output}->display();

View File

@ -30,9 +30,9 @@ sub new {
bless $self, $class; bless $self, $class;
$self->{version} = '0.1'; $self->{version} = '0.1';
%{$self->{modes}} = ( $self->{modes} = {
'connection' => 'apps::protocols::udp::mode::connection', 'connection' => 'apps::protocols::udp::mode::connection'
); };
return $self; return $self;
} }