Skip to content

Instantly share code, notes, and snippets.

@sorz
Created Sep 4, 2013
Embed
What would you like to do?
Making the LED on router flash with traffic. http://sorz.org/flashled/
#!/bin/sh
#set -x
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
IFNAME="eth0"
FULLSPEED=1200 # KiB/s
LED='/sys/class/leds/tp-link:blue:system/brightness'
while [ True ]
do
# Get traffic data:
str=$(ifconfig $IFNAME | grep 'RX bytes')
str=${str#*'RX bytes:'}
str=${str%' ('*}
str=${str%' ('*}
now=$((str/1024))
speed=$((now-last))
if [ "$speed" -gt "$FULLSPEED" ] ; then
speed="$FULLSPEED"
fi
last="$now"
# Flash LED:
ratio=`echo "scale=3;$speed/$FULLSPEED*1000"|bc`
ontime=${ratio%'.'*}
offtime=$((1000-ontime))
#echo "$ontime"
if [ "$ontime" -gt "0" ] ; then
echo 255 > "$LED"
msleep "$ontime"
fi
if [ "$offtime" -gt "0" ] ; then
echo 0 > "$LED"
msleep "$offtime"
fi
#sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment