2

Is there a method to get the current network speed (just like I can get with «nload») in a bash script? I would just put into a variable the speed at the moment where script is running. Ex: «./script.sh» would output «In: 50Ko/s | Out: 25Ko/s».

Thanks :)

tux1124
  • 21
  • 1
  • 2
  • netstat -i can be used as well, but same is necessary as CJSewell mentioned, you have to take samples with a time diff and calculate. – MaQleod Mar 05 '15 at 17:04

2 Answers2

4

cat /proc/net/dev shows how many bytes have been transferred on each interface.

You could take a few samples and calculate the current kilobytes per second?

Here is a script I found and modified a little traffic.sh. Copy it to some where in your PATH and change permisions to 755 chmod 755 traffic.sh

Usage is

traffic.sh eth0

Prints out

 Transmitted: 5352K
 Received: 13M
 Total: 19M
 Sleeping 3 to calculate speed...
 Current speed: 690B/s
CJSewell
  • 131
  • 1
  • 6
  • Created a new version which outputs what you requested [netspeed.sh](https://gist.github.com/cjsewell/a139be3c6db581da521d) – CJSewell Mar 04 '15 at 19:32
0

Below is a script that works on most embedded Linux routers such as Ubiquiti and OpenWRT compatible ones and gets its details from /proc/net/dev.

(And easy to change to packets etc.)

#!/bin/sh

SLP=1 # output / sleep interval
DEVICE=$1
IS_GOOD=0
for GOOD_DEVICE in `grep \: /proc/net/dev | awk -F: '{print $1}'`; do
    if [ "$DEVICE" = $GOOD_DEVICE ]; then
        IS_GOOD=1
        break
    fi
done

if [ $IS_GOOD -eq 0 ]; then
    echo "Device not found. Should be one of these:"
        grep ":" /proc/net/dev | awk -F: '{print $1}' | sed s@\ @@g 
    exit 1
fi

while true; do

LINE=`grep $1 /proc/net/dev | sed s/.*://`;
RECEIVED1=`echo $LINE | awk '{print $1}'`
TRANSMITTED1=`echo $LINE | awk '{print $9}'`
TOTAL=$(($RECEIVED1+$TRANSMITTED1))

sleep $SLP

LINE=`grep $1 /proc/net/dev | sed s/.*://`;
RECEIVED2=`echo $LINE | awk '{print $1}'`
TRANSMITTED2=`echo $LINE | awk '{print $9}'`
SPEED=$((($RECEIVED2+$TRANSMITTED2-$TOTAL)/$SLP))
INSPEED=$((($RECEIVED2-$RECEIVED1)/$SLP))
OUTSPEED=$((($TRANSMITTED2-$TRANSMITTED1)/$SLP))

printf "In: %12i KB/s | Out: %12i KB/s | Total: %12i KB/s\n" $(($INSPEED/1024)) $(($OUTSPEED/1024)) $((($INSPEED+$OUTSPEED)/1024)) ;

done;

Copy the above to your clipboard and then in a terminal session on your router:

$ cat > /tmp/n.sh

then: Ctrl+V (or right click / Paste)

then: Ctrl+D

$ chmod +x /tmp/n.sh

$ /tmp/n.sh eth0

You can also paste it in a notepad, then just repeat the above if you need to edit it - not all embedded routers have an editor! Make sure you copy everything from the # at the top to the done; at the bottom.

You might have to change the /bin/bash to /bin/sh or vice versa. There is another example that uses /sys here: How to get real-time network statistics in Linux with KB/MB/Bytes format and for specific port or application processID?

Source: https://gist.github.com/dagelf/ab2bad26ce96fa8d79b0834cd8cab549

Dagelf
  • 901
  • 10
  • 18
  • Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by [voting to close it as a duplicate](https://superuser.com/help/privileges/close-questions) or, if you don't have enough reputation for that, [raise a flag](https://superuser.com/help/privileges/flag-posts) to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill May 23 '18 at 17:15
  • How is your comment relevant to the question at hand? Does SO not have a private message function? Does SO have a similar but not duplicate? If you took the time to read you would see that it is tailored. I posted it here because this comes up on another Google search and why post just a link when I can put the answer here? Rather fix SO. – Dagelf May 24 '18 at 19:51
  • The private message function is reserved for situations that require a moderator message - normally this is only used for behavior which requires suspension - which this does not. – DavidPostill May 24 '18 at 20:24