jackal-kr
New Around Here
hi all,
Sorry for writing into such an old thread but just have a created and scheduled to be run on my AX88U a small and nice presence script to see who is at home and when leaves.... Maybe would be useful for someone...
Sorry for writing into such an old thread but just have a created and scheduled to be run on my AX88U a small and nice presence script to see who is at home and when leaves.... Maybe would be useful for someone...
Bash:
#!/bin/sh
# *********************************************************
# Track who is at home script
# Copyright by JOD 2022-Jun-07
#
# Instruction:
# 1) Set the mac addresses, user names, and the home dir to store the temp and report fils (e.g. mounted SDCard or USB drive)
# 2) Ssh your router as admin
# 3) copy the the script file to the router into the root dir, let's assume its name is "who_is_home.sh"
# 4) Execute the following list of commends to schedule it with cron:
# $ chmod +x ./who_is_home.sh
# $ mv ./who_is_home.sh /jffs/
# $ cru a every5mincheckwhoishome "*/5 * * * * sh /jffs/who_is_home.sh"
# $ cru l
# 5) Go to your home dir and wait for 5 min to see the temp and report files there
# 6) Enjoy :)
# *********************************************************
# ---------------------------------------------------------
# Define global vars and consts
# ---------------------------------------------------------
# Debug mode flag - put 1 instead of "" to enable echoing of some vars and debug the script
DEBUG_MODE=""
# Specify the list of macs (devices of users) that will be tracked, if put zeros, would be ignored
mac1=11:11:11:11:11:11 # Jade
mac2=22:22:22:22:22:22 # Peter
mac3=33:33:33:33:33:33 # Max
mac4=00:00:00:00:00:00
mac5=00:00:00:00:00:00
mac6=00:00:00:00:00:00
mac7=00:00:00:00:00:00
# Map the user names to the macs of their mobile devices
user1="Jade"
user2="Peter"
user3="Max"
user4=""
user5=""
user6=""
user7=""
# Few important directories and files where the script will be located and keep temp files
home_dir="/tmp/mnt/Sys/track_my_home/" # Set the working dir to the mounted SD Card volume
report_file=$home_dir"report.txt" # Set the report name
temp_file=$home_dir"state.log" # Set the name of of temp file to keep previous run data
# ---------------------------------------------------------
# Define a few useful functions
# ---------------------------------------------------------
# 1) Get the list of currently connected macs
getConnectedMacs() {
local x
local connected_mas
for x in `wl -i "$1" assoclist | awk '{print $2}'`
do
connected_macs=$connected_macs";"$x;
done;
echo "$connected_macs"
}
# 2) Define a function to easier check the mac in the list of macs
stringContains() {
[ -z "$1" ] || { [ -z "${2##*$1*}" ] && [ -n "$2" ];};
}
# 3) Make a record in the report
makeRecord() {
# if in debug mode, tell me what's going on...
if [ ! -z "$DEBUG_MODE" ]; then
echo $c_run_date" "$c_run_time": ""$1"
fi
# Append the requested info to the report file
echo $c_run_date" "$c_run_time": ""$1" >> "$report_file"
}
# 4) Record if the status of the device/mac changed since the last run
checkMacStatus() {
if stringContains "$1" $c_connected_macs; then
if ! stringContains "$1" $l_connected_macs; then
# if conneted now but was not previous time - record the user is home
makeRecord "$1: $2 is at home"
fi
else
if stringContains "$1" $l_connected_macs; then
# if not found in connected but was there the previous time - record the user is away
makeRecord "$1: $2 is away"
fi
fi
}
# 5) Get the saved data from previous run
getLastState() {
# if the temp file exists, get the requested line from it, otherwise return empty string
if [[ -f "$temp_file" ]]; then
echo $(cat "$temp_file" | sed -n "$1"'p')
else
echo ""
fi
}
# 6) Save the current data for the next run
saveCurrentState() {
# if 2nd param specified, re-create the temp file with the data provided, otherwise append the lines
if [ -z "$2" ]; then
echo "$1" >> "$temp_file"
else
echo "$1" > "$temp_file"
fi
}
# ---------------------------------------------------------
# Start the script
# ---------------------------------------------------------
# Get the previous run date and set of connected macs
l_run_date=$(getLastState 1)
l_run_time=$(getLastState 2)
l_connected_macs=$(getLastState 3)
# if in debug mode, tell me what's going on...
if [ ! -z "$DEBUG_MODE" ]; then
echo "Last run date: "$l_run_date
echo "Last run macs: "$l_connected_macs
fi
# Set the current date/timr and the set of connected macs
c_run_date=$(date '+%Y-%m-%d') # Get the date in format YYYY-mm-dd
c_run_time=$(date '+%H:%M:%S') # Get the time in format "HH:MM:SS:
c_connected_macs=$(getConnectedMacs "eth6")$(getConnectedMacs "eth7") # Look in 2.4GHz (eth6) and 5.0GHz (eth7) network for device AX88U
# Mark the new day start in the report
if [ ! "$l_run_date" == "$c_run_date" ]; then
makeRecord "---------- "$c_run_date" ----------"
fi
# Check the device/mac status and record that, if changed
checkMacStatus $mac1 $user1
checkMacStatus $mac2 $user2
checkMacStatus $mac3 $user3
checkMacStatus $mac4 $user4
checkMacStatus $mac5 $user5
checkMacStatus $mac6 $user6
checkMacStatus $mac5 $user5
checkMacStatus $mac7 $user7
# if in debug mode, tell me what's going on...
if [ ! -z "$DEBUG_MODE" ]; then
echo "Curr run date: "$c_run_date
echo "Curr run macs: "$c_connected_macs
fi
# Save the current set of macs and the run date for the next run
saveCurrentState $c_run_date 1 # Re-create the file with the current date
saveCurrentState $c_run_time # Append the time as next line - can be used to track whether the script is running as expected :)
saveCurrentState $c_connected_macs # Append the current set of connected macs as the 3rd line
# ---- End of Script ----