#!/bin/sh -efu set -efu cd /root/files/wifi-tracker || exit 1 export "PATH=$PATH:/usr/local/sbin:/usr/local/bin" match_home='* hostapd: wlan?: STA * IEEE 802.11: authenticated*' match_stillhome='* hostapd: wlan?: STA * WPA: group key handshake completed*' match_away='* hostapd: wlan?: STA * IEEE 802.11: deauthenticated*' sed_re_mac_only='s|.+[[:space:]]+STA[[:space:]]+||;s|[[:space:]]+.+||' _syslog() { logger -t wifi-tracker "$1" } _duration_human_readable() { local seconds="$1" perl -MTime::Duration -wle \ 'print concise duration_exact shift' "$seconds" || echo "${seconds}s" } _status() { local device="$1" local status="$2" local name="$device" local status_file="$device.status" local name_file="$device.name" local log_file="$device.log" local duration_file="$device.duration" local status_time= local status_duration= local status_duration_human_readable= local prev_status= local prev_status_time= local prev_status_duration= local prev_status_duration_human_readable= if [ -f "$status_file" ] then prev_status="`cat \"$status_file\"`" fi if [ -f "$name_file" ] then name="`cat \"$name_file\"`" fi if [ "$prev_status" -a "$prev_status" != "$status" ] then if [ -f "$log_file" ] then prev_status_time="`stat -f %m \"$log_file\"`" prev_status_duration="$((`date +%s` - $prev_status_time))" prev_status_duration_human_readable="`_duration_human_readable \"$prev_status_duration\"`" _syslog "$name $prev_status ($prev_status_duration_human_readable) -> $status" else _syslog "$name $prev_status -> $status" fi echo "`date +%s`|$status" >> "$log_file" if [ "$prev_status_duration" ] then echo "`date +%s`|$prev_status|$prev_status_duration" >> "$duration_file" fi else if [ -f "$log_file" ] then status_time="`stat -f %m \"$log_file\"`" status_duration="$((`date +%s` - $status_time))" if [ "$status_duration" -eq 0 ] then _syslog "$name is $status" else status_duration_human_readable="`_duration_human_readable \"$status_duration\"`" _syslog "$name is $status ($status_duration_human_readable)" fi else _syslog "$name is $status" fi fi echo "$status" > "$status_file" } while read line do status= case "$line" in $match_home) status=home ;; $match_stillhome) status=home ;; $match_away) status=away ;; esac if [ "$status" ] then device="`echo \"$line\" | sed -E \"$sed_re_mac_only\" | tr -cd 0-9a-f:`" _status "$device" "$status" fi done