Advertisement
Guest User

DD-WRT presence rechecker

a guest
Dec 25th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.54 KB | None | 0 0
  1. #!/bin/sh
  2. ###############################################################
  3. # Proximity detection
  4. #
  5. # A script designed to run on a router running DD-WRT to detect certain devices connected to the router.
  6. # It runs at startup and runs continually, checking for a specific list of devices (phones/laptop, etc)
  7. # that connect wirelessly to the router.  Once a device is connected, the OpenHAB status will
  8. # be updated with either an ON or OFF.  Make sure you set up a switch item in OpenHAB for each device
  9. # you want to track.
  10. #
  11. # The searching frequency can be adjusted to be slower/faster depending on your requirements. Searching too fast
  12. # could burden your router.  Too slow might not update the status as necessary for the application.
  13. #
  14.  
  15.    
  16. # Make changes below
  17. # MAC address of each device to watch. Don't leave blank.
  18. # For security purposes, if your router requires a password, even if someone could clone the MAC of your
  19. # phone, they would still require the password of your network to link to your router.
  20. macdevice1="00:00:00:00:00:00"    #Aaron Phone
  21. macdevice2="00:00:00:00:00:00"    #Device 2
  22. macdevice3="00:00:00:00:00:00"    #Device 3
  23. macdevice4="00:00:00:00:00:00"    #Device 4
  24.  
  25. #OpenHAB username, password, and IP Address
  26. username="OPENHAB_USERNAME"
  27. password="OPENHAB_PASSWORD"
  28. IPAddr="OPENHAB_IP_ADDRESS"
  29. port="OPENHAB_PORT"
  30.  
  31.  
  32. # OpenHAB switch items to be updated for each tracked MAC
  33. item1="aaronPhone"
  34. item2="DEVICE_2"
  35. item3="DEVICE_3"
  36. item4="DEVICE_4"
  37.  
  38.  
  39. # Occupied and unoccupied delay in seconds to check status
  40. # Adjust for shorter/longer wait times.  For instance, when one device is already
  41. # connected, you might want to check less frequently.  This could also delay the
  42. # notification of a disconnect.
  43. delay_occupied=4
  44. delay_unoccupied=2
  45.  
  46. # initial testing loop count - uncomment the counter near the bottom of the script for testing only.
  47. limit=120
  48.  
  49. ###############################################
  50. # do not change below here
  51. ###############################################
  52.  
  53. sleep
  54. #initialize internal variables
  55.  
  56. # status of each MAC. 0=disconnected. 1=connected.  -1 initially forces isy update first loop
  57. macconnected1=-1
  58. macconnected2=-1
  59. macconnected3=-1
  60. macconnected4=-1
  61. connected=-1
  62. # total number of currently conencted devices.  
  63. currentconnected=0
  64. counter=1
  65.  
  66. recheck_counter=0
  67. recheck_done=0
  68. # Initial testing loop.  Will run continually after testing is complete
  69. while [ $counter -lt $limit ]; do
  70.  
  71. if [ recheck_done -ne 1 ]; then
  72.     if [ $reset_count -eq 100 ]; then
  73.         recheck_counter=recheck_counter+1;
  74.     else
  75.         # after 100 loops, recheck the status of everything, then assume it will work from there
  76.         recheck_done=1;
  77.         macconnected1=-1
  78.         macconnected2=-1
  79.         macconnected3=-1
  80.         macconnected4=-1
  81.     fi
  82. fi
  83.  
  84.  
  85. #maclist stored mac listing in router from status screen
  86. maclist=$(wl_atheros -i ath0 assoclist | cut -d" " -f2)
  87.  
  88. #reset current status. Two variables are used for each device.  The past known status and the current
  89. # status.  Only a change is reported to the ISY.  Otherwise, it would constantly be updating the ISY with
  90. # the current status creating unnecessary traffic for both the router and the ISY
  91. maccurrent1=0;
  92. maccurrent2=0;
  93. maccurrent3=0;
  94. maccurrent4=0;
  95.  
  96.  
  97. # compare each device that is currently connected to the MAC devices we want to watch.
  98. for mac in $maclist; do
  99. case $mac in
  100.    "$macdevice1") maccurrent1=1;;
  101.    "$macdevice2") maccurrent2=1;;
  102.    "$macdevice3") maccurrent3=1;;
  103.    "$macdevice4") maccurrent4=1;;
  104. esac
  105. done
  106.  
  107. # Look for a change in status from the old known to the current status.
  108. # If it changed, update the ISY. Otherwise it leaves it as is.
  109. if [ $macconnected1 -ne $maccurrent1 ]; then
  110.      if [ $maccurrent1 -eq 1 ]; then
  111.          macstatus1="ON";
  112.      else
  113.          macstatus1="OFF";
  114.      fi
  115.      curl -X POST -d $macstatus1 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item1
  116. fi
  117.  
  118. if [ $macconnected2 -ne $maccurrent2 ]; then
  119.      if [ $maccurrent2 -eq 1 ]; then
  120.          macstatus2="ON";
  121.      else
  122.          macstatus2="OFF";
  123.      fi
  124.      curl -X POST -d $macstatus2 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item2
  125. fi
  126.  
  127. if [ $macconnected3 -ne $maccurrent3 ]; then
  128.      if [ $maccurrent3 -eq 1 ]; then
  129.          macstatus3="ON";
  130.      else
  131.          macstatus3="OFF";
  132.      fi
  133.      curl -X POST -d $macstatus3 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item3
  134. fi
  135.    
  136. if [ $macconnected4 -ne $maccurrent4 ]; then
  137.      if [ $maccurrent4 -eq 1 ]; then
  138.          macstatus4="ON";
  139.      else
  140.          macstatus4="OFF";
  141.      fi
  142.      curl -X POST -d $macstatus4 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item4
  143. fi
  144.  
  145. # Update the known status from the current.  Ready for the next loop.
  146. macconnected1=$maccurrent1;
  147. macconnected2=$maccurrent2;
  148. macconnected3=$maccurrent3;
  149. macconnected4=$maccurrent4;
  150.  
  151. # Total up the number of devices connected.
  152. let currentconnected=$macconnected1+$macconnected2+$macconnected3+$macconnected4
  153.  
  154. connected=$currentconnected
  155.  
  156. # Delay (sleep) depending on the connection status.
  157. # No devices connected could delay less.  Once a device is connected, it could delay longer.
  158. if [ $connected -gt 0 ]; then
  159.     sleep $delay_occupied
  160.     else
  161.     sleep $delay_occupied
  162. fi
  163.  
  164. #for testing only - uncomment to have the looping stop at X loops defined in variable:  limit.
  165. #let counter=$counter+1
  166.  
  167.  
  168.  
  169. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement