Useful Scripts
From DD-WRT Wiki
(Difference between revisions)
Revision as of 19:53, 29 October 2006 (edit) Alexeen (Talk | contribs) (→GPIO Info for Linksys WRT) ← Previous diff |
Revision as of 05:44, 1 November 2006 (edit) (undo) JohnnyPrimus (Talk | contribs) (→WRT Scripts - - Arrange scripts such that the non-LED scripts appear properly in the TOC) Next diff → |
||
Line 111: | Line 111: | ||
</pre> | </pre> | ||
- | === | + | ===wlan.sh=== |
+ | * Uses front button LED to show WLAN state. Amber LED indicates one or more associated clients, white LED flashes when data is sent over WLAN. | ||
+ | <pre> | ||
+ | #!/bin/sh | ||
+ | I=`nvram get wl0_ifname` | ||
+ | |||
+ | while sleep 1; do | ||
+ | if [ "`wl assoclist`" != "" ]; then | ||
+ | XFER=`ifconfig $I|grep bytes` | ||
+ | if [ "$XFER" != "$PXFER" ]; then | ||
+ | LED='gpio disable 3 ; gpio disable 2' | ||
+ | PXFER=$XFER | ||
+ | else | ||
+ | LED='gpio disable 3 ; gpio enable 2' | ||
+ | fi | ||
+ | else | ||
+ | LED='gpio enable 3 ; gpio enable 2' | ||
+ | fi | ||
+ | |||
+ | if [ "$LED" != "$PLED" ]; then | ||
+ | eval $LED | ||
+ | PLED=$LED | ||
+ | fi | ||
+ | done | ||
+ | </pre> | ||
+ | |||
+ | ===wlan.sh - Buffalo Routers=== | ||
+ | * AOSS led to show WLAN is associated with clients. | ||
+ | * Bridge led flashes when data is transmitted over WLAN. | ||
+ | <pre> | ||
+ | #!/bin/sh | ||
+ | I=`nvram get wl0_ifname` | ||
+ | |||
+ | while sleep 1; do | ||
+ | if [ "`wl assoclist`" != "" ]; then | ||
+ | XFER=`ifconfig $I|grep bytes` | ||
+ | if [ "$XFER" != "$PXFER" ]; then | ||
+ | LED='gpio disable 1 ; gpio enable 1 ; gpio disable 6' | ||
+ | PXFER=$XFER | ||
+ | else | ||
+ | LED='gpio disable 6' | ||
+ | fi | ||
+ | else | ||
+ | LED='gpio enable 6' | ||
+ | fi | ||
+ | |||
+ | if [ "$LED" != "$PLED" ]; then | ||
+ | eval $LED | ||
+ | PLED=$LED | ||
+ | fi | ||
+ | done | ||
+ | </pre> | ||
+ | |||
+ | ===scan.sh=== | ||
+ | <pre> | ||
+ | BEGIN{ | ||
+ | command = "wl scan 2> /dev/null ; wl scanresults 2> /dev/null"; | ||
+ | red = "\x1b[31m"; green = "\x1b[32m"; | ||
+ | greenback="\x1b[42m"; yellow = "\x1b[33m"; | ||
+ | cyan = "\x1b[36m"; blue = "\x1b[34m"; | ||
+ | blueback = "\x1b[44m"; white = "\x1b[37m"; | ||
+ | whiteback = "\x1b[47m"; reset = "\x1b[0m"; | ||
+ | underscore = "\x1b[4m"; clear = "\x1b[2J"; | ||
+ | home = "\x1b[0;0H"; erase2end = "\x1b[K"; | ||
+ | cName = white; cSignal = green; | ||
+ | cNoise = red; cCaps = green; | ||
+ | cStrengthLow = blue blueback; cChannel = green; | ||
+ | cStrengthMed = white whiteback; | ||
+ | cStrengthHi = green greenback; | ||
+ | cStrengthAged = red; | ||
+ | |||
+ | print clear; | ||
+ | for(;;) | ||
+ | { | ||
+ | while (command|getline) | ||
+ | { | ||
+ | if(/^SSID/) {cn = $2; name[cn] = cn; rssi[cn] = $6;noise[cn]= $9} | ||
+ | if(/^Mode/) {rssi[cn] = $4;noise[cn]= $7; channel[cn] = $10 } | ||
+ | if(/^BSSID/) {caps[cn] = $4" "$5" "$6" "$7" "$8" "$9" "$10 } | ||
+ | } | ||
+ | close(command) | ||
+ | printf home; | ||
+ | ln = 0; | ||
+ | print white " Name Signal Noise Channel Type"; | ||
+ | for (x in name) | ||
+ | { | ||
+ | sigstrength = ((rssi[x] - noise[x])*1.5) + ((rssi[x] +90)*1.5); | ||
+ | if (sigstrength 4) cStrength = cStrengthMed; | ||
+ | if(sigstrength>7) cStrength = cStrengthHi; | ||
+ | if(age[x]=0) cStrength = cStrengthAged; | ||
+ | |||
+ | fmt = "%s%-15s %s%0"sigstrength"d "reset erase2end "\n %s%-4d %s%-4d %s%-4d %s%2s %s%10s " reset erase2end "\n" erase2end "\n"; | ||
+ | printf fmt, cName,name[x],cStrength,0,cSignal,rssi[x],cNoise,noise[x],cChannel, channel[x],cCaps,caps[x]; | ||
+ | rssi[x] = -100; | ||
+ | ln++; | ||
+ | } | ||
+ | print erase2end; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | ==ssh_users.sh== | ||
* Displays when someone is connected using SSH. | * Displays when someone is connected using SSH. | ||
Remove lines starting with '# ' (hash + blank). | Remove lines starting with '# ' (hash + blank). | ||
Line 142: | Line 244: | ||
</pre> | </pre> | ||
- | + | ==wake.sh== | |
* Enables you to power on a LAN computer by name instead of IP address/MAC, based on DHCP lease table (mandatory).<br>Usage: /path/to/wake.sh <''hostname''><br>(Default hostname is ''desktop'') | * Enables you to power on a LAN computer by name instead of IP address/MAC, based on DHCP lease table (mandatory).<br>Usage: /path/to/wake.sh <''hostname''><br>(Default hostname is ''desktop'') | ||
Line 178: | Line 280: | ||
</pre> | </pre> | ||
- | + | ==always_on.sh== | |
* Pings your default gateway every time and force a DHCP renew if no packets are received.<br>Usage: /path/to/always_on.sh & | * Pings your default gateway every time and force a DHCP renew if no packets are received.<br>Usage: /path/to/always_on.sh & | ||
<pre> | <pre> | ||
Line 214: | Line 316: | ||
fi | fi | ||
done | done | ||
- | </pre> | ||
- | |||
- | ===wlan.sh=== | ||
- | * Uses front button LED to show WLAN state. Amber LED indicates one or more associated clients, white LED flashes when data is sent over WLAN. | ||
- | <pre> | ||
- | #!/bin/sh | ||
- | I=`nvram get wl0_ifname` | ||
- | |||
- | while sleep 1; do | ||
- | if [ "`wl assoclist`" != "" ]; then | ||
- | XFER=`ifconfig $I|grep bytes` | ||
- | if [ "$XFER" != "$PXFER" ]; then | ||
- | LED='gpio disable 3 ; gpio disable 2' | ||
- | PXFER=$XFER | ||
- | else | ||
- | LED='gpio disable 3 ; gpio enable 2' | ||
- | fi | ||
- | else | ||
- | LED='gpio enable 3 ; gpio enable 2' | ||
- | fi | ||
- | |||
- | if [ "$LED" != "$PLED" ]; then | ||
- | eval $LED | ||
- | PLED=$LED | ||
- | fi | ||
- | done | ||
- | </pre> | ||
- | |||
- | ===wlan.sh - Buffalo Routers=== | ||
- | * AOSS led to show WLAN is associated with clients. | ||
- | * Bridge led flashes when data is transmitted over WLAN. | ||
- | <pre> | ||
- | #!/bin/sh | ||
- | I=`nvram get wl0_ifname` | ||
- | |||
- | while sleep 1; do | ||
- | if [ "`wl assoclist`" != "" ]; then | ||
- | XFER=`ifconfig $I|grep bytes` | ||
- | if [ "$XFER" != "$PXFER" ]; then | ||
- | LED='gpio disable 1 ; gpio enable 1 ; gpio disable 6' | ||
- | PXFER=$XFER | ||
- | else | ||
- | LED='gpio disable 6' | ||
- | fi | ||
- | else | ||
- | LED='gpio enable 6' | ||
- | fi | ||
- | |||
- | if [ "$LED" != "$PLED" ]; then | ||
- | eval $LED | ||
- | PLED=$LED | ||
- | fi | ||
- | done | ||
- | </pre> | ||
- | |||
- | ===scan.sh=== | ||
- | <pre> | ||
- | BEGIN{ | ||
- | command = "wl scan 2> /dev/null ; wl scanresults 2> /dev/null"; | ||
- | red = "\x1b[31m"; green = "\x1b[32m"; | ||
- | greenback="\x1b[42m"; yellow = "\x1b[33m"; | ||
- | cyan = "\x1b[36m"; blue = "\x1b[34m"; | ||
- | blueback = "\x1b[44m"; white = "\x1b[37m"; | ||
- | whiteback = "\x1b[47m"; reset = "\x1b[0m"; | ||
- | underscore = "\x1b[4m"; clear = "\x1b[2J"; | ||
- | home = "\x1b[0;0H"; erase2end = "\x1b[K"; | ||
- | cName = white; cSignal = green; | ||
- | cNoise = red; cCaps = green; | ||
- | cStrengthLow = blue blueback; cChannel = green; | ||
- | cStrengthMed = white whiteback; | ||
- | cStrengthHi = green greenback; | ||
- | cStrengthAged = red; | ||
- | |||
- | print clear; | ||
- | for(;;) | ||
- | { | ||
- | while (command|getline) | ||
- | { | ||
- | if(/^SSID/) {cn = $2; name[cn] = cn; rssi[cn] = $6;noise[cn]= $9} | ||
- | if(/^Mode/) {rssi[cn] = $4;noise[cn]= $7; channel[cn] = $10 } | ||
- | if(/^BSSID/) {caps[cn] = $4" "$5" "$6" "$7" "$8" "$9" "$10 } | ||
- | } | ||
- | close(command) | ||
- | printf home; | ||
- | ln = 0; | ||
- | print white " Name Signal Noise Channel Type"; | ||
- | for (x in name) | ||
- | { | ||
- | sigstrength = ((rssi[x] - noise[x])*1.5) + ((rssi[x] +90)*1.5); | ||
- | if (sigstrength 4) cStrength = cStrengthMed; | ||
- | if(sigstrength>7) cStrength = cStrengthHi; | ||
- | if(age[x]=0) cStrength = cStrengthAged; | ||
- | |||
- | fmt = "%s%-15s %s%0"sigstrength"d "reset erase2end "\n %s%-4d %s%-4d %s%-4d %s%2s %s%10s " reset erase2end "\n" erase2end "\n"; | ||
- | printf fmt, cName,name[x],cStrength,0,cSignal,rssi[x],cNoise,noise[x],cChannel, channel[x],cCaps,caps[x]; | ||
- | rssi[x] = -100; | ||
- | ln++; | ||
- | } | ||
- | print erase2end; | ||
- | } | ||
- | } | ||
</pre> | </pre> | ||
Revision as of 05:44, 1 November 2006
Contents |
WRT Scripts
This page is intended to house useful scripts that help monitor or enhance the features of your DD-WRT capable router.
The example scripts are written using the GPIO info for the Linksys WRT. Remember to change them for whatever router you have.
See Startup Scripts to run scripts on router startup.
LED Scripts
GPIO Info for Linksys WRT
Pin Direction Name Use GPIO 0 Output WLAN LED (LED - Wireless) GPIO 1 Output POWER LED (LED - Power) GPIO 2 Output ADM_EECS (LED - White, Cisco Button V3.0+) GPIO 3 Output ADM_EESK (LED - Amber, Cisco Button V3.0+) GPIO 4 Input ADM_EEDO (Button - Cisco Button V3.0+) GPIO 5 Output ADM_EEDI (Unknown) Seems to cycle all LED colors disabled. GPIO 6 Input RESET (Button - Reset Button) GPIO 7 Output DMZ LED (LED - DMZ)
GPIO Info for Linksys WRTSL54GS
This is for the WRTSL54GS model only.
Pin Direction Name Use GPIO 5 OUTPUT SES LED (Cisco White LED) GPIO 7 OUTPUT SES LED (Cisco AMBER LED)
GPIO Info for Buffalo WHR
Pin Direction Use GPIO 0 Input AOSS Button GPIO 1 Output Bridge LED GPIO 2 Output WLAN LED GPIO 3 Output Extra LED between bridge and WLAN GPIO 4 Input Reset Button GPIO 5 Input Bridge/Auto Switch GPIO 6 Output AOSS LED GPIO 7 Output DIAG LED GPIO 8 n/a Unkown/None GPIO 9 Output Power LED
load.sh
- Uses front button LED to display current load on router.
#!/bin/sh gpio="gpio" amber=3 white=2 delay=3 meltdown=400 overload=100 highload=70 medload=30 while sleep $delay do set -- $(cat /proc/loadavg) load="${1%.*}${1#*.}" if [ $load -gt $meltdown ] then $gpio disable $amber usleep 50000 $gpio disable $white usleep 50000 reboot elif [ $load -gt $overload ] then $gpio disable $amber usleep 50000 elif [ $load -gt $highload ] then $gpio disable $amber usleep 12500 $gpio enable $amber usleep 12500 $gpio disable $amber usleep 12500 $gpio enable $amber usleep 12500 $gpio disable $amber usleep 12500 $gpio enable $amber usleep 12500 elif [ $load -gt $medload ] then $gpio enable $amber $gpio disable $white usleep 25000 $gpio enable $white usleep 25000 $gpio disable $white usleep 25000 $gpio enable $white usleep 25000 else $gpio disable $white usleep 50000 $gpio enable $white usleep 50000 fi done
wlan.sh
- Uses front button LED to show WLAN state. Amber LED indicates one or more associated clients, white LED flashes when data is sent over WLAN.
#!/bin/sh I=`nvram get wl0_ifname` while sleep 1; do if [ "`wl assoclist`" != "" ]; then XFER=`ifconfig $I|grep bytes` if [ "$XFER" != "$PXFER" ]; then LED='gpio disable 3 ; gpio disable 2' PXFER=$XFER else LED='gpio disable 3 ; gpio enable 2' fi else LED='gpio enable 3 ; gpio enable 2' fi if [ "$LED" != "$PLED" ]; then eval $LED PLED=$LED fi done
wlan.sh - Buffalo Routers
- AOSS led to show WLAN is associated with clients.
- Bridge led flashes when data is transmitted over WLAN.
#!/bin/sh I=`nvram get wl0_ifname` while sleep 1; do if [ "`wl assoclist`" != "" ]; then XFER=`ifconfig $I|grep bytes` if [ "$XFER" != "$PXFER" ]; then LED='gpio disable 1 ; gpio enable 1 ; gpio disable 6' PXFER=$XFER else LED='gpio disable 6' fi else LED='gpio enable 6' fi if [ "$LED" != "$PLED" ]; then eval $LED PLED=$LED fi done
scan.sh
BEGIN{ command = "wl scan 2> /dev/null ; wl scanresults 2> /dev/null"; red = "\x1b[31m"; green = "\x1b[32m"; greenback="\x1b[42m"; yellow = "\x1b[33m"; cyan = "\x1b[36m"; blue = "\x1b[34m"; blueback = "\x1b[44m"; white = "\x1b[37m"; whiteback = "\x1b[47m"; reset = "\x1b[0m"; underscore = "\x1b[4m"; clear = "\x1b[2J"; home = "\x1b[0;0H"; erase2end = "\x1b[K"; cName = white; cSignal = green; cNoise = red; cCaps = green; cStrengthLow = blue blueback; cChannel = green; cStrengthMed = white whiteback; cStrengthHi = green greenback; cStrengthAged = red; print clear; for(;;) { while (command|getline) { if(/^SSID/) {cn = $2; name[cn] = cn; rssi[cn] = $6;noise[cn]= $9} if(/^Mode/) {rssi[cn] = $4;noise[cn]= $7; channel[cn] = $10 } if(/^BSSID/) {caps[cn] = $4" "$5" "$6" "$7" "$8" "$9" "$10 } } close(command) printf home; ln = 0; print white " Name Signal Noise Channel Type"; for (x in name) { sigstrength = ((rssi[x] - noise[x])*1.5) + ((rssi[x] +90)*1.5); if (sigstrength 4) cStrength = cStrengthMed; if(sigstrength>7) cStrength = cStrengthHi; if(age[x]=0) cStrength = cStrengthAged; fmt = "%s%-15s %s%0"sigstrength"d "reset erase2end "\n %s%-4d %s%-4d %s%-4d %s%2s %s%10s " reset erase2end "\n" erase2end "\n"; printf fmt, cName,name[x],cStrength,0,cSignal,rssi[x],cNoise,noise[x],cChannel, channel[x],cCaps,caps[x]; rssi[x] = -100; ln++; } print erase2end; } }
ssh_users.sh
- Displays when someone is connected using SSH.
Remove lines starting with '# ' (hash + blank).
#!/bin/sh led=2 interval=5 on=0 /sbin/gpio enable $led while sleep $interval do # Make sure we get local port 22 and not any port starting with 22: users=$(/bin/netstat -n | /usr/bin/awk '$4~/:22$/ {++x}; END {print x+0}') if [ $users -gt 0 ]; then if [ $on -eq 0 ]; then /sbin/gpio disable $led on=1 fi else if [ $on -eq 1 ]; then /sbin/gpio enable $led on=0 fi fi done
wake.sh
- Enables you to power on a LAN computer by name instead of IP address/MAC, based on DHCP lease table (mandatory).
Usage: /path/to/wake.sh <hostname>
(Default hostname is desktop)
STATION=mm WOL=/usr/sbin/wol STATICS=/tmp/udhcpd.statics DEV=br0 if [ -n "$1" ]; then STATION=$1 fi while read LINE do IP=`echo $LINE | awk '{print $1}'` MAC=`echo $LINE | awk '{print $2}'` FOUND=`ip neigh | grep "$IP.*REACHABLE"` if [ -z "$FOUND" ]; then echo Creating ARP entry for $IP $MAC ip neigh add $IP lladdr $MAC dev $DEV nud reachable 2> /dev/null ip neigh change $IP lladdr $MAC dev $DEV nud reachable 2> /dev/null fi done < $STATICS LEASE=`cat $STATICS | grep "\b$STATION\b$"` if [ -n "$LEASE" ]; then IP=`echo $LEASE | awk '{print $1}'` MAC=`echo $LEASE | awk '{print $2}'` $WOL -i $IP $MAC else echo Unable to find \"$STATION\" in DHCP static file $STATICS, please use \"$0 \<hostname\>\" fi
always_on.sh
- Pings your default gateway every time and force a DHCP renew if no packets are received.
Usage: /path/to/always_on.sh &
#!/bin/sh INTERVAL=10 PACKETS=1 UDHCPC="udhcpc -i vlan1 -p /var/run/udhcpc.pid -s /tmp/udhcpc" IFACE=vlan1 ME=`basename $0` RUNNING=`ps | grep $ME | wc -l` if [ "$RUNNING" -gt 3 ]; then echo "Another instance of \"$ME\" is running" exit fi while sleep $INTERVAL do TARGET=`ip route | awk '/default via/ {print $3}'` RET=`ping -c $PACKETS $TARGET 2> /dev/null | awk '/packets received/ {print $4}'` if [ "$RET" -ne "$PACKETS" ]; then echo Ping failed, releasing IP address: $IFACE #send a RELEASE signal kill -USR2 `cat /var/run/udhcpc.pid` 2> /dev/null #ensure udhcpc is not running killall udhcpc 2> /dev/null echo Renewing IP address: $IFACE $UDHCPC echo Waiting 10 s ... sleep 10 else echo Network is up via $TARGET fi done
Links
Startup Scripts
Script Examples