/[nagios-plugins-perl]/trunk/plugins/check_ssh_login.pl
ViewVC logotype

Contents of /trunk/plugins/check_ssh_login.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 168 - (show annotations) (download)
Fri Jan 29 11:05:04 2016 UTC (4 years, 10 months ago) by racvision
File MIME type: text/plain
File size: 3557 byte(s)
add plugin check_ssh_login
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2016 St├ęphane URBANOVSKI - CRT Supervision
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #
19 #
20
21
22 use strict;
23 use warnings;
24
25
26 use POSIX qw(setlocale);
27 use Locale::gettext;
28
29 use Nagios::Plugin ;
30
31 use Net::SSH2;
32
33 my $VERSION = '1.0';
34 my $DEBUG = 0;
35
36 my $TIMEOUT = 9;
37
38 # i18n :
39 setlocale(LC_MESSAGES, '');
40 textdomain('nagios-plugins-perl');
41
42 my $np = Nagios::Plugin->new(
43 version => $VERSION,
44 blurb => _gt('Plugin to check ssh2 login'),
45 usage => "Usage: %s [ -v|--verbose ] -u <url> [-t <timeout>] [ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ]",
46 timeout => $TIMEOUT+1
47 );
48 $np->add_arg (
49 spec => 'debug|d',
50 help => _gt('Debug level'),
51 default => 0,
52 );
53
54
55 $np->add_arg (
56 spec => 'hostname|H=s',
57 help => _gt('Hostname'),
58 required => 1,
59 );
60
61 $np->add_arg (
62 spec => 'user|u=s',
63 help => _gt('User'),
64 required => 1,
65 );
66
67 $np->add_arg (
68 spec => 'password|p=s',
69 help => _gt('Password'),
70 required => 1,
71 );
72
73 $np->add_arg (
74 spec => 'expect|e=s',
75 help => _gt('Expected string'),
76 required => 1,
77 );
78
79 $np->getopts;
80
81 $DEBUG = $np->opts->get('debug');
82
83 my $host = $np->opts->get('hostname');
84 my $user = $np->opts->get('user');
85 my $password = $np->opts->get('password');
86 my $expect = $np->opts->get('expect');
87
88
89 my $ssh2 = Net::SSH2->new();
90
91 if ( ! $ssh2->connect($host) ) {
92 $np->nagios_exit(CRITICAL, "SSH connection failed :".$! );
93 }
94
95 # pass gruicK42
96
97 my $version = $ssh2->version();
98 my ($version2, $vernum, $banner) = $ssh2->version();
99
100 logD ("SSH infos: version=$version2 vernum=$vernum sshbanner=$banner");
101
102 if ( $ssh2->auth_password($user,$password) ) {
103 my $chan = $ssh2->channel();
104
105
106 if ( $chan->shell() ) {
107
108 # check if we can execute something
109 print $chan "/bin/echo WeHaveaShell\n";
110
111 $chan->blocking(0); # don't block, or we'll wait forever
112 my @poll = { 'handle' => $chan, 'events' => ['in'] };
113 $ssh2->poll(2000, \@poll);
114
115 my $found = 0;
116 while ( my $out = <$chan> ) {
117 chomp $out;
118 logD ("out:$out");
119
120 if ( $out =~ /WeHaveaShell/ ) {
121 # We have a running shell ...
122 $np->add_message(CRITICAL, sprintf("SSH %s - We should'nt have a shell !!",$version2) );
123 } elsif ( $out =~ /$expect/ ) {
124 $np->add_message(OK, sprintf("SSH %s - Expected string found.",$version2) );
125 $found++;
126 }
127 }
128
129 if ( ! $found ) {
130 $np->add_message(WARNING, sprintf("SSH %s - Expected string not found !.",$version2) );
131 }
132
133
134 } else {
135 $np->add_message(CRITICAL, "Shell failed !" );
136 }
137 $chan->close();
138
139 } else {
140
141 $np->add_message(CRITICAL, "Password authentification failed !" );
142 }
143 $ssh2->disconnect('leaving');
144
145 my ($status, $message) = $np->check_messages('join' => '<br/>');
146 $np->nagios_exit($status, $message );
147
148
149 sub logD {
150 print STDERR 'DEBUG: '.$_[0]."\n" if ($DEBUG);
151 }
152 sub logW {
153 print STDERR 'WARNING: '.$_[0]."\n" if ($DEBUG);
154 }
155 # Gettext wrapper
156 sub _gt {
157 return gettext($_[0]);
158 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8