Use GeekTool to track network data transfer rates

Mar 27, '09 07:30:04AM

Contributed by: Anonymous

I use GeekTool to display some network information for my MacBook on my desktop. One thing I couldn't find was a way to show the current transfer rate (in Kb/sec) in and out. So I wrote a shell script that calls on netstat, waits one second, calls on netstat again, and then uses the difference in the numbers to display how many kilobytes were sent or received during that second.

If you use ethernet instead of AirPort, you'll have to change en1 to the interface you need (probably en0). This could also be used to monitor your bluetooth speeds as well. To set this up, first create a file (I named mine inout.sh) and save it wherever you like. Insert the following code and save it:

#!/bin/sh
# created by chris helming.
# chris dot helming at gmail

# get the current number of bytes in and bytes out
myvar1=`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'` #  bytes in
myvar3=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'` # bytes out

#wait one second
sleep 1

# get the number of bytes in and out one second later
myvar2=`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'` # bytes in again
myvar4=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'` # bytes out again

# find the difference between bytes in and out during that one second
subin=$(($myvar2 - $myvar1))
subout=$(($myvar4 - $myvar3))

# convert bytes to kilobytes
kbin=`echo "scale=2; $subin/1024;" | bc`
kbout=`echo "scale=2; $subout/1024;" | bc`

# print the results
echo "in: $kbin Kb/sec"
echo "out: $kbout Kb/sec"
After saving your file, switch to GeekTool, click the New Entry button, give it a name (Net Activity), and set the pop-up on the right to Shell. On the Command tab, set the command to something like this: sh /path/to/inout.sh, replacing /path/to/ with the path to the folder containing the inout.sh file. I left the Refresh setting at 10, and so GeekTool updates my Kb/sec transfer rates every 10 seconds. It can be changed to whatever you want, though.

[robg adds: I tested this, and it works as described.]

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090326125916351