根据原文地址: 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