You are not logged in.

#1 2007-12-05 02:22:44

thayer
Fellow
From: Vancouver, BC
Registered: 2007-05-20
Posts: 1,560
Website

Bash scripting help: How to poll network up/down speeds

I'm putting together a bash if...then statement for dzen that will handle the display of ethernet and wireless network information and one of the things I'd like to do is list the up/down speeds of a given interface.  What file should I be referencing this information?

Any help would be greatly appreciated!


thayer williams ~ cinderwick.ca

Offline

#2 2007-12-05 03:01:26

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Bash scripting help: How to poll network up/down speeds

You can find some stats in /proc/net/dev

Offline

#3 2007-12-05 19:24:39

gotmor
Member
From: Germany
Registered: 2007-09-03
Posts: 84
Website

Re: Bash scripting help: How to poll network up/down speeds

thayer wrote:

I'm putting together a bash if...then statement for dzen that will handle the display of ethernet and wireless network information and one of the things I'd like to do is list the up/down speeds of a given interface.  What file should I be referencing this information?

Any help would be greatly appreciated!

I'd recoment you to use /sys/class/net/$INTERFACE/statistics/rx_bytes and /sys/class/net/$INTERFACE/statistics/tx_bytes.

So, here a very brief example:

INTERFACE=eth0

while :; do 
   P2=$P1
   P1=`cat /sys/class/net/${INTERFACE}/statistics/rx_bytes`
   echo Down: `expr  $P1 - $P2` B/s
   sleep 1;
done

Last edited by gotmor (2007-12-05 19:38:41)

Offline

#4 2007-12-05 21:10:43

thayer
Fellow
From: Vancouver, BC
Registered: 2007-05-20
Posts: 1,560
Website

Re: Bash scripting help: How to poll network up/down speeds

If I am understanding this correctly, there is no way of capturing the up/down rates in a raw form.  Is that right?  It looks like I'll have to pass two values (snapshots at different intervals, e.g. 1 second apart) through an equation in order to calculate the current rate. I was trying to avoid that because I'd like to have the net stats embedded in a script that displays other system stats and sleeps for 5-20 seconds between updates.

Last edited by thayer (2007-12-05 21:11:30)


thayer williams ~ cinderwick.ca

Offline

#5 2007-12-05 22:34:39

elide
Member
From: Russia
Registered: 2007-12-02
Posts: 40

Re: Bash scripting help: How to poll network up/down speeds

thayer wrote:

I'd like to have the net stats embedded in a script that sleeps for 5-20 seconds between updates

Just keep time and stat data from last call:

function get_net_speed(){ # get_net_speed eth1 rx
    CF="/sys/class/net/$1/statistics/$2_bytes"
    PF="$TMP/$1-$2"
    PFT="$PF-ts"

    PFTV=`cat "$PFT" 2>/dev/null`
    PFV=`cat "$PF" 2>/dev/null`
    CFTV=`date +%s`
    CFV=`cat "$CF" 2>/dev/null`

    cat "$CF" > "$PF"
    date +%s > "$PFT"

    echo "scale=2; ( $CFV - ${PFV:-0} ) / ( $CFTV - ${PFTV:-0} ) / 1024" | bc
}

get_net_speed eth1 rx
sleep 1
get_net_speed eth1 rx
sleep 10
get_net_speed eth1 rx
sleep 7
get_net_speed eth1 rx
sleep 3
get_net_speed eth1 rx

Offline

#6 2007-12-06 00:22:28

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Bash scripting help: How to poll network up/down speeds

I have been using this as part of screen's status line for years:
http://code.phraktured.net/?p=config.gi … en/netinfo

Offline

#7 2007-12-12 00:32:44

thayer
Fellow
From: Vancouver, BC
Registered: 2007-05-20
Posts: 1,560
Website

Re: Bash scripting help: How to poll network up/down speeds

Thanks for the tips, guys.  Unfortunately, I couldn't the examples to work and I couldn't understand them enough to figure out why.  Rob has posted a network script for dzen though so that saves me from reinventing the wheel.  Cheers!


thayer williams ~ cinderwick.ca

Offline

Board footer

Powered by FluxBB