From ecf3f468905dc2b8e8470eb5288ce8b9f845c26f Mon Sep 17 00:00:00 2001 From: Anton Lofgren Date: Thu, 15 May 2014 14:48:26 +0200 Subject: check_ssh: Handle non-alpha software versions This patch fixes a bug where we would reject version control strings that do not contain letters, because the assumption is made that they always do. This is not required by the RFC however, and there exist implementations that do not contain letters. I've also added a few references to the RFC to make the process of parsing the control string more apparent. This fixes op5#8716 (https://bugs.op5.com/view.php?id=8716) Signed-off-by: Anton Lofgren --- plugins/t/check_ssh.t | 76 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 31 deletions(-) (limited to 'plugins/t') diff --git a/plugins/t/check_ssh.t b/plugins/t/check_ssh.t index 3e1824df..7df62651 100644 --- a/plugins/t/check_ssh.t +++ b/plugins/t/check_ssh.t @@ -8,10 +8,13 @@ use strict; use Test::More; use NPTest; +my $res; + # 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" ); @@ -20,13 +23,37 @@ my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost" ); -my $res; +plan tests => 14 + 6; -plan tests => 18; SKIP: { + skip "SSH_HOST must be defined", 6 unless $ssh_host; + my $result = NPTest->testCmd( + "./check_ssh -H $ssh_host" + ); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)"); - skip "No netcat available", 12 unless (system("which nc > /dev/null") == 0); + + $result = NPTest->testCmd( + "./check_ssh -H $host_nonresponsive -t 2" + ); + cmp_ok($result->return_code, '==', 2, "Exit with return code 0 (OK)"); + like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)"); + + + + $result = NPTest->testCmd( + "./check_ssh -H $hostname_invalid -t 2" + ); + cmp_ok($result->return_code, '==', 3, "Exit with return code 0 (OK)"); + 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); my $nc_flags = "-l 5003 -i 1"; #A valid protocol version control string has the form @@ -41,6 +68,13 @@ SKIP: { like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); close NC; + open(NC, "echo 'SSH-2.0-3.2.9.1' | nc ${nc_flags}|"); + sleep 1; + $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); + cmp_ok( $res->return_code, "==", 0, "Got SSH protocol version control string with non-alpha softwareversion string"); + like( $res->output, '/^SSH OK - 3.2.9.1 \(protocol 2.0\)/', "Output OK for non-alpha softwareversion string"); + close NC; + open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1 this is a comment' | nc ${nc_flags} |"); sleep 1; $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ); @@ -48,7 +82,6 @@ SKIP: { like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); close NC; - open(NC, "echo 'SSH-' | nc ${nc_flags}|"); sleep 1; $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); @@ -72,36 +105,17 @@ SKIP: { #RFC 4253 permits servers to send any number of data lines prior to sending the protocol version control string - open(NC, "echo 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n - BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n - CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n - DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\n - Some\nPrepended\nData\nLines\nSSH-2.0-nagiosplug.ssh.0.2' | nc ${nc_flags}|"); + open(NC, "{ echo 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; sleep 1; + echo 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'; sleep 1; + echo 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'; sleep 1; + echo 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD'; sleep 1; + printf 'EEEEEEEEEEEEEEEEEE'; sleep 1; + printf 'EEEEEEEEEEEEEEEEEE\n'; sleep 1; + echo 'Some\nPrepended\nData\nLines\n'; sleep 1; + echo 'SSH-2.0-nagiosplug.ssh.0.2';} | nc ${nc_flags}|"); sleep 1; $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); cmp_ok( $res->return_code, '==', 0, "Got delayed SSH protocol version control string"); like( $res->output, '/^SSH OK - nagiosplug.ssh.0.2 \(protocol 2.0\)/', "Output OK"); close NC; } - -SKIP { - skip "SSH_HOST must be defined", 6 unless $ssh_host; - $res = NPTest->testCmd( - "./check_ssh -H $ssh_host" - ); - cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); - like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)"); - - $res = NPTest->testCmd( - "./check_ssh -H $host_nonresponsive -t 2" - ); - cmp_ok($result->return_code, '==', 2, "Exit with return code 2 (CRITICAL)"); - like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)"); - - $res = NPTest->testCmd( - "./check_ssh -H $hostname_invalid -t 2" - ); - 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)"); - -} -- cgit v1.2.3-74-g34f1