#!/usr/bin/perl # displays to stdout the bandwidth use of a interface $alive = 1; $SIG{'INT'} = sigshut; $SIG{'TERM'} = sigshut; while($alive) { sleep(1); open(IFC,"/sbin/ifconfig eth6|"); while($in = ) { if(($x) = $in =~ /.*RX bytes:(\d+).*/) { $rxt = $rxd; $rxd = $x; } if(($x) = $in =~ /.*TX bytes:(\d+).*/) { $txt = $txd; $txd = $x; } } close(IFC); next if(!$rxt || !$txt); $rxsec = $rxd - $rxt; $txsec = $txd - $txt; $rxformat = bwformat($rxsec); $txformat = bwformat($txsec); print "rx: $rxformat tx: $txformat\n"; } sub sigshut { $alive = 0; } sub bwformat { $a = shift; if($a < 1024) { return "$a bits/s"; } elsif ($a < (1024 * 1024)) { return int($a / 1024) . " kbits/s"; } else { return $a / (1024 * 1024) . " mbits/s"; } return "NaN"; }