1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#! /usr/bin/perl -w -I ..
#
# check_ssh tests
#
#
use strict;
use warnings;
use Test::More;
use NPTest;
use JSON;
# Required parameters
my $ssh_host = getTestParameter("NP_SSH_HOST",
"A host providing SSH service",
"localhost");
my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE",
"The hostname of system not responsive to network requests",
"10.0.0.1" );
my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID",
"An invalid (not known to DNS) hostname",
"nosuchhost" );
my $outputFormat = '--output-format mp-test-json';
plan tests => 24;
my $output;
my $result;
SKIP: {
skip "SSH_HOST must be defined", 6 unless $ssh_host;
my $result = NPTest->testCmd(
"./check_ssh -H $ssh_host" ." ". $outputFormat
);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "OK", "State was correct");
$result = NPTest->testCmd(
"./check_ssh -H $host_nonresponsive -t 2" ." ". $outputFormat
);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "CRITICAL", "State was correct");
$result = NPTest->testCmd(
"./check_ssh -H $hostname_invalid -t 2" ." ". $outputFormat
);
cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)");
like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)");
}
SKIP: {
skip "No netcat available", 14 unless (system("which nc > /dev/null") == 0);
# netcat on linux (on debian) will just keep the socket open if not advised otherwise
# therefore we add -q to close it after two seconds after receiving the EOF from input
my $nc_flags = "-l 5003 -N";
#A valid protocol version control string has the form
# SSH-protoversion-softwareversion SP comments CR LF
#
# where `comments` is optional, protoversion is the SSH protocol version and
# softwareversion is an arbitrary string representing the server software version
my $found_version = 0;
open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1' | nc ${nc_flags}|");
sleep 0.1;
$result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "OK", "State was correct");
# looking for the version
for my $subcheck (@{$output->{'checks'}}) {
if ($subcheck->{'output'} =~ /.*nagiosplug.ssh.0.1 \(protocol version: 2.0\).*/ ){
$found_version = 1;
}
}
cmp_ok($found_version, '==', 1, "Output OK");
close NC;
open(NC, "echo 'SSH-2.0-3.2.9.1' | nc ${nc_flags}|");
sleep 0.1;
$result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "OK", "State was correct");
$found_version = 0;
for my $subcheck (@{$output->{'checks'}}) {
if ($subcheck->{'output'} =~ /3.2.9.1 \(protocol version: 2.0\)/ ){
$found_version = 1;
}
}
cmp_ok($found_version, '==', 1, "Output OK");
close NC;
open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1 this is a comment' | nc ${nc_flags} |");
sleep 0.1;
$result = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ." ". $outputFormat);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "OK", "State was correct");
# looking for the version
$found_version = 0;
for my $subcheck (@{$output->{'checks'}}) {
if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.1 \(protocol version: 2.0\)/ ){
$found_version = 1;
}
}
cmp_ok($found_version, '==', 1, "Output OK");
close NC;
open(NC, "echo 'SSH-' | nc ${nc_flags}|");
sleep 0.1;
$result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "CRITICAL", "Got invalid SSH protocol version control string");
close NC;
open(NC, "echo '' | nc ${nc_flags}|");
sleep 0.1;
$result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "CRITICAL", "No version control string received");
close NC;
open(NC, "echo 'Not a version control string' | nc ${nc_flags}|");
sleep 0.1;
$result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "CRITICAL", "No version control string received");
close NC;
#RFC 4253 permits servers to send any number of data lines prior to sending the protocol version control string
open(NC, "{ echo 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; sleep 0.5;
echo 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'; sleep 0.5;
echo 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'; sleep 0.2;
echo 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD'; sleep 0.3;
printf 'EEEEEEEEEEEEEEEEEE'; sleep 0.2;
printf 'EEEEEEEEEEEEEEEEEE\n'; sleep 0.2;
echo 'Some\nPrepended\nData\nLines\n'; sleep 0.2;
echo 'SSH-2.0-nagiosplug.ssh.0.2';} | nc ${nc_flags}|");
sleep 0.1;
$result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat);
cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
$output = decode_json($result->output);
is($output->{'state'}, "OK", "State was correct");
# looking for the version
$found_version = 0;
for my $subcheck (@{$output->{'checks'}}) {
if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.2 \(protocol version: 2.0\)/ ){
$found_version = 1;
}
}
cmp_ok($found_version, '==', 1, "Output OK");
close NC;
}
|