Find the answer to your Linux question:
Results 1 to 3 of 3
I don't know of any programs which will give me raw interface speeds, so I am trying to write a bash script. This is all so I can have speeds ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    45

    help with bash script

    I don't know of any programs which will give me raw interface speeds, so I am trying to write a bash script. This is all so I can have speeds for my interface (wlan0) in an fvwm module. It was working a minute ago... now it's not. I changed something, don't know what. This is the script:
    Code:
    #!/bin/sh
      2 # this script counts the number of new bytes per second for rx and tx as seen
         by ifconfig
      3 # if you want to see it live from the console use watch -n1,
      4 # it's meant to be repeated from an FvwmScript's PeriodicTasks section
      5 i=0
      6 while [ 2 -eq 2 ] ; do
      7         ref1="$(/sbin/ifconfig wlan0)"
      8         prdct1="$(echo "$ref1" | sed --quiet -e '/RX bytes/p')"
      9         rxpre="$(echo $prdct1 | awk -F' ' '{print $2}')"
     10         txpre="$(echo $prdct1 | awk -F' ' '{print $6}')"
     11         rx="$(echo $rxpre | awk -F: '{print $2}')"
     12         tx="$(echo $txpre | awk -F: '{print $2}')"
     13         getrx() {
     14                 rx_onesecago="$rx"
     15                 sleep 1
     16                 rx_now="$rx"
     17                 currentrx="$(expr $rx_now - $rx_onesecago)"
     18                 echo "$rx_now - $rx_onesecago"
     19                 return $currentrx
     20         }
     21         gettx() {
     22                 tx_onesecago="$tx"
     23                 sleep 1
     24                 tx_now="$tx"
     25                 currenttx="$(expr $tx_now - $tx_onesecago)"
     26         }
     27         if [ $i -lt 1 ]; then
     28                 getrx &
     29                 rx_speed=$?
     30                 gettx
     31                 echo "rx: $rx_speed B/s tx: $currenttx B/s"
     32                 if [ "$1" == "full" ]; then
     33                 echo -e "rx_size_before: $rx_onesecago\ttx_size_before: $tx_
        onesecago"
     34                 echo -e "rx_size_after: $rx\ttx_size_after: $tx "
     35                 fi
     36         else
     37                 break 2
     38         fi
     39         i=$(expr $i + 1)
     40 done
    I'm figuring rx_now and rx_onesecago are coming out the same because prdct1,rxpre,etc.. are not updated each time they are used. I really have no idea how it worked before.
    output: (with internet radio station streaming)
    rx: 0 B/s tx: 0 B/s
    rx_size_before: tx_size_before: 10447727
    rx_size_after: 93203253 tx_size_after: 10447727
    93203253 - 93203253

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    This can be of help.
    Code:
    #!/bin/bash
    getData() {
      ref1="$(/sbin/ifconfig eth0)"
      prdct1="$(echo "$ref1" | sed --quiet -e '/RX bytes/p')"
      rxpre="$(echo $prdct1 | awk -F' ' '{print $2}')"
      txpre="$(echo $prdct1 | awk -F' ' '{print $6}')"
      rx="$(echo $rxpre | awk -F: '{print $2}')"
      tx="$(echo $txpre | awk -F: '{print $2}')"
      [ $1 = 0 ] && echo $rx
      [ $1 = 1 ] && echo $tx
    }
    while [ "$1" = "15" ] ; do
      rx0=$(getData 0)
      tx0=$(getData 1)
      sleep 1
      rx1=$(getData 0)
      tx1=$(getData 1)
      echo $(($rx1-$rx0)) $(($tx1-$tx0))
    done
    Why should rx_onesecago and rx_now be different in

    Code:
    14                  rx_onesecago="$rx"
     15                 sleep 1
     16                 rx_now="$rx"
    ?
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined!
    Join Date
    Jul 2009
    Posts
    45
    thank you.
    to get the increase of the rx and tx totals

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...