#!/usr/bin/perl # perl script to measure in/out traffic in realtime # ver: 0.1 # usage: iftraffic.pl # example: iftraffic.pl eth0 # Notes: # 1) Numbers are not exact by choice. They are converted to integers. If you are running the script on a heavily # loaded system expect a 10-15% deviation # 2) If you want to have repetitions uncomment line 17 and 32 and comment line 31. # Then use it like: iftraffic.pl eth0 10 # 3) You can also change it back to reading the ifconfig output as the original script by uncommenting lines 20,21 # and commenting lines 22,23 # by kargig [at] void [dot] gr # original version: http://perlmonks.org/?node_id=635792 my $dev=$ARGV[0]; #my $repetitions=$ARGV[1]; sub get_measures { ##my $data = `/sbin/ifconfig $dev | grep "RX bytes" | head -n1`; ##$data =~ /RX bytes\:(\d+) .*TX bytes\:(\d+) .*/; my $data = `cat /proc/net/dev | grep "$dev" | head -n1`; $data =~ /$dev\:(\d+)\D+\d+\D+\d+\D+\d+\D+\d+\D+\d+\D+\d+\D+\d+\D+(\d+)\D+/; my $recv = int($1/1024); my $sent= int($2/1024); return ($recv,$sent); } my @m1 = get_measures; while(1) { #for (my $time=0;$time<$repetitions;$time++) { sleep 1; my @m2 = get_measures; my @rates = ($m2[0] - $m1[0], $m2[1]-$m1[1]); foreach ('received' , ' transmit') { printf "$_ rate:%sKB",shift @rates; } print "\n"; @m1=@m2; }