利用 openwrt 路由器+树莓派+bash 脚本实时发现隔壁老王来访并发送邮件+短信通知,隔壁老王连上我路由器可以发现并收到通知,一直在发送,根本停不下来,求消停的方法

2016-12-30 11:45:58 +08:00
 zgzh

根据原文地址: http://blog.csdn.net/c80486/article/details/43503301

作者原文中有说到:如果一个手机持续连接 WIFI ,则每次查找时均可找到它,如果不断发短信是很烦人的,所以程序中采用了一个变量 last_time 记录上一次找到 MAC 码的时间,如果是连续找到,则不会触发网页。 但是目前的实际情况是每分钟就发一个短信,根本停不下来,求消停的方法,比如发送 2 次后停止发送。代码如下

#!/bin/sh
MAC="E0:19:1D:E4:22:25"
URL="http://192.168.31.131/wifi/find.php?mac="

#check duration, in seconds
interval=2

#To avoid notify continously, last_time is the last time we find the MAC
last_time=$(date +%y%m%d%H%M%S)
let last_time=last_time-interval-interval

#Function: Find MAC address in associated users , query the url while matched. return 1 if found, return 0 if not found
find_mac() {
#Use ifconfig to find interface which name starts with wl
ifconfig | grep wl[0-9] | awk '{print $1}' | while read WLAN
do
#Use iwinfo to find MAC address of connected users
iwinfo $WLAN assoclist | grep dBm | awk '{print $1}' | while read MAC1
do
#if MAC1 address is the target we want
if [ $MAC1 = $MAC ]
then
#Calculate time passed since last_time
this_time=$(date +%y%m%d%H%M%S)
let time_passed=this_time-last_time
let interval_min=interval+1
if [ $time_passed -gt $interval_min ]
then
#construct the url , append the MAC address to the end
QUREY_URL="${URL}${MAC}"
#Use wget to query the url
wget -q -O web_response $QUREY_URL
echo "FIND $MAC"
return 1
fi
fi
done
done
return 0
}

#Main program: it's a dead loop, exec find_mac() every n seconds(defined by interval)
while [ 1 -lt 2 ]
do
if find_mac
then
#if found, update last_time
last_time=$(date +%y%m%d%H%M%S)
fi
sleep $interval
done

1730 次点击
所在节点   Bash
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/331191

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX