User:Tc23emp

From DD-WRT Wiki

Jump to: navigation, search

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