【已更改为wifi蓝牙结合检测】linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

回复
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

【已更改为wifi蓝牙结合检测】linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 DT27 »

=======================================================
更新:2017-06-30 11:15:57
此帖原方法已弃用,改为blindlight推荐的方法,此方法结合wifi与蓝牙,实现每分钟检测,延时低。
新方法原帖:https://www.domoticz.com/forum/viewtopi ... 23&t=12570
简单介绍:
新方法通过fping来ping手机无线IP来检测wifi在线状态,通过l2ping来检测手机蓝牙mac(只需要手机开启蓝牙,不需要配对)。
脚本中先ping一次wifiIP,如果不通,再检测一次蓝牙,如果还是不通,重复监测一遍wifi蓝牙。
将脚本添加到系统计划任务,每分钟执行一次。可以保证状态更改后一分钟内更新到domoticz。
脚本代码

代码: 全选

#!/bin/bash

# Set Parameters
Name='Bens Mobile Phone(名称随便写)'
domoticzserverip='127.0.0.1:8080' #domoticz服务器IP:端口,这里是
IDX='108'
IP='192.168.0.80' #手机wifiIP,在路由中配置静态IP
bluetoothmac='04:9C:02:57:23:F5' #手机蓝牙mac地址,在关于手机中查看

# First network ping attempt
fping -c1 -b 32 -t1000 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ] ; then
device=$(echo "On")
technology="Wifi 1st attempt"
success="yes"
sleep 1
else
success="no"
technology=''
fi

# First bluetooth ping attempt
if [[ $success != 'yes' ]]; then
bt1=$(l2ping -c1 -s32 -t1 "$bluetoothmac" > /dev/null && echo "On" || echo "Off")
if [[ $bt1 == 'On' ]]; then
device=$(echo "On")
technology="Bluetooth 1st attempt"
success="yes"
fi
fi

# Second network ping attempt
if [[ $success != 'yes' ]]; then
fping -c1 -b 32 -t1000 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ] ; then
device=$(echo "On")
technology="Wifi 2nd attempt"
success="yes"
fi
fi

# Second bluetooth ping attempt
if [[ $success != 'yes' ]]; then
bt1=$(l2ping -c1 -s32 -t1 "$bluetoothmac" > /dev/null && echo "On" || echo "Off")
if [[ $bt1 == 'On' ]]; then
device=$(echo "On")
technology="Bluetooth 2nd attempt"
success="yes"
fi
fi

# If the device is still offline, declare it for processing
if [[ $success != 'yes' ]]; then
device=$(echo "Off")
fi

# Check Online / Offline state of Domoticz device
domoticzstatus=$(curl -s "http://"$domoticzserverip"/json.htm?type=devices&rid="$IDX"" | grep '"Data" :' | awk '{ print $3 }' | sed 's/[!@#\$%",^&*()]//g')
# Compare ping result to Domoticz device status
if [ "$device" = "$domoticzstatus" ] ; then
echo "Status in sync $technology"
else
echo "Status out of sync, correcting..."
if [ "$device" = On ] ; then
echo "$Name" "Online"
curl -s "http://"$domoticzserverip"/json.htm?type=command&param=switchlight&idx="$IDX"&switchcmd=On" 2>/dev/null 1>/dev/null
else
echo "$Name" "Offline"
curl -s "http://"$domoticzserverip"/json.htm?type=command&param=switchlight&idx="$IDX"&switchcmd=Off" 2>/dev/null 1>/dev/null
fi
fi
=======================================================

通过监测手机在不在线可间接判断人在不在家
  • 此脚本在树莓派上运行正常
  • 无需定时执行,仅设置开机启动即可
  • 局域网设备扫描使用arp-scan,安装命令 sudo apt-get install arp-scan
  • 由于iOS设备的wifi休眠特性,设备离线时无法立即判定离线
具体信息请查看代码中的注释。
person.py

代码: 全选

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import subprocess
import urllib2
from time import sleep
from threading import Thread

#此脚本在树莓派上运行正常
#无需定时执行,仅设置开机启动即可
#需要先安装Python模块urllib2
#局域网设备扫描使用arp-scan,安装命令 sudo apt-get install arp-scan
#由于iOS设备的wifi休眠特性,设备离线时无法立即判定离线

#Domoticz服务器地址及端口号
domoticzserver = "127.0.0.1:8080"
#此方法向Domoticz服务器发送请求
def domoticzrequest (url):
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    return response.read()

# 要检测的设备
device = ["myPhone","herPhone"]

# 设备是否会将WIFI休眠,iOS设备请设置1
type = ["0","1"]

# 设备MAC地址
address = ["00:00:00:00:00:00","00:00:00:00:00:00"]

# 设备在Domoticz中对应虚拟开关的IDX
idxs = ["21","22"]

#延时,防止刚开机时获取失败
#print("30秒后开始运行")
sleep(30)

# 记录设备状态
firstRun = [1] * len(device)
presentSent = [0] * len(device)
notPresentSent = [0] * len(device)
counter = [0] * len(device)


# Function that checks for device presence
def whoIsHere(i):

    #print("休息30秒,以供主线程完成arp-scan并输出")
    sleep(30)

    # Loop through checking for devices and counting if they're not present
    while True:

        # 键盘输入后退出
        if stop == True:
            print "退出"
            exit()
        else:
            pass

        # 设备在线
        #print("开始分析output数据")
        if address[i] in output:
            # 检测到设备在线
            #print(device[i] + " 已连接"+bytes(i))
            if presentSent[i] == 0:
                # 向外输出
                #print(device[i] + " 向外输出设备在线")
                domoticzrequest("http://"+domoticzserver+"/json.htm?type=command&param=switchlight&idx="+idxs[i]+"&switchcmd=On")
                # Reset counters so another stream isn't sent if the device
                # is still present
                firstRun[i] = 0
                presentSent[i] = 1
                notPresentSent[i] = 0
                counter[i] = 0
                #print("输出在线状态后休息15分钟")
                sleep(900)
            else:
                #print("之前已向外输出,重置计数器并休息15分钟")
                counter[i] = 0
                sleep(900)
        else:
            #print(device[i] + " 离线"+bytes(i))
            
            # Wifi会休眠的设备
            if type[i] == 1:
                # 第一次执行或累计15分钟内30次扫描均离线
                if counter[i] == 30 or firstRun[i] == 1:
                    firstRun[i] = 0
                    if notPresentSent[i] == 0:
                        # 未发送离线状态
                        #print(device[i] + " 向外输出设备离线")
                        domoticzrequest("http://"+domoticzserver+"/json.htm?type=command&param=switchlight&idx="+idxs[i]+"&switchcmd=Off")
                        # 设置已输出
                        notPresentSent[i] = 1
                        presentSent[i] = 0
                        counter[i] = 0
                    else:
                        # 发送过离线状态
                        counter[i] = 0
                        sleep(30)

                else:
                    # Count how many 30 second intervals have happened since the device
                    # disappeared from the network
                    counter[i] = counter[i] + 1
                    #print(device[i] + "计数器 " + str(counter[i]))
                    #print("休息30秒")
                    sleep(30)
            else:
                # 第一次执行或累计5分钟内10次扫描均离线
                if counter[i] == 10 or firstRun[i] == 1:
                    firstRun[i] = 0
                    if notPresentSent[i] == 0:
                        # 未发送离线状态
                        #print(device[i] + " 向外输出设备离线")
                        domoticzrequest("http://"+domoticzserver+"/json.htm?type=command&param=switchlight&idx="+idxs[i]+"&switchcmd=Off")
                        # 设置已输出
                        notPresentSent[i] = 1
                        presentSent[i] = 0
                        counter[i] = 0
                    else:
                        # 发送过离线状态
                        counter[i] = 0
                        sleep(30)

                else:
                    # Count how many 30 second intervals have happened since the device
                    # disappeared from the network
                    counter[i] = counter[i] + 1
                    #print(device[i] + "计数器 " + str(counter[i]))
                    #print("休息30秒")
                    sleep(30)


# Main thread
try:
    global stop
    stop = False

    # Start the thread(s)
    for i in range(len(device)):
        t = Thread(target=whoIsHere, args=(i,))
        #print("开始进程"+bytes(i))
        t.start()

    while True:
        global output
        # 获取局域网所有设备
        output = subprocess.check_output("sudo arp-scan --interface=wlan0 --localnet", shell=True)
        # 每次扫描间隔30秒
        #print("本次扫描结束并已输出到output,30秒后进行下一次扫描...")
        sleep(30)

except KeyboardInterrupt:
    stop = True
    exit()
设置开机启动方法(树莓派):
①为了让py脚本保持后台运行,可以新建一个person.sh文件,文件内容

代码: 全选

#! /bin/sh

python /home/pi/person.py &
②增加执行权限

代码: 全选

sudo chmod +x /home/pi/person.sh
③然后将person.sh设置为开机启动

代码: 全选

sudo nano /etc/rc.local
在exit 0上方加入

代码: 全选

sudo sh /home/pi/domoticz/scripts/person.sh
屏幕快照 2017-06-07 13.32.40.jpg
屏幕快照 2017-06-07 13.32.40.jpg (59.92 KiB) 查看 40985 次
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

Re: linux系统监测wifi设备在线脚本

帖子 chengka3463 »

System Alive Checker (Ping) 系统硬件里可以选择的,设置轮询时间和Ping超时,简单。这个脚本有什么优势吗?
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: linux系统监测wifi设备在线脚本

帖子 DT27 »

chengka3463 写了: 周三 6月 07, 2017 13:47 System Alive Checker (Ping) 系统硬件里可以选择的,设置轮询时间和Ping超时,简单。这个脚本有什么优势吗?
只是提供另一种思路 :)
另外有的设备不支持ping。
blindlight
帖子: 98
注册时间: 周四 3月 30, 2017 00:03

Re: linux系统监测wifi设备在线脚本

帖子 blindlight »

chengka3463 写了: 周三 6月 07, 2017 13:47 System Alive Checker (Ping) 系统硬件里可以选择的,设置轮询时间和Ping超时,简单。这个脚本有什么优势吗?
提供一个老外的手机presence,fping手机wifi和蓝牙
https://www.domoticz.com/forum/viewtopi ... t=presence
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 chengka3463 »

基础小白,这个脚本要好好研究一整才能借鉴啦,谢谢
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 DT27 »

chengka3463 写了: 周三 6月 07, 2017 14:15 基础小白,这个脚本要好好研究一整才能借鉴啦,谢谢
可以用楼上 blindlight 发那个,比这个简单,而且反应快,就是得一直开着蓝牙。
pizixiaotian
帖子: 29
注册时间: 周三 4月 05, 2017 22:15

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 pizixiaotian »

群晖用户没法通过apt-get命令安装arp-scan。而且群晖上面也没有蓝牙。怎么办啊
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 DT27 »

pizixiaotian 写了: 周三 6月 07, 2017 22:32 群晖用户没法通过apt-get命令安装arp-scan。而且群晖上面也没有蓝牙。怎么办啊
可以手动安装
Run autoreconf --install to generate a viable ./configure file
Run ./configure to generate a makefile for your system
Run make to build the project
Optionally run make check to verify that everything works as expected
Run make install to install (you'll need root or sudo for this part)
用root用户登录后执行

代码: 全选

git clone https://github.com/royhills/arp-scan.git
cd arp-scan
autoreconf --install
./configure
make install
 
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 chengka3463 »

先安装Python模块urllib2 怎么安装啊?
#Domoticz服务器地址及端口号
domoticzserver = "127.0.0.1:8080"
这里要改自己的 ip吗?
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 DT27 »

chengka3463 写了: 周二 6月 13, 2017 18:33 先安装Python模块urllib2 怎么安装啊?
#Domoticz服务器地址及端口号
domoticzserver = "127.0.0.1:8080"
这里要改自己的 ip吗?
python模块一般安装方法

代码: 全选

sudo pip install urllib2
多百度下吧。
127.0.0.1代表本机,不用改。
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 chengka3463 »

百度过了 一样指令 返回结果
$ sudo pip install urllib2
^[[B/usr/local/lib/python2.7/dist-packages/urllib3/contrib/socks.py:37: DependencyWarning: SOCKS support in urllib3 requires the installation of optional dependencies: specifically, PySocks. For more information, see https://urllib3.readthedocs.io/en/lates ... ks-proxies
DependencyWarning
Downloading/unpacking urllib2
^[[A Real name of requirement urllib2 is urllib3
Could not find any downloads that satisfy the require
Cleaning up...
No distributions at all found for urllib2
Storing debug log for failure in /root/.pip/pip.log
所以来论坛资讯啊!安装不上
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 DT27 »

chengka3463 写了: 周三 6月 14, 2017 09:18 百度过了 一样指令 返回结果
$ sudo pip install urllib2
^[[B/usr/local/lib/python2.7/dist-packages/urllib3/contrib/socks.py:37: DependencyWarning: SOCKS support in urllib3 requires the installation of optional dependencies: specifically, PySocks. For more information, see https://urllib3.readthedocs.io/en/lates ... ks-proxies
DependencyWarning
Downloading/unpacking urllib2
^[[A Real name of requirement urllib2 is urllib3
Could not find any downloads that satisfy the require
Cleaning up...
No distributions at all found for urllib2
Storing debug log for failure in /root/.pip/pip.log
所以来论坛资讯啊!安装不上
查了下,urllib2一般默认就有的,不需要安装,直接执行下面代码测试urllib2能不能用。

代码: 全选

python2.7 -m urllib2 &>- && echo you can use urllib2 || echo urllib2 is not available
屏幕快照 2017-06-14 14.40.13.jpg
屏幕快照 2017-06-14 14.40.13.jpg (22.62 KiB) 查看 41027 次
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 chengka3463 »

脚本执行进程怎么查看? 依然不起作用,在线设备不显示。 还是新建虚拟开关时,硬件选择有问题?
1BF9MNKH}GB095Y}L{$$2LO.png
1BF9MNKH}GB095Y}L{$$2LO.png (34.11 KiB) 查看 41026 次
~C5JWF8}3}UZZ%A7{ZBB7XQ.png
~C5JWF8}3}UZZ%A7{ZBB7XQ.png (8.94 KiB) 查看 41026 次
S8]09ECEC(683OU[U}1KNS2.png
S8]09ECEC(683OU[U}1KNS2.png (11.33 KiB) 查看 41026 次
DR4(U})U3SB{2L_M@95P2%G.png
DR4(U})U3SB{2L_M@95P2%G.png (9.86 KiB) 查看 41026 次
两个亮的是同一设备设置的System Alive Checker (Ping)
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 DT27 »

chengka3463 写了: 周四 6月 15, 2017 17:44 脚本执行进程怎么查看? 依然不起作用,在线设备不显示。 还是新建虚拟开关时,硬件选择有问题?1BF9MNKH}GB095Y}L{$$2LO.png~C5JWF8}3}UZZ%A7{ZBB7XQ.pngS8]09ECEC(683OU[U}1KNS2.pngDR4(U})U3SB{2L_M@95P2%G.png
两个亮的是同一设备设置的System Alive Checker (Ping)
直接执行python /home/pi/person.py看看什么结果
chengka3463
帖子: 37
注册时间: 周四 6月 01, 2017 19:32

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 chengka3463 »

脚本确实是执行了。查看日志都是显示off,但设备都是正常在线的。哪里填错了,还是mac地址格式有错误?
sxren1
帖子: 51
注册时间: 周三 5月 24, 2017 10:04

Re: linux系统监测wifi设备在线脚本(arp-scan扫描局域网mac)

帖子 sxren1 »

chengka3463 写了: 周六 6月 17, 2017 16:12 脚本确实是执行了。查看日志都是显示off,但设备都是正常在线的。哪里填错了,还是mac地址格式有错误?
我的也是这个情况,你现在解决了吗,请说明一下方法
回复