~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to ntop/www/Perl/nClient

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl
2
 
#
3
 
# Copyright (C) 2001 Luca Deri <deri@ntop.org>
4
 
#
5
 
#               http://www.ntop.org/
6
 
#
7
 
#
8
 
# LWP module can be downloaded from www.cpan.org
9
 
#
10
 
use LWP::Simple;
11
 
 
12
 
# $debug = 1;
13
 
 
14
 
$ntopUser    = "";
15
 
$ntopHost    = "";
16
 
$ntopPort    = "";
17
 
$passwdFile  = "";
18
 
$homeNtopURL = "";
19
 
 
20
 
$passwdFile = $ENV{HOME}."/.ntopasswd";
21
 
 
22
 
 
23
 
##############################
24
 
 
25
 
if($debug) {
26
 
    for($i=0; $i<10; $i++) {
27
 
        if($ARGV[$i] ne "") {
28
 
            print $i."\t".$ARGV[$i]."\n";
29
 
        } else {
30
 
            last;
31
 
        }
32
 
    }
33
 
}
34
 
 
35
 
###############################
36
 
 
37
 
if($ARGV[0]    =~ /attach/)    { attach();    }
38
 
elsif($ARGV[0] =~ /lsdev/)     { lsdev();     }
39
 
elsif($ARGV[0] =~ /devinfo/)   { devinfo();   }
40
 
elsif($ARGV[0] =~ /lshost/)    { lshost();    }
41
 
elsif($ARGV[0] =~ /hostinfo/)  { hostinfo();  }
42
 
elsif($ARGV[0] =~ /arp/)       { arp();       }
43
 
elsif($ARGV[0] =~ /nbt/)       { nbt();       }
44
 
else                           { usage();     }
45
 
 
46
 
 
47
 
##############################
48
 
 
49
 
sub attach {
50
 
# attach  [user:pw@]host:port
51
 
 
52
 
    if($ENV{NTOP_ROOT} eq "") {
53
 
        print "ERROR: NTOP_ROOT is empty. Set it to user\@ntopHost:ntopPort\n\n";
54
 
        usage();
55
 
    }
56
 
 
57
 
    if($ENV{NTOP_ROOT} =~ /(\S*)@(\S*):(\d*)/) {
58
 
        $ntopUser = $1;
59
 
        $ntopHost = $2;
60
 
        $ntopPort = $3;
61
 
 
62
 
        if($debug) {
63
 
            print "user: $ntopUser\n";
64
 
            print "host: $ntopHost\n";
65
 
            print "port: $ntopPort\n";
66
 
        }
67
 
 
68
 
        if(($1 eq "") || ($2 eq "") ||  ($3 eq "")) {
69
 
            print "ERROR: NTOP_ROOT is not properly defined. Set it to user\@ntopHost:ntopPort\n\n";
70
 
            usage();
71
 
        }
72
 
 
73
 
        print "Password: ";
74
 
        $ntopPwd = <STDIN>; chop($ntopPwd);
75
 
 
76
 
        if($debug) { print "Password: '".$ntopPwd."'\n"; }
77
 
 
78
 
        $URL = "http://$ntopUser:$ntopPwd\@".$ntopHost.":".$ntopPort."/";
79
 
 
80
 
        if($debug) { print "URL: '".$URL."'\n"; }
81
 
 
82
 
        $content = get($URL);
83
 
 
84
 
        if($content eq "") {
85
 
            print "ERROR: unable to connect to ntop. Please check host:port/user/pw\n";
86
 
            exit(-1);
87
 
        } else {
88
 
            print "User $ntopUser successfully connected to $ntopHost:$ntopPort\n";
89
 
        }
90
 
 
91
 
        umask((umask() & 077) | 077); # -rw-------
92
 
 
93
 
        if(open(OUT, "> ".$passwdFile)) {
94
 
            print OUT "http://$ntopUser:$ntopPwd\@".$ntopHost.":".$ntopPort."/\n";
95
 
            close(OUT);
96
 
        } else {
97
 
            print "ERROR: unable to create/update file $passwdFile\n";
98
 
            exit(-1);
99
 
        }
100
 
    } else {
101
 
        usage();
102
 
    }
103
 
}
104
 
 
105
 
##############################
106
 
 
107
 
sub lsdev {
108
 
    getNtopHomeURL();
109
 
 
110
 
    $content = get($homeNtopURL."dumpTrafficData.html?language=perl&filter=ifAddr");
111
 
 
112
 
    if($debug) { print "Received: $content\n"; }
113
 
 
114
 
    if($content eq "") {
115
 
        print "ERROR: unable to talk with ntop.\n";
116
 
        exit(-1);
117
 
    } else {
118
 
        %hash = eval($content);
119
 
 
120
 
        while (($key, $value) = each %hash) {
121
 
            print $key."\n";
122
 
        }
123
 
    }
124
 
}
125
 
 
126
 
##############################
127
 
 
128
 
sub unbundle {
129
 
    my(%elems) = @_;
130
 
    my(@unbundled) = ();
131
 
    my($index)= 0;
132
 
    while (($key, $value) = each %elems) {
133
 
        if($value =~ /^HASH/) {
134
 
             while (($key1, $value1) = each %$value) {
135
 
                 if($value1 =~ /^HASH/) {
136
 
                     while (($key2, $value2) = each %$value1) {
137
 
                         $unbundled[$index][0] = $key.":".$key.":".$key2;
138
 
                         $unbundled[$index][1] = $value2;       
139
 
                         $index++;
140
 
                     }
141
 
                 } else {
142
 
                     $unbundled[$index][0] = $key.":".$key1;
143
 
                     $unbundled[$index][1] = $value1;
144
 
                     $index++;
145
 
                 }
146
 
             }
147
 
        } else {
148
 
            $unbundled[$index][0] = $key;
149
 
            $unbundled[$index][1]= $value;
150
 
            $index++;
151
 
        }
152
 
    }
153
 
 
154
 
    return(@unbundled);
155
 
}
156
 
 
157
 
##############################
158
 
 
159
 
sub devinfo {
160
 
    if($ARGV[1] eq "") { usage(); }
161
 
 
162
 
    getNtopHomeURL();
163
 
 
164
 
    $content = get($homeNtopURL."dumpTrafficData.html?language=perl&key=$ARGV[1]");
165
 
 
166
 
    if($debug) { print "Received: $content\n"; }
167
 
 
168
 
    if($content eq "") {
169
 
        print "ERROR: unable to talk with ntop.\n";
170
 
        exit(-1);
171
 
    } else {
172
 
        %hash = eval($content);
173
 
 
174
 
        if(!$hash{$ARGV[1]}) {
175
 
            print "ERROR: unknown device $ARGV[1].\n";
176
 
            exit(-1);
177
 
        }
178
 
 
179
 
        $elems = $hash{$ARGV[1]};
180
 
        %elems = %$elems;
181
 
        @allelems = unbundle(%elems);
182
 
 
183
 
        foreach $i (0 .. $#allelems) {
184
 
            printf("%-30s\t%s\n", $allelems[$i][0], $allelems[$i][1]);
185
 
        }
186
 
    }
187
 
}
188
 
 
189
 
##############################
190
 
 
191
 
sub lshost {
192
 
    getNtopHomeURL();
193
 
 
194
 
    $content = get($homeNtopURL."dumpData.html?language=perl&filter=ethAddressString");
195
 
 
196
 
    if($debug) { print "Received: $content\n"; }
197
 
 
198
 
    if($content eq "") {
199
 
        print "ERROR: unable to talk with ntop.\n";
200
 
        exit(-1);
201
 
    } else {
202
 
        %hash = eval($content);
203
 
 
204
 
        while (($key, $value) = each %hash) {
205
 
            print $key."\n";
206
 
        }
207
 
    }
208
 
}
209
 
 
210
 
##############################
211
 
 
212
 
sub hostinfo {
213
 
    if($ARGV[1] eq "") { usage(); }
214
 
 
215
 
    getNtopHomeURL();
216
 
 
217
 
    $content = get($homeNtopURL."dumpData.html?language=perl&key=$ARGV[1]");
218
 
 
219
 
    if($debug) { print "Received: $content\n"; }
220
 
 
221
 
    if($content eq "") {
222
 
        print "ERROR: unable to talk with ntop.\n";
223
 
        exit(-1);
224
 
    } else {
225
 
        %hash = eval($content);
226
 
 
227
 
        if(!$hash{$ARGV[1]}) {
228
 
            print "ERROR: unknown device $ARGV[1].\n";
229
 
            exit(-1);
230
 
        }
231
 
 
232
 
        $elems = $hash{$ARGV[1]};
233
 
        %elems = %$elems;
234
 
        @allelems = unbundle(%elems);
235
 
 
236
 
        foreach $i (0 .. $#allelems) {
237
 
            printf("%-30s\t%s\n", $allelems[$i][0], $allelems[$i][1]);
238
 
        }
239
 
    }
240
 
}
241
 
 
242
 
##############################
243
 
 
244
 
sub arp {
245
 
    getNtopHomeURL();
246
 
 
247
 
    $content = get($homeNtopURL."dumpData.html?language=perl&filter=ethAddressString");
248
 
 
249
 
    if($debug) { print "Received: $content\n"; }
250
 
 
251
 
    if($content eq "") {
252
 
        print "ERROR: unable to talk with ntop.\n";
253
 
        exit(-1);
254
 
    } else {
255
 
        %hash = eval($content);
256
 
 
257
 
        while (($key, $dummy) = each %hash) {
258
 
            my($macAddress) = $hash{$key}{ethAddressString};
259
 
 
260
 
            if(($key ne "")
261
 
               && ($macAddress ne "")
262
 
               && (!($key =~ /:/))) {
263
 
                printf("%-20s\t%-20s\n", $key, $macAddress);
264
 
            }
265
 
        }
266
 
    }
267
 
 
268
 
}
269
 
 
270
 
 
271
 
##############################
272
 
 
273
 
sub nbt {
274
 
    getNtopHomeURL();
275
 
 
276
 
    $content = get($homeNtopURL."dumpData.html?language=perl&filter=nbHostName");
277
 
 
278
 
    if($debug) { print "Received: $content\n"; }
279
 
 
280
 
    if($content eq "") {
281
 
        print "ERROR: unable to talk with ntop.\n";
282
 
        exit(-1);
283
 
    } else {
284
 
        %hash = eval($content);
285
 
 
286
 
        while (($key, $dummy) = each %hash) {
287
 
            my($nbHostName) = $hash{$key}{nbHostName};
288
 
 
289
 
            if(($key ne "") && ($nbHostName ne "")) {
290
 
                printf("%-20s\t%-20s\n", $key, $nbHostName);
291
 
            }
292
 
        }
293
 
    }
294
 
}
295
 
 
296
 
 
297
 
##############################
298
 
 
299
 
sub getNtopHomeURL {
300
 
    if(open(IN, $passwdFile)) {
301
 
        $homeNtopURL = <IN>;
302
 
        chop($homeNtopURL);
303
 
        close(IN);
304
 
        if($homeNtopURL eq "") {
305
 
            print "ERROR: problems while reading $passwdFile. Did you attach to a running ntop?\n";
306
 
            exit(-1);
307
 
        }
308
 
    } else {
309
 
        print "ERROR: unable to read file $passwdFile. Did you attach to a running ntop?\n";
310
 
        exit(-1);
311
 
    }
312
 
}
313
 
 
314
 
 
315
 
##############################
316
 
 
317
 
sub usage {
318
 
 
319
 
print "
320
 
    nClient <command> [ <parameters> ]
321
 
 
322
 
    Available commands:
323
 
       attach            attach to a running ntop
324
 
       lsdev             show ntop available network devices
325
 
       devinfo <device>  show info about the specified device
326
 
       lshost <host>     show ntop's known host
327
 
       hostinfo          show info about the specified host
328
 
       arp               show ntop's ARP table
329
 
       nbt               show ntop's NetBIOS hosts
330
 
 
331
 
 
332
 
    How to use nClient:
333
 
    1) start a copy of ntop at ntopHost:ntopPort
334
 
    2) Set the environment variable NTOP_ROOT to
335
 
       user\@ntopHost:ntopPort
336
 
       
337
 
       For instance (on tcsh):
338
 
       setenv NTOP_ROOT luca\@dummy.ntop.org:3000
339
 
    3) nClient attach
340
 
    4) nClient <insert your command here>
341
 
    5) Have fun!
342
 
 
343
 
    ======================================
344
 
 
345
 
    (C) 2001 - Luca Deri <deri\@ntop.org>\n";
346
 
 
347
 
    exit(0);
348
 
}
349
 
 
350