-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlan_traffic
executable file
·80 lines (67 loc) · 2.09 KB
/
lan_traffic
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
#!/usr/bin/env perl
use Net::Pcap;
use NetPacket::Ethernet;
use NetPacket::IP;
use Time::HiRes qw(time);
use Net::Subnet;
use strict;
use warnings;
my $err;
my $traffic = {};
my $traffic2 = {};
my $last_time = time;
my $is_rfc1918 = subnet_matcher qw(
10.0.0.0/16
192.168.0.0/16
);
my $dev = $ARGV[0] or die "Usage: lan_traffic interface \n";
$dev = Net::Pcap::lookupdev(\$err);
die "Unable to determine network device for monitoring - $err\n" if defined $err;
my ($address, $netmask);
Net::Pcap::lookupnet($dev, \$address, \$netmask, \$err);
die "Unable to look up device information for $dev - $err\n" if defined $err;
my $object;
$object = Net::Pcap::open_live($dev, 1500, 0, 0, \$err);
die "Unable to create packet capture on device $dev - $err\n" if defined $err;
print "...\n";
Net::Pcap::loop($object, -1, \&syn_packets, '') or die "Unable to perform packet capture on device $dev\n";
Net::Pcap::close($object);
sub format_size
{
my $s = shift;
return sprintf("%.2fB", $s) if $s < 1024;
return sprintf("%.2fKB", $s / 1024) if $s < 1024 * 1024;
return sprintf("%.2fMB", $s / 1024 / 1024);
}
sub show_data
{
my $time = time;
my $delta = $time - $last_time;
return if $delta < 1;
print "===\n";
my $total = 0;
for my $ip (keys %$traffic)
{
my $len = $traffic->{$ip};
my $last_len = $traffic2->{$ip} || 0;
my $speed = ($len - $last_len) / $delta;
next if $speed <= 0;
$total += $speed;
print "$ip " . format_size($speed) . "/s\n";
}
printf("Total speed : %s\n", format_size($total));
$traffic2 = $traffic;
$traffic = {};
$last_time = $time;
}
sub syn_packets {
my ($user_data, $header, $packet) = @_;
my $ether_data = NetPacket::Ethernet::strip($packet);
my $ip = NetPacket::IP->decode($ether_data);
my $src_ip = $ip->{src_ip};
my $dest_ip = $ip->{dest_ip};
return if($is_rfc1918->($src_ip) && $is_rfc1918->($dest_ip));
$traffic->{$src_ip} += $ip->{len} if $is_rfc1918->($src_ip);
$traffic->{$dest_ip} += $ip->{len} if $is_rfc1918->($dest_ip);
show_data();
}