/[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.1 - (show annotations)
Wed Jul 31 14:19:05 2002 UTC (21 years, 6 months ago) by dpavlin
Branch: MAIN
tcp chat through socks server -- usefull if you want to monitor your
external HTTP or SMTP port and you don't have direct connection to it.

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 my $sock = new Net::SOCKS(socks_addr => $SOCKS_SERVER,
107 socks_port => $SOCKS_PORT,
108 #user_id => 'the_user',
109 #user_password => 'the_password',
110 #force_nonanonymous => 1,
111 protocol_version => 5);
112
113 if (!defined $sock) {
114 die "(local err) could not create socks socket: $!\n";
115 } elsif ($verbose) {
116 push @detail, "(debug) connected to socks server $SOCKS_SERVER:$SOCKS_PORT";
117 }
118
119 my $r;
120
121 eval {
122 local $SIG{"ALRM"} = sub { die "alarm\n" };
123
124 alarm $TIMEOUT;
125
126 $r = $sock->connect(peer_addr => $host, peer_port => $PORT);
127
128 push @detail, "(debug) connect status: ".Net::SOCKS::status_message($sock->param('status_num')) if ($verbose);
129
130 if ($sock->param('status_num') != SOCKS_OKAY) {
131 die "(local err) could not connect to peer $host port $PORT: $!\n";
132 }
133
134 alarm 0;
135 };
136
137 if ($@) {
138 push @failures, $host;
139
140 if ($@ eq "alarm\n") {
141 push @detail, "$host timeout on connect";
142 } else {
143 push @detail, "$host interrupted syscall on connect: $!";
144 }
145
146 $sock->close();
147 next;
148 }
149
150 if (!defined $r) {
151 push @failures, $host;
152 push @detail, "$host: could not connect: $!";
153 $sock->close();
154 next;
155 }
156
157 if (defined($SEND)) {
158 my $rc= undef;
159
160 $SEND=~ s/\\n/\n/g;
161 $SEND=~ s/\\r/\r/g;
162 eval {
163 local $SIG{"ALRM"} = sub { die "alarm\n" };
164
165 alarm $TIMEOUT;
166 push @detail, "(debug) sending '$SEND'" if ($verbose);
167 print $r $SEND;
168 alarm 0;
169 };
170 if ($@) {
171 push @failures, $host;
172
173 if ($@ eq "alarm\n") {
174 push @detail, "$host timeout on write";
175 } else {
176 push @detail, "$host interrupted syscall on write: $!";
177 }
178 }
179 }
180
181 if (defined($EXPECT)) {
182 # read and match
183
184 my $alldata= "";
185
186 eval {
187 local $SIG{"ALRM"} = sub { die "alarm\n" };
188
189 alarm $TIMEOUT;
190
191 push @detail, "(debug) expecting '$EXPECT'" if ($verbose);
192 while (<$r>) {
193 $alldata .= $_;
194 last if (checkbuf($alldata, $EXPECT));
195 }
196 alarm 0;
197 };
198
199 if ($@) {
200 push @failures, $host;
201
202 if ($@ eq "alarm\n") {
203 push @detail, "$host timeout on read";
204 } else {
205 push @detail, "$host interrupted syscall on read: $!";
206 }
207 }
208
209 if (! checkbuf($alldata, $EXPECT)) {
210 push @failures, $host;
211 push @detail, "$host: did not recv expected response";
212 $sock->close();
213 next;
214 }
215 }
216
217 if (defined($QUITSTR)) {
218 my $rc= undef;
219
220 $QUITSTR=~ s/\\n/\n/g;
221 $QUITSTR=~ s/\\r/\r/g;
222
223 eval {
224 local $SIG{"ALRM"} = sub { die "alarm\n" };
225
226 alarm $TIMEOUT;
227
228 push @detail, "(debug) sending '$QUITSTR'" if ($verbose);
229 print $r $QUITSTR;
230 alarm 0;
231 };
232 if ($@) {
233 push @failures, $host;
234
235 if ($@ eq "alarm\n") {
236 push @detail, "$host timeout writing quitstr";
237 } else {
238 push @detail, "$host interrupted syscall writing quitstr: $!";
239 }
240 }
241 }
242
243 if (! $sock->close()) {
244 push @failures, $host;
245 push @detail, "$host: could not close socket: $!";
246 next;
247 }
248 }
249
250 if (@failures > 0 || $verbose) {
251 print "@failures\n";
252 print "\n", join ("\n", @detail), "\n";
253 }
254
255 if (@failures == 0) {
256 exit 0;
257 }
258
259 exit 1;

  ViewVC Help
Powered by ViewVC 1.1.26