11

Displays in and out bandwidth of your network interfaces. You must also copy the included shell script.

Defaults to en0, the hardwired network interface on (most) macbooks. Invoke with en1 to change to the wireless interface.

Example

network_speed en0 en1
en0 in:  31.48 kbps
en0 out: 1.08 kbps

en1 in:  0.00 kbps
en1 out: 0.00 kbps

The script

#!/bin/zsh
PATH=/usr/sbin:/usr/bin:/bin

seconds=$1
if [ -z "$seconds" ]; then
  seconds=15
else
  shift
fi

divisor=`expr $seconds * 1024`
[ $divisor = 0 ] && divisor=1

[ -z "$*" ] && set "en0"

initial_bytes_in=()
initial_bytes_out=()
subsequent_bytes_in=()
subsequent_bytes_out=()

for interface in $*; do
   b=`netstat -ibI $interface | awk '$3 ~ /Link/ { print $7, $10 }'`
   if [ -z "$b" ]; then
      initial_bytes_in+=(0)
      initial_bytes_out+=(0)
   else
      initial_bytes_in+=(`echo $b | cut -f 1 -d " "` )
      initial_bytes_out+=(`echo $b | cut -f 2 -d " "` )
   fi
   unset b
done

sleep $seconds

for interface in $*; do
   b=`netstat -ibI $interface | awk '$3 ~ /Link/ { print $7, $10 }'`
   if [ -z "$b" ]; then
      subsequent_bytes_in+=(0)
      subsequent_bytes_out+=(0)
   else
      subsequent_bytes_in+=(`echo $b | cut -f 1 -d " "` )
      subsequent_bytes_out+=(`echo $b | cut -f 2 -d " "` )
   fi
   unset b
done

for i in {1..$#*}; do
   printf "$*[$i] in:  %04.2f kbpsn" `echo "scale=2; ($subsequent_bytes_in[$i] - $initial_bytes_in[$i])/$divisor" | bc 2>/dev/null`
   printf "$*[$i] out: %04.2f kbpsn" `echo "scale=2; ($subsequent_bytes_out[$i] - $initial_bytes_out[$i])/$divisor" | bc 2>/dev/null`
done

Geeklet files to download

Comments

User Avatar
maelcum 800 days ago
This is a shorter version which works for all interfaces (change the "en0" to anything you want):

Just copy and paste into GeekTool/NerdTool. No need for an external script that is called:

INTERFACE=en0;

SAMPLE_A=(`/usr/sbin/netstat -ib | awk "/$INTERFACE/"'{print $7" "$10; exit}'`);

sleep 1;

SAMPLE_B=(`/usr/sbin/netstat -ib | awk "/$INTERFACE/"'{print $7" "$10; exit}'`);

BANDWIDTH_USAGE=(`echo "2k ${SAMPLE_B[0]} ${SAMPLE_A[0]} - 1024 / p" "${SAMPLE_B[1]} ${SAMPLE_A[1]} - 1024 / p" | dc`);echo " in: ${BANDWIDTH_USAGE[0]} Kb/secnout: ${BANDWIDTH_USAGE[1]} Kb/sec";
User Avatar
llorban 722 days ago
Maelcum works in Snow Leopard, but the other one didn't work.
User Avatar
Hakeemas 407 days ago
how do I add a new line after the 'in'
User Avatar
cycle4passion 129 days ago
neither works in lion, looks for a working Lion version please!
User Avatar
hillscottc 109 days ago
The other one works, here.... http://www.macosxtips.co.uk/geeklets/system/current-network-transfer-rate-in-and-out/
User Avatar
hillscottc 109 days ago
Correction, I can't get the other to work, either. *grumble* Getting no output, right?
User Avatar
sachman 81 days ago
It works in Lion like this:

INTERFACE=en0;

SAMPLE_A=(`/usr/sbin/netstat -bI "$INTERFACE" | awk "/$INTERFACE/"'{print $7" "$10; exit}'`);

sleep 1;

SAMPLE_B=(`/usr/sbin/netstat -bI "$INTERFACE" | awk "/$INTERFACE/"'{print $7" "$10; exit}'`);

BANDWIDTH_USAGE=(`echo "2k ${SAMPLE_B[0]} ${SAMPLE_A[0]} - 1024 / p" "${SAMPLE_B[1]} ${SAMPLE_A[1]} - 1024 / p" | dc`);echo " in: ${BANDWIDTH_USAGE[0]} KBps out: ${BANDWIDTH_USAGE[1]} KBps";

On the other hand, does anyone know how to change the units to kilo bits per second?

Log in to comment or register here.