summaryrefslogtreecommitdiffstats
path: root/plugins/t/check_ssh.t
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/t/check_ssh.t')
-rw-r--r--plugins/t/check_ssh.t120
1 files changed, 100 insertions, 20 deletions
diff --git a/plugins/t/check_ssh.t b/plugins/t/check_ssh.t
index a5cd23c..7df6265 100644
--- a/plugins/t/check_ssh.t
+++ b/plugins/t/check_ssh.t
@@ -8,34 +8,114 @@ use strict;
8use Test::More; 8use Test::More;
9use NPTest; 9use NPTest;
10 10
11my $res;
12
11# Required parameters 13# Required parameters
12my $ssh_host = getTestParameter("NP_SSH_HOST", "A host providing SSH service", "localhost"); 14my $ssh_host = getTestParameter("NP_SSH_HOST",
13my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1" ); 15 "A host providing SSH service",
14my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost" ); 16 "localhost");
17
18my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE",
19 "The hostname of system not responsive to network requests",
20 "10.0.0.1" );
21
22my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID",
23 "An invalid (not known to DNS) hostname",
24 "nosuchhost" );
25
26
27plan tests => 14 + 6;
28
29SKIP: {
30 skip "SSH_HOST must be defined", 6 unless $ssh_host;
31 my $result = NPTest->testCmd(
32 "./check_ssh -H $ssh_host"
33 );
34 cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
35 like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)");
36
37
38 $result = NPTest->testCmd(
39 "./check_ssh -H $host_nonresponsive -t 2"
40 );
41 cmp_ok($result->return_code, '==', 2, "Exit with return code 0 (OK)");
42 like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)");
43
44
45
46 $result = NPTest->testCmd(
47 "./check_ssh -H $hostname_invalid -t 2"
48 );
49 cmp_ok($result->return_code, '==', 3, "Exit with return code 0 (OK)");
50 like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)");
51
15 52
53}
54SKIP: {
16 55
17plan skip_all => "SSH_HOST must be defined" unless $ssh_host; 56 skip "No netcat available", 14 unless (system("which nc > /dev/null") == 0);
18plan tests => 6;
19 57
58 my $nc_flags = "-l 5003 -i 1";
59 #A valid protocol version control string has the form
60 # SSH-protoversion-softwareversion SP comments CR LF
61 #
62 # where `comments` is optional, protoversion is the SSH protocol version and
63 # softwareversion is an arbitrary string representing the server software version
64 open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1' | nc ${nc_flags}|");
65 sleep 1;
66 $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" );
67 cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string");
68 like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK");
69 close NC;
20 70
21my $result = NPTest->testCmd( 71 open(NC, "echo 'SSH-2.0-3.2.9.1' | nc ${nc_flags}|");
22 "./check_ssh -H $ssh_host" 72 sleep 1;
23 ); 73 $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" );
24cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); 74 cmp_ok( $res->return_code, "==", 0, "Got SSH protocol version control string with non-alpha softwareversion string");
25like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)"); 75 like( $res->output, '/^SSH OK - 3.2.9.1 \(protocol 2.0\)/', "Output OK for non-alpha softwareversion string");
76 close NC;
26 77
78 open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1 this is a comment' | nc ${nc_flags} |");
79 sleep 1;
80 $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" );
81 cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string, and parsed comment appropriately");
82 like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK");
83 close NC;
27 84
28$result = NPTest->testCmd( 85 open(NC, "echo 'SSH-' | nc ${nc_flags}|");
29 "./check_ssh -H $host_nonresponsive -t 2" 86 sleep 1;
30 ); 87 $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" );
31cmp_ok($result->return_code, '==', 2, "Exit with return code 0 (OK)"); 88 cmp_ok( $res->return_code, '==', 2, "Got invalid SSH protocol version control string");
32like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)"); 89 like( $res->output, '/^SSH CRITICAL/', "Output OK");
90 close NC;
33 91
92 open(NC, "echo '' | nc ${nc_flags}|");
93 sleep 1;
94 $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" );
95 cmp_ok( $res->return_code, '==', 2, "No version control string received");
96 like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK");
97 close NC;
34 98
99 open(NC, "echo 'Not a version control string' | nc ${nc_flags}|");
100 sleep 1;
101 $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" );
102 cmp_ok( $res->return_code, '==', 2, "No version control string received");
103 like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK");
104 close NC;
35 105
36$result = NPTest->testCmd(
37 "./check_ssh -H $hostname_invalid -t 2"
38 );
39cmp_ok($result->return_code, '==', 3, "Exit with return code 0 (OK)");
40like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)");
41 106
107 #RFC 4253 permits servers to send any number of data lines prior to sending the protocol version control string
108 open(NC, "{ echo 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; sleep 1;
109 echo 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'; sleep 1;
110 echo 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'; sleep 1;
111 echo 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD'; sleep 1;
112 printf 'EEEEEEEEEEEEEEEEEE'; sleep 1;
113 printf 'EEEEEEEEEEEEEEEEEE\n'; sleep 1;
114 echo 'Some\nPrepended\nData\nLines\n'; sleep 1;
115 echo 'SSH-2.0-nagiosplug.ssh.0.2';} | nc ${nc_flags}|");
116 sleep 1;
117 $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" );
118 cmp_ok( $res->return_code, '==', 0, "Got delayed SSH protocol version control string");
119 like( $res->output, '/^SSH OK - nagiosplug.ssh.0.2 \(protocol 2.0\)/', "Output OK");
120 close NC;
121}