User:Tc23emp
From DD-WRT Wiki
(Difference between revisions)
Revision as of 17:13, 30 August 2010 (edit) Tc23emp (Talk | contribs) (Use cron_jobs over startup crontab) ← Previous diff |
Current revision (18:15, 13 April 2011) (edit) (undo) Tc23emp (Talk | contribs) m (wlclient explicit watch) |
||
(3 intermediate revisions not shown.) | |||
Line 1: | Line 1: | ||
- | These scripts can be added via the web interface. | + | These scripts can be added via the web interface.<br> |
Tested on DD-WRT v24-sp2 (08/12/10) std-nokaid-small (SVN revision 14929) | Tested on DD-WRT v24-sp2 (08/12/10) std-nokaid-small (SVN revision 14929) | ||
- | Administration -> Management -> | + | Administration -> Management -><br> |
- | Enable Cron | + | Enable Cron = true<br> |
Additional Cron Jobs: | Additional Cron Jobs: | ||
<pre> | <pre> | ||
Line 10: | Line 10: | ||
</pre> | </pre> | ||
- | Administration -> Commands -> Custom Script | + | Administration -> Commands -> Custom Script: |
<pre> | <pre> | ||
#!/bin/sh | #!/bin/sh | ||
Line 25: | Line 25: | ||
#Parse the previous day and monthly totals and send an email | #Parse the previous day and monthly totals and send an email | ||
aff="aff" #keyword workaround | aff="aff" #keyword workaround | ||
- | | + | yday=$(date -D %s -d $(( $(date +%s) - 86400)) +%d) |
- | + | ymon=$(date -D %s -d $(( $(date +%s) - 86400)) +tr$aff-%m-%Y) | |
+ | if [ $(date +%d) -eq 1 ]; then monmsg="Last Month"; else monmsg="Month to Date"; fi | ||
+ | msg=$(nvram get $ymon | awk '{print $'$yday', $NF}' | sed 's/\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)]/Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for '"${monmsg}"'\nIncoming: \3 MB\nOutgoing: \4 MB\n/') | ||
fnc_mail "Bandwidth Report" "$msg" "/tmp/bwmail.log" | fnc_mail "Bandwidth Report" "$msg" "/tmp/bwmail.log" | ||
fi | fi | ||
if [ "$1" = "wlclient" ]; then | if [ "$1" = "wlclient" ]; then | ||
- | #Send | + | # Send an e-mail whenever a new wireless client connects, remember them only until the next router reboot (if list is not stored on persistent filesystem) |
- | #Uncomment the following line and edit the list MAC addresses you | + | ### wlclient settings ### |
+ | ## Make sure that the line where change a setting is uncommented (no leading pound sign {#}) | ||
+ | ## Specify whether you want to remove a client from the previously connected list if they are disconnected when the script checks, and | ||
+ | ## be notified again when they reconnect. (unset / 0 = false, 1 = true) | ||
+ | #flushdisc=1 | ||
+ | |||
+ | ## Only use one of the following two settings, either a macignore list or an explicit macwatch list. macwatch makes macignore useless | ||
+ | ## Uncomment the following line and edit the list of MAC addresses you never want to be notified about (seperated by spaces) | ||
#macignore="0A:1B:2C:3D:4E:5F" | #macignore="0A:1B:2C:3D:4E:5F" | ||
+ | |||
+ | ## Uncomment the following line and edit the list of the only MAC addresses that you want to be notified about (seperated by spaces) | ||
+ | #macwatch="0A:1B:2C:3D:4E:5F" | ||
+ | |||
+ | ### wlclient settings end ### | ||
+ | |||
+ | # /tmp/wlclient.lst is a list of clients what you were already notified about or that you are ignoring | ||
for mac in $macignore; do | for mac in $macignore; do | ||
grep -q $mac /tmp/wlclient.lst | grep -q $mac /tmp/wlclient.lst | ||
Line 40: | Line 56: | ||
fi | fi | ||
done | done | ||
+ | |||
maclist=$(wl -i $(nvram get wl0_ifname) assoclist | cut -d" " -f2) | maclist=$(wl -i $(nvram get wl0_ifname) assoclist | cut -d" " -f2) | ||
for mac in $maclist; do | for mac in $maclist; do | ||
+ | if [ -n "$macwatch" ]; then | ||
+ | echo $macwatch | grep -q $mac | ||
+ | if [ $? -gt 0 ]; then | ||
+ | continue # to next mac if watching other MACs only | ||
+ | fi | ||
+ | fi | ||
+ | |||
grep -q $mac /tmp/wlclient.lst | grep -q $mac /tmp/wlclient.lst | ||
if [ $? -gt 0 ]; then | if [ $? -gt 0 ]; then | ||
+ | # append to already notified list and formatted message | ||
echo $mac >> /tmp/wlclient.lst | echo $mac >> /tmp/wlclient.lst | ||
inf=$(arp | grep $mac) | inf=$(arp | grep $mac) | ||
Line 53: | Line 78: | ||
fi | fi | ||
done | done | ||
- | | + | |
- | + | if [ -n "$flushdisc" ]; then | |
+ | echo "$maclist" | tr ' ' '\n' > /tmp/wlclient.lst # only store currently connected clients, disconnected clients are flushed | ||
+ | fi | ||
+ | |||
if [ -n "$msg" ]; then | if [ -n "$msg" ]; then | ||
msg=$(echo -e "$msg") | msg=$(echo -e "$msg") | ||
fnc_mail "New Wireless Client(s)" "$msg" | fnc_mail "New Wireless Client(s)" "$msg" | ||
fi | fi | ||
- | fi | + | fi # wlclient end |
</pre> | </pre> |
Current revision
These scripts can be added via the web interface.
Tested on DD-WRT v24-sp2 (08/12/10) std-nokaid-small (SVN revision 14929)
Administration -> Management ->
Enable Cron = true
Additional Cron Jobs:
5 0 * * * root /tmp/custom.sh bwmail */5 * * * * root /tmp/custom.sh wlclient
Administration -> Commands -> Custom Script:
#!/bin/sh fnc_mail() { subj="$1" msg="$2" if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastmail.log"; else logfile="$3"; fi sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"$subj" -m"$msg" me@gmail.com > $logfile 2>&1 } if [ "$1" = "bwmail" ]; then #Parse the previous day and monthly totals and send an email aff="aff" #keyword workaround yday=$(date -D %s -d $(( $(date +%s) - 86400)) +%d) ymon=$(date -D %s -d $(( $(date +%s) - 86400)) +tr$aff-%m-%Y) if [ $(date +%d) -eq 1 ]; then monmsg="Last Month"; else monmsg="Month to Date"; fi msg=$(nvram get $ymon | awk '{print $'$yday', $NF}' | sed 's/\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)]/Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for '"${monmsg}"'\nIncoming: \3 MB\nOutgoing: \4 MB\n/') fnc_mail "Bandwidth Report" "$msg" "/tmp/bwmail.log" fi if [ "$1" = "wlclient" ]; then # Send an e-mail whenever a new wireless client connects, remember them only until the next router reboot (if list is not stored on persistent filesystem) ### wlclient settings ### ## Make sure that the line where change a setting is uncommented (no leading pound sign {#}) ## Specify whether you want to remove a client from the previously connected list if they are disconnected when the script checks, and ## be notified again when they reconnect. (unset / 0 = false, 1 = true) #flushdisc=1 ## Only use one of the following two settings, either a macignore list or an explicit macwatch list. macwatch makes macignore useless ## Uncomment the following line and edit the list of MAC addresses you never want to be notified about (seperated by spaces) #macignore="0A:1B:2C:3D:4E:5F" ## Uncomment the following line and edit the list of the only MAC addresses that you want to be notified about (seperated by spaces) #macwatch="0A:1B:2C:3D:4E:5F" ### wlclient settings end ### # /tmp/wlclient.lst is a list of clients what you were already notified about or that you are ignoring for mac in $macignore; do grep -q $mac /tmp/wlclient.lst if [ $? -gt 0 ]; then echo $mac >> /tmp/wlclient.lst fi done maclist=$(wl -i $(nvram get wl0_ifname) assoclist | cut -d" " -f2) for mac in $maclist; do if [ -n "$macwatch" ]; then echo $macwatch | grep -q $mac if [ $? -gt 0 ]; then continue # to next mac if watching other MACs only fi fi grep -q $mac /tmp/wlclient.lst if [ $? -gt 0 ]; then # append to already notified list and formatted message echo $mac >> /tmp/wlclient.lst inf=$(arp | grep $mac) if [ $? -gt 0 ]; then msg="$msg""Unknown at ${mac}\n" else msg="$msg""${inf}\n" fi fi done if [ -n "$flushdisc" ]; then echo "$maclist" | tr ' ' '\n' > /tmp/wlclient.lst # only store currently connected clients, disconnected clients are flushed fi if [ -n "$msg" ]; then msg=$(echo -e "$msg") fnc_mail "New Wireless Client(s)" "$msg" fi fi # wlclient end