Skip to content

Instantly share code, notes, and snippets.

@avin
Last active August 29, 2015 14:07
Show Gist options
  • Save avin/c2599851d063c0767d5f to your computer and use it in GitHub Desktop.
Save avin/c2599851d063c0767d5f to your computer and use it in GitHub Desktop.
Show current bandwith using tcpdump
#!/usr/bin/perl
# Usage: tcpdump -i ng10 -l -e -n dst host 192.168.1.200 | ./tcpdump_bps.pl
use strict;
use warnings;
use Time::HiRes;
my $reporting_interval = 10.0; # seconds
my $bytes_this_interval = 0;
my $start_time = [Time::HiRes::gettimeofday()];
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