我使用发现的here脚本检测Raspberry Pi附近的信标。这是脚本代码
#!/bin/bash
# iBeacon Scan by Radius Networks
if [[ $1 == "parse" ]]; then
packet=""
capturing=""
count=0
while read line
do
count=$[count + 1]
if [ "$capturing" ]; then
if [[ $line =~ ^[0-9a-fA-F]{2}\ [0-9a-fA-F] ]]; then
packet="$packet $line"
else
if [[ $packet =~ ^04\ 3E\ 2A\ 02\ 01\ .{26}\ 02\ 01\ .{14}\ 02\ 15 ]]; then
UUID=`echo $packet | sed 's/^.\{69\}\(.\{47\}\).*$/\1/'`
MAJOR=`echo $packet | sed 's/^.\{117\}\(.\{5\}\).*$/\1/'`
MINOR=`echo $packet | sed 's/^.\{123\}\(.\{5\}\).*$/\1/'`
POWER=`echo $packet | sed 's/^.\{129\}\(.\{2\}\).*$/\1/'`
UUID=`echo $UUID | sed -e 's/\ //g' -e 's/^\(.\{8\}\)\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)\(.\{12\}\)$/\1-\2-\3-\4-\5/'`
MAJOR=`echo $MAJOR | sed 's/\ //g'`
MAJOR=`echo "ibase=16; $MAJOR" | bc`
MINOR=`echo $MINOR | sed 's/\ //g'`
MINOR=`echo "ibase=16; $MINOR" | bc`
POWER=`echo "ibase=16; $POWER" | bc`
POWER=$[POWER - 256]
if [[ $2 == "-b" ]]; then
echo "$UUID $MAJOR $MINOR $POWER"
else
echo "UUID: $UUID MAJOR: $MAJOR MINOR: $MINOR POWER: $POWER"
fi
fi
capturing=""
packet=""
fi
fi
if [ ! "$capturing" ]; then
if [[ $line =~ ^\> ]]; then
packet=`echo $line | sed 's/^>.\(.*$\)/\1/'`
capturing=1
fi
fi
done
else
sudo hcitool lescan --duplicates 1>/dev/null &
if [ "$(pidof hcitool)" ]; then
sudo hcidump --raw | ./$0 parse $1
fi
fi
脚本正常工作并显示UUID,Major,Minor和Power值。但是,我还想获得每个信标的MAC地址。我几乎可以肯定,因为命令中使用了该值
脚本sudo hcitool lescan --duplicates
向我展示MAC连衣裙,但我无法将其添加到此行
echo "$UUID $MAJOR $MINOR $POWER"
有人知道如何获得价值吗?
答案 0 :(得分:0)
您可以使用这些代码行
# parse line from 21 and 17 characters (including spaces), then remove the spaces
REVERSE_MAC=`echo $packet | sed 's/^.\{21\}\(.\{17\}\).*$/\1/' | sed 's/\ //g'`
# the MAC was reversed so I have to put it in order, always by 2 characters
MAC=`echo "${REVERSE_MAC:10:2}:${REVERSE_MAC:8:2}:${REVERSE_MAC:6:2}:${REVERSE_MAC:4:2}:${REVERSE_MAC:2:2}:${REVERSE_MAC:0:2}"`
所以整个代码是:
#!/bin/bash
# iBeacon Scan by Radius Networks
if [[ $1 == "parse" ]]; then
packet=""
capturing=""
count=0
while read line
do
count=$[count + 1]
if [ "$capturing" ]; then
if [[ $line =~ ^[0-9a-fA-F]{2}\ [0-9a-fA-F] ]]; then
packet="$packet $line"
else
if [[ $packet =~ ^04\ 3E\ 2A\ 02\ 01\ .{26}\ 02\ 01\ .{14}\ 02\ 15 ]]; then
UUID=`echo $packet | sed 's/^.\{69\}\(.\{47\}\).*$/\1/'`
MAJOR=`echo $packet | sed 's/^.\{117\}\(.\{5\}\).*$/\1/'`
MINOR=`echo $packet | sed 's/^.\{123\}\(.\{5\}\).*$/\1/'`
POWER=`echo $packet | sed 's/^.\{129\}\(.\{2\}\).*$/\1/'`
UUID=`echo $UUID | sed -e 's/\ //g' -e 's/^\(.\{8\}\)\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)\(.\{12\}\)$/\1-\2-\3-\4-\5/'`
MAJOR=`echo $MAJOR | sed 's/\ //g'`
MAJOR=`echo "ibase=16; $MAJOR" | bc`
MINOR=`echo $MINOR | sed 's/\ //g'`
MINOR=`echo "ibase=16; $MINOR" | bc`
POWER=`echo "ibase=16; $POWER" | bc`
POWER=$[POWER - 256]
# CAPTURE MAC ADDRESS
# parse line from 21 and 17 characters (including spaces), then remove the spaces
REVERSE_MAC=`echo $packet | sed 's/^.\{21\}\(.\{17\}\).*$/\1/' | sed 's/\ //g'`
# the MAC was reversed so I have to put it in order, always by 2 characters
MAC=`echo "${REVERSE_MAC:10:2}:${REVERSE_MAC:8:2}:${REVERSE_MAC:6:2}:${REVERSE_MAC:4:2}:${REVERSE_MAC:2:2}:${REVERSE_MAC:0:2}"`
if [[ $2 == "-b" ]]; then
echo "$UUID $MAJOR $MINOR $POWER"
else
echo "UUID: $UUID MAJOR: $MAJOR MINOR: $MINOR POWER: $POWER MAC: $MAC"
fi
fi
capturing=""
packet=""
fi
fi
if [ ! "$capturing" ]; then
if [[ $line =~ ^\> ]]; then
packet=`echo $line | sed 's/^>.\(.*$\)/\1/'`
capturing=1
fi
fi
done
else
sudo hcitool lescan --duplicates 1>/dev/null &
if [ "$(pidof hcitool)" ]; then
sudo hcidump --raw | ./$0 parse $1
fi
fi