Advertisement
Guest User

Untitled

a guest
Nov 19th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. USER="admin"
  2. PASS="passhere"
  3. PROTOCOL="http"
  4. ROUTER_IP="192.168.1.1"
  5.  
  6. # Port to connect to which will provide the JSON data.
  7. PORT=9898
  8.  
  9. while [ 1 ]
  10. do
  11. # Grab connected device MAC addresses through router status page.
  12. MACS=$(curl -s --user $USER:$PASS $PROTOCOL://$ROUTER_IP/Status_Wireless.live.asp)
  13.  
  14. # clear temp JSON file
  15. echo > temp.log
  16.  
  17. # Get hostname and IP (just in case there is no hostname).
  18. for MAC in $(echo $MACS | grep -oE "wl_mac::[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}" | cut -c 9-);
  19. do
  20. grep 0x /proc/net/arp | awk '{print $1 " " $4}' | while IFS= read -r line
  21. do
  22. IP=$(echo $line | cut -d' ' -f1)
  23. MACTEMP=$(echo $line | cut -d' ' -f2)
  24. HOST=$(arp -a | grep $IP | cut -d' ' -f1)
  25.  
  26. # if no hostname exists, just use IP.
  27. if [ "$HOST" == "" ]
  28. then
  29. HOST=$IP
  30. fi
  31.  
  32. if [ "$MAC" == "$MACTEMP" ]
  33. then
  34. JSON="{'hostname' : '$HOST', 'mac_address' : '$MAC'}"
  35. echo $JSON >> temp.log
  36. fi
  37.  
  38. done
  39. done
  40.  
  41. # Provide the JSON formatted output on $PORT of router.
  42. # This allows one connection before closing the port (connect, receive data, close).
  43. # Port will reopen every 5 minutes with new data as setup in a cron job.
  44. echo -e "HTTP/1.1 200 OKnn $(cat temp.log)" | nc -l -p$PORT >/dev/null
  45.  
  46. # Wait for 10 seconds and do it all over.
  47. sleep 10
  48.  
  49. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement