/[mon-modules]/socksch.monitor
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /socksch.monitor

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Wed Jul 31 14:29:30 2002 UTC (16 years, 1 month ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +2 -0 lines
display host name that is tesed in debug output

1 #!/usr/bin/perl -w
2 #
3 # try to connect to a particular port on a bunch of hosts using
4 # socks proxy to connect through. For use with "mon".
5 #
6 # Options are
7 # -p <port-num>
8 # -t <connect-timeout-in-seconds> (default 15)
9 # -s <string to send upon connecting to provoke some output>
10 # -e <Perl regexp to expect in response>
11 # -q <string to send before closing after parsing response>
12 # -d <string to use as line delimiter for regexp matching>
13 # -S <socks server to connect to>
14 # -P <socks server port> (1080 by default)
15 # -v verbose (dump lot of debugging inforations)
16
17 # without /-s/-e/-q/, just checks that the socket can be opened
18 # and closed.
19
20 # cheap transformations done on send/quit/delim strings - \r and \n are
21 # converted to CR and LF. \\ is not supported - no escape possible.
22
23 # sample usage:
24 #
25 # smtp: socksch.monitor -p 25 -e '^220\b' -q 'QUIT\r\n'
26 # web: socksch.monitor -p 80 -s 'GET / HTTP/1.0\r\n\r\n' -e '^HTTP.*200 OK'
27
28
29 #
30 # Jim Trocki, trockij@transmeta.com
31 # updated August 2000 by Ed Ravin <eravin@panix.com> for send/expect/quit
32 # updated July 2002 by Dobrica Pavlinusic <dpavlin@rot13.org> to use
33 # socks 4|5 servers
34 #
35 # Copyright (C) 1998, Jim Trocki
36 #
37 # This program is free software; you can redistribute it and/or modify
38 # it under the terms of the GNU General Public License as published by
39 # the Free Software Foundation; either version 2 of the License, or
40 # (at your option) any later version.
41 #
42 # This program is distributed in the hope that it will be useful,
43 # but WITHOUT ANY WARRANTY; without even the implied warranty of
44 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 # GNU General Public License for more details.
46 #
47 # You should have received a copy of the GNU General Public License
48 # along with this program; if not, write to the Free Software
49 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
50 #
51 use Getopt::Std;
52 use Socket;
53
54 use strict;
55 use Net::SOCKS;
56
57 my %opt;
58 getopts ("d:p:t:s:e:q:S:P:v", \%opt);
59 my $USAGE= "Usage: socksch.monitor -p port [-t timeout] [-s sendstr] [-e regexp] [-q quitstr] [-d line-delim] [-S socks server] [-P socks port] [-v]\n";
60
61 my $PORT = $opt{"p"} || undef;
62 my $TIMEOUT = $opt{"t"} || 15;
63
64 my $SEND= $opt{"s"} || undef;
65 my $EXPECT= $opt{"e"} || undef;
66 my $QUITSTR=$opt{"q"} || undef;
67 my $DELIM= $opt{"d"} || "\n";
68 my $SOCKS_SERVER = $opt{"S"} || "socks.pliva.hr";
69 my $SOCKS_PORT = $opt{"P"} || 1080;
70 my $verbose = $opt{"v"} || 0;
71
72 if ($DELIM)
73 {
74 $DELIM=~ s/\\n/\n/g;
75 $DELIM=~ s/\\r/\r/g;
76 }
77
78 my @failures = ();
79 my @detail = ();
80
81 my $ALARM = 0;
82
83 sub checkbuf # buffer, regexp
84 {
85 my ($buffer, $regexp)= @_;
86
87 return $buffer =~ /$regexp/ if ($DELIM eq '');
88
89 my @lines= split($DELIM, $buffer);
90
91 foreach my $line (@lines)
92 {
93 if ($line =~ /$regexp/)
94 {
95 return 1;
96 }
97 }
98 return 0;
99 }
100
101 die $USAGE unless (@ARGV > 0);
102 die "$0: missing port number\n" unless defined $PORT;
103
104 foreach my $host (@ARGV) {
105
106 push @detail, "(debug) testing $host";
107
108 my $sock = new Net::SOCKS(socks_addr => $SOCKS_SERVER,
109 socks_port => $SOCKS_PORT,
110 #user_id => 'the_user',
111 #user_password => 'the_password',
112 #force_nonanonymous => 1,
113 protocol_version => 5);
114
115 if (!defined $sock) {
116 die "(local err) could not create socks socket: $!\n";
117 } elsif ($verbose) {
118 push @detail, "(debug) connected to socks server $SOCKS_SERVER:$SOCKS_PORT";
119 }
120
121 my $r;
122
123 eval {
124 local $SIG{"ALRM"} = sub { die "alarm\n" };
125
126 alarm $TIMEOUT;
127
128 $r = $sock->connect(peer_addr => $host, peer_port => $PORT);
129
130 push @detail, "(debug) connect status: ".Net::SOCKS::status_message($sock->param('status_num')) if ($verbose);
131
132 if ($sock->param('status_num') != SOCKS_OKAY) {
133 die "(local err) could not connect to peer $host port $PORT: $!\n";
134 }
135
136 alarm 0;
137 };
138
139 if ($@) {
140 push @failures, $host;
141
142 if ($@ eq "alarm\n") {
143 push @detail, "$host timeout on connect";
144 } else {
145 push @detail, "$host interrupted syscall on connect: $!";
146 }
147
148 $sock->close();
149 next;
150 }
151
152 if (!defined $r) {
153 push @failures, $host;
154 push @detail, "$host: could not connect: $!";
155 $sock->close();
156 next;
157 }
158
159 if (defined($SEND)) {
160 my $rc= undef;
161
162 $SEND=~ s/\\n/\n/g;
163 $SEND=~ s/\\r/\r/g;
164 eval {
165 local $SIG{"ALRM"} = sub { die "alarm\n" };
166
167 alarm $TIMEOUT;
168 push @detail, "(debug) sending '$SEND'" if ($verbose);
169 print $r $SEND;
170 alarm 0;
171 };
172 if ($@) {
173 push @failures, $host;
174
175 if ($@ eq "alarm\n") {
176 push @detail, "$host timeout on write";
177 } else {
178 push @detail, "$host interrupted syscall on write: $!";
179 }
180 }
181 }
182
183 if (defined($EXPECT)) {
184 # read and match
185
186 my $alldata= "";
187
188 eval {
189 local $SIG{"ALRM"} = sub { die "alarm\n" };
190
191 alarm $TIMEOUT;
192
193 push @detail, "(debug) expecting '$EXPECT'" if ($verbose);
194 while (<$r>) {
195 $alldata .= $_;
196 last if (checkbuf($alldata, $EXPECT));
197 }
198 alarm 0;
199 };
200
201 if ($@) {
202 push @failures, $host;
203
204 if ($@ eq "alarm\n") {
205 push @detail, "$host timeout on read";
206 } else {
207 push @detail, "$host interrupted syscall on read: $!";
208 }
209 }
210
211 if (! checkbuf($alldata, $EXPECT)) {
212 push @failures, $host;
213 push @detail, "$host: did not recv expected response";
214 $sock->close();
215 next;
216 }
217 }
218
219 if (defined($QUITSTR)) {
220 my $rc= undef;
221
222 $QUITSTR=~ s/\\n/\n/g;
223 $QUITSTR=~ s/\\r/\r/g;
224
225 eval {
226 local $SIG{"ALRM"} = sub { die "alarm\n" };
227
228 alarm $TIMEOUT;
229
230 push @detail, "(debug) sending '$QUITSTR'" if ($verbose);
231 print $r $QUITSTR;
232 alarm 0;
233 };
234 if ($@) {
235 push @failures, $host;
236
237 if ($@ eq "alarm\n") {
238 push @detail, "$host timeout writing quitstr";
239 } else {
240 push @detail, "$host interrupted syscall writing quitstr: $!";
241 }
242 }
243 }
244
245 if (! $sock->close()) {
246 push @failures, $host;
247 push @detail, "$host: could not close socket: $!";
248 next;
249 }
250 }
251
252 if (@failures > 0 || $verbose) {
253 print "@failures\n";
254 print "\n", join ("\n", @detail), "\n";
255 }
256
257 if (@failures == 0) {
258 exit 0;
259 }
260
261 exit 1;

  ViewVC Help
Powered by ViewVC 1.1.26