homematic-community / hm_pdetect

:iphone: A HomeMatic CCU-Addon to regularly query AVM FRITZ!-devices for registered WiFi/LAN devices and deduce general user presence from this information
GNU Lesser General Public License v3.0
73 stars 6 forks source link

Modify script to use direct JSON output of fritzbox #4

Closed jens-maus closed 8 years ago

jens-maus commented 8 years ago

After some analysis it seems to be possible to directly query some status information from a fritz.box and get the result returned in JSON output:

http://fritz.box/query.lua?sid=<enter_your_sid_here>&network=landevice:settings/landevice/list(name,ip,mac,UID,dhcp,wlan,ethernet,active,static_dhcp,manu_name,wakeup,deleteable,source,online,speed,wlan_UIDs,auto_wakeup,guest,url,wlan_station_type,vendorname,parentname,parentuid,ethernet_port,wlan_show_in_monitor,plc,ipv6_ifid,parental_control_abuse)

Thus, hm_pdetect could be quite easily be changed to use this method instead of having to parse the HTML output. This would definitely be a better approach than the current implementation as it would

jens-maus commented 8 years ago

The following URL should return a pretty nice parseable JSON output of the relevant data which we could then parse more easily than the current error prone HTML parsing routines:

http://fritz.box/query.lua?sid=<enter_your_sid_here>&network=landevice:settings/landevice/list(name,ip,mac,wlan,ethernet,active,online,guest)
jens-maus commented 8 years ago

Please note that the following shell script can be used to quickly parse the JSON output and which can be used in an adapted form to extract the necessary information more quickly/nicely than having to parse the HTML output of the FRITZ!Box:

#!/bin/bash

re1="\"(name)\"[[:space:]]*:[[:space:]]*\"([^\"]*)\""
re2="\"(mac)\"[[:space:]]*:[[:space:]]*\"([^\"]*)\""
re3="\"(active)\"[[:space:]]*:[[:space:]]*\"([^\"]*)\""
re4="\"(guest)\"[[:space:]]*:[[:space:]]*\"([^\"]*)\""

while read -r l; do
    if [[ $l =~ $re1 ]] || [[ $l =~ $re2 ]] || [[ $l =~ $re3 ]] || [[ $l =~ $re4 ]]; then
        name="${BASH_REMATCH[1]}"
        value="${BASH_REMATCH[2]}"
        echo "$name=$value"
    fi
done
jens-maus commented 8 years ago

Implemented in v0.6