Skip to content

Instantly share code, notes, and snippets.

@tbaschak
Created March 2, 2017 05:16
Show Gist options
  • Save tbaschak/87b8d306d270006d532cd85bbb27b147 to your computer and use it in GitHub Desktop.
Save tbaschak/87b8d306d270006d532cd85bbb27b147 to your computer and use it in GitHub Desktop.
Measures BPS coming out of tcpdump. Usage: `tcpdump -l -e -n ether broadcast | ./netbps`. Un-Modified from http://superuser.com/questions/356907/how-to-get-real-time-network-statistics-in-linux-with-kb-mb-bytes-format-and-for Raw
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes;
my $reporting_interval = 5.0; # seconds
my $bytes_this_interval = 0;
my $start_time = [Time::HiRes::gettimeofday()];
STDOUT->autoflush(1);
while (<>) {
if (/ length (\d+):/) {
$bytes_this_interval += $1;
my $elapsed_seconds = Time::HiRes::tv_interval($start_time);
if ($elapsed_seconds > $reporting_interval) {
my $bps = $bytes_this_interval / $elapsed_seconds;
printf "%02d:%02d:%02d %10.2f Bps\n", (localtime())[2,1,0],$bps;
$start_time = [Time::HiRes::gettimeofday()];
$bytes_this_interval = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment