I made a copy of the script in /jffs/scripts and have been testing it from there, going to be much harder to diagnose the script without a proper IDE.
I got the script pasted over and the only thing that caused an error was the line iw event | \
I don't know if iw is a function call, or being used as some kind of variable just yet.
I commented it out for now.
I recognise the code as some mongrel version of C. But before I start delving into the code to see what is happening today, I am going to do a bit of pre testing... i.e. see if I can send some basic messages from the router to the Mosquitto on the RPi, and from my laptop.
#!/bin/sh
DEFAULT_MODE="event"
DEFAULT_MQTT_SERVER="192.168.1.50"
DEFAULT_LAST_SEEN_UPDATE_PERIOD_S=120
MODE=$DEFAULT_MODE
MQTT_SERVER=$DEFAULT_MQTT_SERVER
LAST_SEEN_UPDATE_PERIOD_S=$DEFAULT_LAST_SEEN_UPDATE_PERIOD_S
MQTT_ID_EVENT="AsusWRT-Presence-Event"
MQTT_ID_LASTSEEN="AsusWRT-Presence-LastSeen"
MQTT_TOPIC="AsusWRTwifi/status/mac-" SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
# Parse command line args
test_for_mode(){
param_mode=$1
if [ "$param_mode" == "event" -o "$param_mode" == "lastseen" ]; then
MODE=$param_mode return 0
fi
return 1
}
test_for_ipv4(){
param_ip=$1
echo $param_ip | grep -E '\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b' > /dev/null
if [ $? -eq 0 ]; then MQTT_SERVER=$param_ip return 0
fi
return 1
}
test_for_update_periode_s(){
param_up=$1 expr $param_up : '[0-9][0-9]*$'
if [ $? -eq 0 ]; then
LAST_SEEN_UPDATE_PERIOD_S=$param_up return 0
fi
return 1
}
print_usage(){
cat << EOF Supported optional parameters:
mode: "event" or "lastseen" (default: $DEFAULT_MODE)
In event mode changes of registered mac addresses are imediately pushed to the MQTT server In lastseen mode the registered mac addresses are periodically pushed to the MQTT server
MQTT server IP: the IPv4 address of the MQTT server (default $DEFAULT_MQTT_SERVER)
Udate periode: only relevant for lastseen mode (default $LAST_SEEN_UPDATE_PERIOD_S)
Examples: $SCRIPT_NAME $SCRIPT_NAME 192.168.1.2
$SCRIPT_NAME lastseen 300
EOF
}
for param in "$@"; do
test_for_mode $param || \
test_for_ipv4 $param || \
test_for_update_periode_s $param || \
{ print_usage; exit 1; }
done
if [ "$MODE" == "event" ]; then
echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER"
# iw event | \
while read LINE; do
if echo $LINE | grep -q -E "(new|del) station"; then
EVENT=`echo $LINE | awk '/(new|del) station/ {print $2}'`
MAC=`echo $LINE | awk '/(new|del) station/ {print $4}'`
echo "Mac: $MAC did $EVENT"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_EVENT -t "$MQTT_TOPIC${MAC//:/-}/event" -m $EVENT
fi
done
elif [ "$MODE" == "lastseen" ]; then
echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER, period: $LAST_SEEN_UPDATE_PERIOD_S"
while true
do
for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
# for each interface, get mac addresses of connected stations/clients
maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
for mac in $maclist
do
echo "lastseen epoch ${mac//:/-} $(date +%s)"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/epoch" -m "$(date +%s)" -r
echo "lastseen iso8601 ${mac//:/-} $(date +%Y-%m-%dT%H:%M:%S%z)"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/iso8601" -m "$(date +%Y-%m-%dT%H:%M:%S%z)" -r
done
done
sleep ${LAST_SEEN_UPDATE_PERIOD_S}
done
fi
I got the script pasted over and the only thing that caused an error was the line iw event | \
I don't know if iw is a function call, or being used as some kind of variable just yet.
I commented it out for now.
I recognise the code as some mongrel version of C. But before I start delving into the code to see what is happening today, I am going to do a bit of pre testing... i.e. see if I can send some basic messages from the router to the Mosquitto on the RPi, and from my laptop.
#!/bin/sh
DEFAULT_MODE="event"
DEFAULT_MQTT_SERVER="192.168.1.50"
DEFAULT_LAST_SEEN_UPDATE_PERIOD_S=120
MODE=$DEFAULT_MODE
MQTT_SERVER=$DEFAULT_MQTT_SERVER
LAST_SEEN_UPDATE_PERIOD_S=$DEFAULT_LAST_SEEN_UPDATE_PERIOD_S
MQTT_ID_EVENT="AsusWRT-Presence-Event"
MQTT_ID_LASTSEEN="AsusWRT-Presence-LastSeen"
MQTT_TOPIC="AsusWRTwifi/status/mac-" SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
# Parse command line args
test_for_mode(){
param_mode=$1
if [ "$param_mode" == "event" -o "$param_mode" == "lastseen" ]; then
MODE=$param_mode return 0
fi
return 1
}
test_for_ipv4(){
param_ip=$1
echo $param_ip | grep -E '\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b' > /dev/null
if [ $? -eq 0 ]; then MQTT_SERVER=$param_ip return 0
fi
return 1
}
test_for_update_periode_s(){
param_up=$1 expr $param_up : '[0-9][0-9]*$'
if [ $? -eq 0 ]; then
LAST_SEEN_UPDATE_PERIOD_S=$param_up return 0
fi
return 1
}
print_usage(){
cat << EOF Supported optional parameters:
mode: "event" or "lastseen" (default: $DEFAULT_MODE)
In event mode changes of registered mac addresses are imediately pushed to the MQTT server In lastseen mode the registered mac addresses are periodically pushed to the MQTT server
MQTT server IP: the IPv4 address of the MQTT server (default $DEFAULT_MQTT_SERVER)
Udate periode
Examples: $SCRIPT_NAME $SCRIPT_NAME 192.168.1.2
$SCRIPT_NAME lastseen 300
EOF
}
for param in "$@"; do
test_for_mode $param || \
test_for_ipv4 $param || \
test_for_update_periode_s $param || \
{ print_usage; exit 1; }
done
if [ "$MODE" == "event" ]; then
echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER"
# iw event | \
while read LINE; do
if echo $LINE | grep -q -E "(new|del) station"; then
EVENT=`echo $LINE | awk '/(new|del) station/ {print $2}'`
MAC=`echo $LINE | awk '/(new|del) station/ {print $4}'`
echo "Mac: $MAC did $EVENT"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_EVENT -t "$MQTT_TOPIC${MAC//:/-}/event" -m $EVENT
fi
done
elif [ "$MODE" == "lastseen" ]; then
echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER, period: $LAST_SEEN_UPDATE_PERIOD_S"
while true
do
for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
# for each interface, get mac addresses of connected stations/clients
maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
for mac in $maclist
do
echo "lastseen epoch ${mac//:/-} $(date +%s)"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/epoch" -m "$(date +%s)" -r
echo "lastseen iso8601 ${mac//:/-} $(date +%Y-%m-%dT%H:%M:%S%z)"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/iso8601" -m "$(date +%Y-%m-%dT%H:%M:%S%z)" -r
done
done
sleep ${LAST_SEEN_UPDATE_PERIOD_S}
done
fi
Last edited: