Python plugin: Presence detection from wireless router

Python and python framework
Post Reply
devros
Posts: 177
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros » Wednesday 06 June 2018 9:59

Hello tried on ubitiqui Airos
created ssh key so i can log without pass
but have this error in domoticz

Code: Select all

2018-06-06 09:57:09.632 (Wifi) Returncode ssh command: 255
2018-06-06 09:57:09.632 (Wifi) Output from router: b''
when o log to ubitiqui it looks like arp command is missing, so it probably cant work ?
hmm found answer:
https://community.ubnt.com/t5/airOS-Sof ... d-p/133084

Code: Select all

cat /proc/net/arp
can i somehow fix script to use this command ?

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Wednesday 06 June 2018 10:51

Hi devros,

I was just working on the plugin and added it for you. The plugin will now fall back to reading /proc/net/arp if every other method is unavailable.

Bear in mind that the arp table is a very unreliable method for presence detection. It wil detect presence immediately, but if a devices leaves then it can take a while, even hours(!) for the mac address to disappear from the arp table. At least in my Asus router. Maybe your router is quicker to respond to leaving devices. Please let me know.

If you can figure out a more reliable method for Ubiquiti then i will try to incorporate that into the plugin. That takes some experimenting on the router itself, which only you can do. Maybe it has another command that equals wl or iwinfo.

devros
Posts: 177
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros » Wednesday 06 June 2018 11:24

wow, that was fast :)
allready tried
but still have some errors

Code: Select all

2018-06-06 11:19:54.630 (Wifi3) Monitoring {'jan': 'A0:D7:95:85:36:55'} for presence.
2018-06-06 11:19:54.631 (Wifi3) Pushing 'PollIntervalDirective' on to queue
2018-06-06 11:19:54.631 (Wifi3) Checking router capabilities and wireless interfaces
2018-06-06 11:19:54.631 (Wifi3) Using ssh public key authentication (~/.ssh/id_rsa.pub) for OS user running domoticz.
2018-06-06 11:19:54.631 (Wifi3) Fetching data from router using ssh
2018-06-06 11:19:54.631 (Wifi3) command: ['ssh', '-o', 'ConnectTimeout=2', 'ubnt@192.168.3.1', '#!/bin/sh\n\t\t\t\t\texport PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH\n\t\t\t\t\ttest=$(which wl > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "wl@"\n\t\t\t\t\t\t\tfor iface in $(ifconfig | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(wl -i $iface assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\ttest=$(which iwinfo > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "iwinfo@"\n\t\t\t\t\t\t\tfor iface in $(iwinfo | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(iwinfo wlan0 assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\t\t\t\t\t\n\t\t\t\t\ttest=$(which arp > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "arp"\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\tif [ -f /proc/net/arp ]; then\n\t\t\t\t\t\tprintf "procarp"\n\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\tprintf none']
2018-06-06 11:19:54.983 (Wifi3) Returncode ssh command: 255
2018-06-06 11:19:54.984 (Wifi3) Output from router: b''
2018-06-06 11:19:54.985 (Wifi3) monitoring device: Wifi - jan
2018-06-06 11:19:54.985 (Wifi3) Wifi - jan is stil in use
2018-06-06 11:19:54.985 (Wifi3) devid2domid: {'jan': 2}
2018-06-06 11:19:54.985 (Wifi3) monitormacs: {'jan': 'A0:D7:95:85:36:55'}
2018-06-06 11:19:54.985 (Wifi3) Processing 'PollIntervalDirective' message
2018-06-06 11:19:54.985 (Wifi3) Heartbeat interval set to: 20.

and then

2018-06-06 11:19:54.984 Error: (Wifi3) Router command failed with returncode: 255
2018-06-06 11:19:54.984 Error: (Wifi3) Raw data from router: b''
2018-06-06 11:19:54.984 Error: (Wifi3) Could not determine router capabilities

cant find wl or iwinfo on ubitiqui, but its based on openwrt afaik
in /proc/net i have these apps

Code: Select all

anycast6             if_inet6             ip_conntrack_expect  mcfilter             packet               route                softnet_stat         udplite
arp                  igmp                 ip_mr_cache          mcfilter6            pppoe                rt6_stats            stat                 udplite6
arpnat               igmp6                ip_mr_vif            netfilter            protocols            rt_cache             tcp                  unix
arpnat_cache         ip6_flowlabel        ip_tables_matches    netlink              psched               snmp                 tcp6                 vlan
dev                  ip6_mr_cache         ip_tables_names      netstat              ptype                snmp6                ubnt_poll            wifi0
dev_mcast            ip6_mr_vif           ip_tables_targets    nf_conntrack         raw                  sockstat             udp                  wireless
dev_snmp6            ip_conntrack         ipv6_route           nf_conntrack_expect  raw6                 sockstat6            udp6                 xt_recent
update:
found wlanconfig but cant find parametr for list on local network, only outside monitoring works
Last edited by devros on Wednesday 06 June 2018 14:07, edited 1 time in total.

canix1
Posts: 9
Joined: Friday 25 May 2018 20:37
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by canix1 » Wednesday 06 June 2018 11:30

@EscApe

Ok, I have checked.
Python --Version gives me 2.7.9
Python3 --Version gives me 3.6.5

I this ok?

Cheers

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Wednesday 06 June 2018 11:59

In Domoticz under settings>about you can see the Python version Domoticz is using. It wil probably by 3.6.5 and that's just fine for the plugin.
Are you sure you setup key based authentication for the user that is running the Domoticz service (root by default)?
What OS are you running Domoticz on?

canix1
Posts: 9
Joined: Friday 25 May 2018 20:37
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by canix1 » Wednesday 06 June 2018 14:30

Hi
Yes, I think the authentication works fine. I have verified using a test script below:

Code: Select all

pi@raspberrypi:~/domoticz/plugins/iDetect$ cat testscript.py
import subprocess
host="192.168.1.1"
user="admin"
idfile="/root/.ssh/id_rsa"

ssh = subprocess.Popen(["ssh", "-o", "ConnectTimeout=3", user+"@"+host, "-i", idfile, "wl -i eth1 assoclist;wl -i eth2 assoclist"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ssh_out, ssh_err = ssh.communicate()
print("output:", ssh_out)
print("error:", ssh_err)
pi@raspberrypi:~/domoticz/plugins/iDetect$ sudo python3 testscript.py
output: b'assoclist F4:8C:50:23:58:B1\nassoclist 8C:F5:A3:F3:3C:A2\nassoclist 40:4D:7F:6E:7F:7F\nassoclist 68:AB:1E:F0:A4:31\nassoclist 60:45:BD:E9:DD:88\nassoclist 00:13:EF:C6:01:66\nassoclist 00:22:F4:3A:49:4B\nassoclist B4:E6:2A:21:CB:88\nassoclist B0:C5:54:29:0A:40\n'
error: b''
pi@raspberrypi:~/domoticz/plugins/iDetect$

This is the OS info I got:

Code: Select all

PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
One odd thing I just notice was that in the About info in Domoticz it says it's using Pythin 3.4.2.

Code: Select all

About Domoticz
Version: 3.9484
Build Hash: b158c2f
Compile Date: 2018-05-25 08:25:20
dzVents Version: 2.4.5
Python Version: 3.4.2 (default, Oct 19 2014, 14:03:53) [GCC 4.9.1]
This is the content of /usr/bin/

Code: Select all

py3clean           pydoc3             python2.7          python3.6
py3compile         pydoc3.4           python2.7-config   python3.6m
py3versions        pygettext          python2-config     python3.6m-config
pybuild            pygettext2.7       python3            python3-config
pyclean            pygettext3         python3.4          python3m
pycompile          pygettext3.4       python3.4-config   python3m-config
pydoc              python             python3.4m         python-config
pydoc2.7           python2            python3.4m-config  pyversions

This is the Domoticz log when turning on the plugin:

Code: Select all

2018-06-06 14:27:40.243 (iDetect) Debug logging mask set to: PYTHON PLUGIN QUEUE IMAGE DEVICE CONNECTION MESSAGE ALL 
2018-06-06 14:27:40.243 Error: (iDetect) [ssh -V] will not run but is required! (error: 'module' object has no attribute 'run') 
2018-06-06 14:27:40.243 (iDetect) Monitoring {'Android': '40:4D:7F:6E:7F', 'IPhone': '8C:F5:A3:F3:3C:A2', 'Microsoft': '7F88:66:A5:1A:74'} for presence. 
2018-06-06 14:27:40.243 (iDetect) Pushing 'PollIntervalDirective' on to queue 
2018-06-06 14:27:40.243 (iDetect) Checking router capabilities and wireless interfaces 
2018-06-06 14:27:40.243 (iDetect) Using ssh public key authentication (~/.ssh/id_rsa.pub) for OS user running domoticz. 
2018-06-06 14:27:40.244 (iDetect) Fetching data from router using ssh 
2018-06-06 14:27:40.244 (iDetect) command: ['ssh', '-o', 'ConnectTimeout=2', 'admin@192.168.1.1', '#!/bin/sh\n\t\t\t\t\texport PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH\n\t\t\t\t\ttest=$(which wl > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "wl@"\n\t\t\t\t\t\t\tfor iface in $(ifconfig | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(wl -i $iface assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\ttest=$(which iwinfo > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "iwinfo@"\n\t\t\t\t\t\t\tfor iface in $(iwinfo | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(iwinfo wlan0 assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\t\t\t\t\t\n\t\t\t\t\ttest=$(which arp > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "arp"\n\t\t\t\t\tfi'] 
2018-06-06 14:27:40.244 Error: (iDetect) SSH subprocess failed with error: 'module' object has no attribute 'run' 
2018-06-06 14:27:40.244 Error: (iDetect) Could not determine router capabilities 
2018-06-06 14:27:40.244 (iDetect) monitoring device: iDetect - Android 
2018-06-06 14:27:40.244 (iDetect) deleting device unit: 2 named: iDetect - Android 
2018-06-06 14:27:40.244 (iDetect) monitoring device: iDetect - IPhone 
2018-06-06 14:27:40.244 (iDetect) deleting device unit: 3 named: iDetect - IPhone 
2018-06-06 14:27:40.244 (iDetect) monitoring device: iDetect - Microsoft 
2018-06-06 14:27:40.244 (iDetect) deleting device unit: 4 named: iDetect - Microsoft 
2018-06-06 14:27:40.244 (iDetect) devid2domid: {} 
2018-06-06 14:27:40.244 (iDetect) monitormacs: {'Android': '40:4D:7F:6E:7F', 'IPhone': '8C:F5:A3:F3:3C:A2', 'Microsoft': '7F88:66:A5:1A:74'} 
2018-06-06 14:27:40.245 (iDetect) Deleting device 'iDetect - Android'. 
2018-06-06 14:27:40.245 (iDetect) Deleting device 'iDetect - IPhone'. 
2018-06-06 14:27:40.246 (iDetect) Deleting device 'iDetect - Microsoft'. 
2018-06-06 14:27:40.247 (iDetect) Android not in known device list 
2018-06-06 14:27:40.247 (iDetect) creating device for Android with unit id 2 
2018-06-06 14:27:40.247 (iDetect) Creating device 'Android'. 
2018-06-06 14:27:40.249 (iDetect - Android) Updating device from 0:'' to have values 0:'Off'. 
2018-06-06 14:27:40.253 (iDetect) IPhone not in known device list 
2018-06-06 14:27:40.253 (iDetect) creating device for IPhone with unit id 3 
2018-06-06 14:27:40.254 (iDetect) Creating device 'IPhone'. 
2018-06-06 14:27:40.256 (iDetect - IPhone) Updating device from 0:'' to have values 0:'Off'. 
2018-06-06 14:27:40.260 (iDetect) Microsoft not in known device list 
2018-06-06 14:27:40.260 (iDetect) creating device for Microsoft with unit id 4 
2018-06-06 14:27:40.260 (iDetect) Creating device 'Microsoft'. 
2018-06-06 14:27:40.262 (iDetect - Microsoft) Updating device from 0:'' to have values 0:'Off'. 
2018-06-06 14:27:40.266 (iDetect) Processing 'PollIntervalDirective' message 
2018-06-06 14:27:40.266 (iDetect) Heartbeat interval set to: 50. 
2018-06-06 14:27:49.686 (iDetect) Pushing 'onHeartbeatCallback' on to queue 
2018-06-06 14:27:49.688 (iDetect) Processing 'onHeartbeatCallback' message 
2018-06-06 14:27:49.688 (iDetect) Calling message handler 'onHeartbeat'. 
2018-06-06 14:27:49.688 (iDetect) devid2domid: {'Android': 2, 'IPhone': 3, 'Microsoft': 4} 
2018-06-06 14:27:49.688 Error: (iDetect) No usable commandline to check presence. Trying again to detect router capabilities. 
2018-06-06 14:27:49.688 Error: (iDetect) If you keep getting this message: check your (authentication) settings and router status. 
2018-06-06 14:27:49.688 Error: (iDetect) Make sure your router supports ssh and the commands used by this plugin. If this message persists disable the plugin (for now), or the plugin might flood the Domoticz eventsystem 
2018-06-06 14:27:49.688 (iDetect) Checking router capabilities and wireless interfaces 
2018-06-06 14:27:49.688 (iDetect) Using ssh public key authentication (~/.ssh/id_rsa.pub) for OS user running domoticz. 
2018-06-06 14:27:49.689 (iDetect) Could not detemine the user profile running Domoticz 
2018-06-06 14:27:49.689 (iDetect) Fetching data from router using ssh 
2018-06-06 14:27:49.689 (iDetect) command: ['ssh', '-o', 'ConnectTimeout=2', 'admin@192.168.1.1', '#!/bin/sh\n\t\t\t\t\texport PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH\n\t\t\t\t\ttest=$(which wl > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "wl@"\n\t\t\t\t\t\t\tfor iface in $(ifconfig | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(wl -i $iface assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\ttest=$(which iwinfo > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "iwinfo@"\n\t\t\t\t\t\t\tfor iface in $(iwinfo | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(iwinfo wlan0 assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\t\t\t\t\t\n\t\t\t\t\ttest=$(which arp > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "arp"\n\t\t\t\t\tfi'] 
2018-06-06 14:27:49.689 Error: (iDetect) SSH subprocess failed with error: 'module' object has no attribute 'run' 
2018-06-06 14:27:49.689 Error: (iDetect) Could not determine router capabilities 

Cheers

canix1
Posts: 9
Joined: Friday 25 May 2018 20:37
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by canix1 » Wednesday 06 June 2018 14:56

One question regarding the authentication key.

In the log I see that iDetect is using the public key to connect to the router:
2018-06-06 14:27:49.688 (iDetect) Using ssh public key authentication (~/.ssh/id_rsa.pub) for OS user running domoticz.

Is this really the way to do it? I'm new to this...

The instructions on GitHub says to put the public key on the router, so should you not use the private key to connect to the router?
That is what is working for me at least :)

From GitHub:
step 1: Create a keyfile for the user account (root by default) that runs Domoticz.
The public and private key files should be in /.ssh
step 2: Place the public(!) key on the router. Using the asus merlin firmware this can be done in the GUI. This will be different for other routers and might be impossible on many standard firmwares!

Cheers

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Wednesday 06 June 2018 16:36

Setting up key based authentication is tricky. That's why i added the password based login option. There is some info on it in this thread and lots of instructions to be found on the internet. Basically you generate a key on your Domoticz machine. The router must recognize this key, so the public key needs to be known to the router. Where the router wants this key configured or stored depends on the router software.

If you normally login as (for instance) user1 and created the key under that name it will work fine for user1 and when running your test script from the command line. If the Domoticz service is run as user root it will however not use that key, since it has a different homedirectory (different ~/.ssh).

Maybe you're better of starting with password based authentication (you need to install sshpass first). You can always change to key based when everything else is working.

The plugin won't work as long as Domoticz is using Python3.4, you'll need to update to 3.5 at least. If you installed python3.4 using apt-get (or it was installed by default) you could just run:

Code: Select all

sudo apt-get update
sudo apt-get install --only-upgrade python3
It's always smart to make a backup before you make changes to the OS.

canix1
Posts: 9
Joined: Friday 25 May 2018 20:37
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by canix1 » Thursday 07 June 2018 0:08

Do I have to compile Domoticz again after an upgrade of Python?
The command (sudo apt-get install --only-upgrade python3) does not update anything, I got the latests.

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Thursday 07 June 2018 18:53

I did not have to recompile Domoticz when I downgraded from 3.6 to 3.5. Since I compiled Python 3.6 I could not uninstall it using apt-get so what I did was:

- uninstall all versions of python that where installed by apt-get (3.4 and 3.5)
- manually delete remaining python related stuff (which was mostly 3.6)
- install python3 using apt-get (on raspbian repository 3.5 is the latest version)

All a bit messy, but I could not find another clean way to get rid of 3.6 without breaking (links for) python3 in general. You can google what files and directories are related to python. Best to make a backup before you start deleting stuff.

Edit I don’t know if it will work, but you could try to uninstall python 3.4 expilictely. Or use google to find out how you can manage multiple python emvirionments.

devros
Posts: 177
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros » Friday 08 June 2018 0:12

so on my ubitiqui AirOs now it seem it works great (with user and pass filled in domoticz iDetect plugin setting). Response with ARP is quite fast. :) only problem is that my log is full of errors
2018-06-07 08:14:30.954 Error: test hardware (34) thread seems to have ended unexpectedly

Code: Select all

2018-06-07 08:17:09.437 (test) Debug logging mask set to: PYTHON PLUGIN QUEUE IMAGE DEVICE CONNECTION MESSAGE ALL
2018-06-07 08:17:09.467 (test) Checking if [ssh -V] will run: OK
2018-06-07 08:17:09.485 (test) Checking if [sshpass -V] will run: OK
2018-06-07 08:17:09.486 (test) Monitoring {'svatka': 'XXXX', 'honza': 'XXX'} for presence.
2018-06-07 08:17:09.486 (test) Pushing 'PollIntervalDirective' on to queue
2018-06-07 08:17:09.486 (test) Checking router capabilities and wireless interfaces
2018-06-07 08:17:09.486 (test) Using password instead of ssh public key authentication (less secure!)
2018-06-07 08:17:09.486 (test) Fetching data from router using ssh
2018-06-07 08:17:09.486 (test) command: ['sshpass', '-p', 'xxxx', 'ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=2', 'ubnt@192.168.3.1', '#!/bin/sh\n\t\t\t\t\texport PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH\n\t\t\t\t\ttest=$(which wl > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "wl@"\n\t\t\t\t\t\t\tfor iface in $(ifconfig | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(wl -i $iface assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\ttest=$(which iwinfo > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "iwinfo@"\n\t\t\t\t\t\t\tfor iface in $(iwinfo | cut -d \' \' -f1| tr \':\' \'\n\' | grep \'^eth\\|^wlan\')\n\t\t\t\t\t\t\tdo\n\t\t\t\t\t\t\t\t\ttest=$(iwinfo wlan0 assoclist > /dev/null 2>&1)\n\t\t\t\t\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\t\t\t\t\tprintf "$iface "\n\t\t\t\t\t\t\t\t\tfi\n\t\t\t\t\t\t\tdone\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\t\t\t\t\t\n\t\t\t\t\ttest=$(which arp > /dev/null 2>&1)\n\t\t\t\t\tif [ $? == 0 ]; then\n\t\t\t\t\t\t\tprintf "arp"\n\t\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\tif [ -f /proc/net/arp ]; then\n\t\t\t\t\t\tprintf "procarp"\n\t\t\t\t\t\texit\n\t\t\t\t\tfi\n\t\t\t\t\tprintf none']
2018-06-07 08:17:09.965 (test) Returncode ssh command: 0
2018-06-07 08:17:09.965 (test) Output from router: b'procarp'
2018-06-07 08:17:09.965 (test) Parsed data from router: ['procarp']
2018-06-07 08:17:09.966 (test) Constructed this cmd for the router to poll for present phones: export PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH;cat /proc/net/arp | grep '..:..:..:..:..:..' | awk '{print $ 4}';exit
2018-06-07 08:17:09.966 (test) monitoring device: test - honza
2018-06-07 08:17:09.966 (test) test - honza is stil in use
2018-06-07 08:17:09.966 (test) monitoring device: test - svatka
2018-06-07 08:17:09.966 (test) test - svatka is stil in use
2018-06-07 08:17:09.966 (test) devid2domid: {'svatka': 3, 'honza': 2}
2018-06-07 08:17:09.966 (test) monitormacs: {'svatka': 'xxxx', 'honza': 'xxxx'}
2018-06-07 08:17:09.966 (test) Processing 'PollIntervalDirective' message
2018-06-07 08:17:09.966 (test) Heartbeat interval set to: 300.
2018-06-07 08:17:18.935 (test) Pushing 'onHeartbeatCallback' on to queue
2018-06-07 08:17:18.969 (test) Processing 'onHeartbeatCallback' message
2018-06-07 08:17:18.969 (test) Calling message handler 'onHeartbeat'.
2018-06-07 08:17:18.969 (test) devid2domid: {'svatka': 3, 'honza': 2}
2018-06-07 08:17:18.970 (test) Polling present phones from router
2018-06-07 08:17:18.970 (test) Using password instead of ssh public key authentication (less secure!)
2018-06-07 08:17:18.970 (test) Fetching data from router using ssh
2018-06-07 08:17:18.970 (test) command: ['sshpass', '-p', 'dilnajanova', 'ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=1', 'ubnt@192.168.3.1', "export PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:$PATH;cat /proc/net/arp | grep '..:..:..:..:..:..' | awk '{print $ 4}';exit"]
2018-06-07 08:17:19.500 (test) Returncode ssh command: 0
2018-06-07 08:17:19.500 (test) Output from router: b'f0:fe:6b:6a:0f:1a\n04:95:e6:23:1d:50\n00:a0:de:a2:a4:1c\n78:11:dc:12:ce:b7\na0:d7:95:85:36:55\n32:83:45:4b:85:db\n28:6c:07:10:84:de\nd4:ca:6d:7b:34:eb\n54:10:ec:8b:7c:dd\n60:01:94:70:ac:e0\nf0:fe:6b:28:2a:fe\n78:11:dc:b0:64:78\n00:d0:4b:96:1f:c6\nb4:43:0d:f9:16:04\n'
2018-06-07 08:17:19.501 (test) Seems to have went offline: honza
2018-06-07 08:17:08.675 Status: (test) Stop directive received.
2018-06-07 08:17:08.876 Status: (test) Stopping threads.
2018-06-07 08:17:09.171 Status: (test) Exiting work loop.
2018-06-07 08:17:09.171 Status: (test) Stopped.
2018-06-07 08:17:09.172 Status: (test) Started.
2018-06-07 08:17:09.432 Status: (test) Entering work loop.
2018-06-07 08:17:09.432 Status: (test) Initialized version 0.3.3, author 'ESCape'
2018-06-07 08:17:09.966 Status: (test) Using last resort method (read info from /proc/net/arp file). This method has a slower and less reliable response to absence)
2018-06-07 08:17:19.501 Status: (test) Awaiting confirmation on absence of 1 devices
so it looks firs check is ok
but then i have these error in logs
but plugin seems to work, even with these errors

Code: Select all

2018-06-07 08:14:30.954 Error: test hardware (34) thread seems to have ended unexpectedly
im on Python Version: 3.5.2
dunno how to debug thread bug (maybe some probem with python plugin system in domoticz)

any ideas how to get rid off these errors?

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Friday 08 June 2018 18:42

I don't know what is causing these messages. I had some problems with python plugins and different python version in the past, but ever since i started compiling instead of installing the binary version of Domoticz the plugin framework has been working pretty well.

https://www.domoticz.com/wiki/Raspberry ... rom_source

One thing you could try is change the default timeouts in the plug. I doubt they are causing this, because i would expect other error messages if a timeout occurs... Can't hurt to try. If a router is slow to respond or the Domoticz host is very busy then sometimes the plugin will encounter (and log!) a timeout.
Look for the line that says:

Code: Select all

def getfromssh(self, host, user, passwd, routerscript, alltimeout=2, sshtimeout=1):
Maybe anyone else on the forum knows why these kind of errors occur on some systems and not others??

devros
Posts: 177
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros » Friday 08 June 2018 19:46

EscApe wrote:
Friday 08 June 2018 18:42
I don't know what is causing these messages. I had some problems with python plugins and different python version in the past, but ever since i started compiling instead of installing the binary version of Domoticz the plugin framework has been working pretty well.

https://www.domoticz.com/wiki/Raspberry ... rom_source

One thing you could try is change the default timeouts in the plug. I doubt they are causing this, because i would expect other error messages if a timeout occurs... Can't hurt to try. If a router is slow to respond or the Domoticz host is very busy then sometimes the plugin will encounter (and log!) a timeout.
Look for the line that says:

Code: Select all

def getfromssh(self, host, user, passwd, routerscript, alltimeout=2, sshtimeout=1):
Maybe anyone else on the forum knows why these kind of errors occur on some systems and not others??
thans tried several timeout setting but still same error, but even with errors script works...

Code: Select all

2018-06-08 19:33:57.610 Status: (test) Stop directive received.
2018-06-08 19:33:57.810 Status: (test) Stopping threads.
2018-06-08 19:33:57.944 Status: (test) Exiting work loop.
2018-06-08 19:33:57.946 Status: (test) Stopped.
2018-06-08 19:33:57.947 Status: (test) Started.
2018-06-08 19:33:58.235 Status: (test) Entering work loop.
2018-06-08 19:33:58.236 Status: (test) Initialized version 0.3.3, author 'ESCape'
2018-06-08 19:33:58.830 Status: (test) Using last resort method (read info from /proc/net/arp file). This method has a slower and less reliable response to absence)
noticed that on first run script first check is without error, but then error is on every check..

User avatar
Innovator
Posts: 15
Joined: Tuesday 20 February 2018 13:33
Target OS: NAS (Synology & others)
Domoticz version: 8924
Location: Amsterdam
Contact:

Re: Python plugin: Presence detection from wireless router

Post by Innovator » Tuesday 12 June 2018 20:03

@Escape
Finally I had time for some domoticz test work. Observations today with the latest version 0.3.3 plugin:
2018-06-12 19: 47: 01.524 Error: (iDetect WiFi presence detection) 'onHeartbeat' failed 'AttributeError': '' BasePlugin 'object has no attribute' routercmdline ''.
2018-06-12 19: 47: 01.524 Error: (iDetect WiFi presence detection) ----> Line 352 in /usr/local/domoticz/var/plugins/Presence/plugin.py, function onHeartbeat
2018-06-12 19: 47: 01.524 Error: (iDetect WiFi presence detection) ----> Line 302 in /usr/local/domoticz/var/plugins/Presence/plugin.py, function onHeartbeat

Domoticz runs on Synology DS918 +. Package Python (publisher Synology) version 3.5.1-0108, Domoticz version Beta 3.9421, Router ASUS BRT-AC828
The previous version (with the possibility of completing the path of the ssh key file) turned out well. No ssh keyfile problem seems to me
Synology DS918+ /DS412+
ASUS BRT-AC828, RT-AC86U, RT-AC68U

User avatar
Innovator
Posts: 15
Joined: Tuesday 20 February 2018 13:33
Target OS: NAS (Synology & others)
Domoticz version: 8924
Location: Amsterdam
Contact:

Re: Python plugin: Presence detection from wireless router

Post by Innovator » Tuesday 12 June 2018 20:25

EscApe wrote:
Saturday 26 May 2018 12:30

@Escape

The only command that gives something back is:

wlanconfig ath0 list stand

Output:
ADDR AID CHAN TXRATE RXRATE RSSI IDLE TXSEQ RXSEQ CAPS ACAPS ERP STATE MAXRATE (DOT11) HTCAPS ASSOCILE IEs MODE PSMODE
XX: XX: XX: XX: XX: XX 3 6 52M 43M 47 0 0 65535 EPSs 0 f 0 WPGS 33:54:21 RSN WME IEEE80211_MODE_11NG_HT20 0
XX: XX: XX: XX: XX: XX 5 6 96M 86M 49 0 0 65535 EPSs 0 f 0 AP 28:16:36 RSN WME IEEE80211_MODE_11NG_HT20 0
XX: XX: XX: XX: XX: XX 6 6 72M 54M 36 0 0 65535 EPS 0 f 0 P 14:35:26 RSN WME IEEE80211_MODE_11NG_HT20 1
XX: XX: XX: XX: XX: XX 1 6 192M 173M 44 0 0 65535 EPSs 0 f 0 AP 07:18:56 RSN WME IEEE80211_MODE_11NG_HT20 1
XX: XX: XX: XX: XX: XX 4 6 78M 86M 40 0 ​​0 65535 EPs 0 f 0 AP 02:21:54 RSN WME IEEE80211_MODE_11NG_HT20 1
XX: XX: XX: XX: XX: XX 2 6 72M 72M 40 0 ​​0 65535 EPs 0 f 0 Q 01:07:45 RSN WME IEEE80211_MODE_11NG_HT20 1
Synology DS918+ /DS412+
ASUS BRT-AC828, RT-AC86U, RT-AC68U

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Tuesday 12 June 2018 22:18

@innovator:

If setting up key based access for the user running Domoticz on the Synology (root??) is not an option you can change a small piece of code in the plugin.

Look for (will be around line 81 in the version i'm uploading in a few minutes):

Code: Select all

cmd =[self.sshbin, "-o", "ConnectTimeout=" + str(sshtimeout), user+"@"+host, routerscript]
And change it to (with whatever path you are using):

Code: Select all

cmd =[self.sshbin, "-i", "/volume1/path/to/sshfiles/privatekeyfile", "-o", "ConnectTimeout=" + str(sshtimeout), user+"@"+host, routerscript]
You can experiment from the synology command line to confirm it is working: ssh -i <keyfile-path> usernameonrouter@routerip

I have added the wlanconfig command and ath* interface names to the latest version of the plugin. Will upload it in a few minutes. If anything goes wrong please include some debug logging of the plugin starting up. The part you included only shows that the polling failed, but the reason is a failure while initializing the plugin.

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Tuesday 12 June 2018 22:34

Version 0.4.0 has just been uploaded to GitHub. It offers a new 'Override' feature which is described in the changelog on GitHub.

When upgrading: Please make sure you also download the new ioverride.zip file. It contains the icons for the Override device.

User avatar
Dnpwwo
Posts: 736
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python plugin: Presence detection from wireless router

Post by Dnpwwo » Wednesday 13 June 2018 9:21

but then i have these error in logs
but plugin seems to work, even with these errors

Code: Select all

Code: Select all

2018-06-07 08:14:30.954 Error: test hardware (34) thread seems to have ended unexpectedly
im on Python Version: 3.5.2
dunno how to debug thread bug (maybe some probem with python plugin system in domoticz)

any ideas how to get rid off these errors?
This is covered on the wiki:https://www.domoticz.com/wiki/Developin ... #Callbacks for the onHeartbeat callback, the pertinent bit is:
Warning: Setting this interval to greater than 30 seconds will cause a 'thread seems to have ended unexpectedly' message to be written to the log file every 30 seconds. The plugin will function correctly but this message can not be suppressed because it is a standard warning from Domoticz that a piece of hardware may have stopped responding.
If a plugin wants to heartbeat every 100 seconds it should be coded with the heartbeat interval set to 10, 20 or 25 seconds and only take action every 6th, 5th or 4th time the callback is invoked.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw

devros
Posts: 177
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by devros » Wednesday 13 June 2018 10:55

Dnpwwo wrote:
Wednesday 13 June 2018 9:21
but then i have these error in logs
but plugin seems to work, even with these errors

Code: Select all

Code: Select all

2018-06-07 08:14:30.954 Error: test hardware (34) thread seems to have ended unexpectedly
im on Python Version: 3.5.2
dunno how to debug thread bug (maybe some probem with python plugin system in domoticz)

any ideas how to get rid off these errors?
This is covered on the wiki:https://www.domoticz.com/wiki/Developin ... #Callbacks for the onHeartbeat callback, the pertinent bit is:
Warning: Setting this interval to greater than 30 seconds will cause a 'thread seems to have ended unexpectedly' message to be written to the log file every 30 seconds. The plugin will function correctly but this message can not be suppressed because it is a standard warning from Domoticz that a piece of hardware may have stopped responding.
If a plugin wants to heartbeat every 100 seconds it should be coded with the heartbeat interval set to 10, 20 or 25 seconds and only take action every 6th, 5th or 4th time the callback is invoked.
thanks Dnpwwo...
so my interval was just long. with shorter times it works great

EscApe
Posts: 197
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe » Wednesday 13 June 2018 14:23

@Dnpwwo

Thanks! :? I knew that, but didn't take into account that people might set higher values.

I changed the plugin to handle longer poll intervals.

@devros and others...
There is a new version (0.4.1) on GitHub that lets you set higher (but predefined) intervals.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests