File: tcpch.monitor

package info (click to toggle)
mon-contrib 1.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 892 kB
  • sloc: perl: 5,510; sh: 383; python: 118; makefile: 72
file content (282 lines) | stat: -rwxr-xr-x 5,798 bytes parent folder | download | duplicates (5)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl -w
#
# try to connect to a particular
# port on a bunch of hosts. For use with "mon".
#
# Options are
#   -p <port-num>
#   -t <connect-timeout-in-seconds> (default 15)
#   -s <string to send upon connecting to provoke some output>
#   -e <Perl regexp to expect in response>
#   -q <string to send before closing after parsing response>
#   -d <string to use as line delimiter for regexp matching>

# without /-s/-e/-q/, just checks that the socket can be opened
# and closed.

# cheap transformations done on send/quit/delim strings - \r and \n are
# converted to CR and LF.  \\ is not supported - no escape possible.

# sample usage:
#
# smtp:    tcpch.monitor -p 25  -e '^220\b' -q 'QUIT\r\n'
# web:     tcpch.monitor -p 80  -s 'GET / HTTP/1.0\r\n' -e '^HTTP.*200 OK'


#
# Jim Trocki, trockij@transmeta.com
# updated August 2000 by Ed Ravin <eravin@panix.com> for send/expect/quit
#
# $Id: tcpch.monitor,v 1.1.1.1 2005/02/18 17:52:23 trockij Exp $
#
#    Copyright (C) 1998, Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use Getopt::Std;
use Socket;

my %opt;
getopts ("d:p:t:s:e:q:", \%opt);
$USAGE= "Usage: tcpch.monitor -p port [-t timeout] [-s sendstr] [-e regexp] [-q quitstr] [-d line-delim]\n";

my $PORT = $opt{"p"} || undef;
my $TIMEOUT = $opt{"t"} || 15;

my $SEND=   $opt{"s"} || undef;
my $EXPECT= $opt{"e"} || undef;
my $QUITSTR=$opt{"q"} || undef;
my $DELIM=  $opt{"d"} || "\n";
if ($DELIM)
{
	$DELIM=~ s/\\n/\n/g;
	$DELIM=~ s/\\r/\r/g;
}

my @failures = ();
my @detail = ();

my $ALARM = 0;

sub checkbuf  # buffer, regexp
{
	my ($buffer, $regexp)= @_;

	return $buffer =~ /$regexp/ if ($DELIM eq '');

	my @lines= split($DELIM, $buffer);

	foreach my $line (@lines)
	{
		if ($line =~ /$regexp/)
		{
			return 1;
		}
	}
	return 0;
}

die $USAGE unless (@ARGV > 0);
die "$0: missing port number\n" unless defined $PORT;

foreach my $host (@ARGV) {
    my $pro = getprotobyname ('tcp');

    if (!defined $pro) {
    	die "(local err) could not getprotobyname\n";
    }

    if (!defined socket (S, PF_INET, SOCK_STREAM, $pro)) {
    	die "(local err) could not create socket: $!\n";
    }

    my $a = inet_aton ($host);
    if (!defined $a) {
    	push @failures, $host;
	push @detail, "(local err) $host could not inet_aton";
	close (S);
	next;
    }

    my $sin = sockaddr_in ($PORT, $a);
    if (!defined $sin) {
	push @failures, $host;
	push @detail, "(local err) $host could not sockaddr_in";
    	close (S);
	next;
    }

    my $r;

    eval {
	local $SIG{"ALRM"} = sub { die "alarm\n" };

	alarm $TIMEOUT;

	$r = connect (S, $sin);

	alarm 0;
    };

    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
			push @detail, "$host timeout on connect";
		} else {
			push @detail, "$host interrupted syscall on connect: $!";
		}

	close (S);
	next;
    }

    if (!defined $r) {
	push @failures, $host;
	push @detail, "$host: could not connect: $!";
	close (S);
	next;
    }

    select S; $|= 1; select STDOUT;

	if (defined($SEND))
	{
		my $rc= undef;

		$SEND=~ s/\\n/\n/g;
		$SEND=~ s/\\r/\r/g;
		eval {
			local $SIG{"ALRM"} = sub { die "alarm\n" };

			alarm $TIMEOUT;
			$rc= send S, $SEND, 0;
			alarm 0;
		    };
	    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
				push @detail, "$host timeout on write";
			} else {
				push @detail, "$host interrupted syscall on write: $!";
			}
		}

		if (! $rc)
		{
			push @failures, $host;
			push @detail, "$host: write failed: $!";
			close (S);
			next;
		}
	}

	if (defined($EXPECT))
	{
		# read and match

		my $rc= undef;
		my $alldata= "";

		eval {
			local $SIG{"ALRM"} = sub { die "alarm\n" };

			alarm $TIMEOUT;

			$rc= recv S, $rxdata, 1024, 0;
			$alldata= $alldata . $rxdata;

			while ( !checkbuf($alldata,  $EXPECT))
			{
				$rc= recv S, $rxdata, 1024, 0;
				$alldata= $alldata . $rxdata;
			}
			alarm 0;
		    };
	    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
				push @detail, "$host timeout on read";
			} else {
				push @detail, "$host interrupted syscall on read: $!";
			}
		}
		if ($rc)
		{
			push @failures, $host;
			push @detail, "$host: recv failed : $!";
			close (S);
			next;
		}

		if (! checkbuf($alldata, $EXPECT))
		{
			push @failures, $host;
			push @detail, "$host: did not recv expected response";
			close (S);
			next;
		}
	}

	if (defined($QUITSTR))
	{
		my $rc= undef;

		$QUITSTR=~ s/\\n/\n/g;
		$QUITSTR=~ s/\\r/\r/g;

		eval {
			local $SIG{"ALRM"} = sub { die "alarm\n" };

			alarm $TIMEOUT;
			$rc= send S, $QUITSTR, 0;
			alarm 0;
		    };
	    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
				push @detail, "$host timeout writing quitstr";
			} else {
				push @detail, "$host interrupted syscall writing quitstr: $!";
			}
		}

		if (! $rc)
		{
			push @failures, $host;
			push @detail, "$host: quit write failed: $!";
			close (S);
			next;
		}
	}

    if (!defined close (S)) {
    	push @failures, $host;
	push @detail, "$host: could not close socket: $!";
	next;
    }
}

if (@failures == 0) {
    exit 0;
}

print "@failures\n";
print "\n", join ("\n", @detail), "\n";

exit 1;