OPENWRT双频合一,2.4G自动切换5G
#!/bin/sh
# Modify by HSIAO-YANG CHEN
# crontab: * * * * * /bin/sh /usr/kickass.sh
#
#强信号 -60
STRONG=-60
#弱信号 -78
WEAK=-78
# 指定日志文件和5G频段设备MAC保存地址
logfile="/tmp/kickass.log"
dev5gmacfile="/usr/devmac_5g.db" ;
#默认自动添加计划任务
crontabsfile="/etc/crontabs/root"
if [ -z $( grep -i $0 "$crontabsfile" ) ] ; then
echo "* * * * * /bin/sh /usr/kickass.sh" >>$crontabsfile
echo "Add task scheduler to crontabs"
fi
#只能运行一个kickass任务
if [ "$( ps | grep -c "kick" )" -gt 3 ] ; then
echo "shell script already running "
else
datetime=`date +%Y-%m-%d_%H:%M:%S`
if [ ! -f "$logfile" ]; then
echo "creating kickass logfile: $logfile"
echo "$datetime: kickass log file created." > $logfile
fi
#踢除wlan-MAC列表
kicklist=""; while true ; do
datetime=`date +%Y-%m-%d_%H:%M:%S`
#从文件中读取5G频段的MAC地址
if [ -f "$dev5gmacfile" ] ; then
dev5gmac=$(cat "$dev5gmacfile")
fi
# wlanlist for multiple wlans (e.g., 5GHz/2.4GHz)
wlanlist="" ; wlanlist=$(ifconfig | grep -i wlan | grep -i -v sta | awk '{ print $1 }')
#echo "$wlanlist"
#获取多张网卡的SSID,为后面2.4G强信号判定是否踢除5G设备
ssid2g5g="" ; for wlan in $wlanlist ; do
ssid2g5g="$ssid2g5g $(iw $wlan info | grep ssid | awk '{ print $2 }')"
done
#echo "$ssid2g5g"
#loop for each wlan
wlan="" ; for wlan in $wlanlist ; do
maclist="" ; maclist=$(iw $wlan station dump | awk '/Station/{print $2}')
#5G频段的网卡AP
if [ "$(iw $wlan info | grep center1 | awk '{ print $2 }')" -gt "14" ];then
#loop for each associated client (station)
for mac in $maclist ; do
#新增5G频段的设备MAC
if [ -z "$(echo $dev5gmac | grep -i "$mac")" ] ; then
echo "$mac" >> $dev5gmacfile
echo "$datetime: Add new 5G wifi $mac success! " >> $logfile
fi
#获取信号强度
signal=$(iwinfo $wlan assoclist | awk 'BEGIN{IGNORECASE=1}/'$mac'/{print $2}')
#踢除弱信号设备,终端尝试连接2.4G频段
if [ "$signal" -le "$WEAK" ] ; then
#匹配2次踢除设备记录,启动踢除设备
if [ -n "$(echo $kicklist | grep -i "$wlan-$mac.*$wlan-$mac")" ] ; then
ubus call hostapd.$wlan del_client '{"addr":"'"$mac"'", "reason": 5, "deauth": True, "ban_time": 100}'
echo "$datetime: Kick $wlan MAC:$mac signal:$signal dBm switch 2.4G!" >> $logfile
#踢除设备记录删减
kicklist=$(echo $kicklist |sed "s/$wlan-$mac//g")
else
#踢除设备记录新增
kicklist="$kicklist$wlan-$mac"
fi
else
#踢除设备记录删减
kicklist=$(echo $kicklist |sed "s/$wlan-$mac//g")
fi
done
else
#2.4G频段的网卡AP
for mac in $maclist ; do
signal=$(iwinfo $wlan assoclist | awk 'BEGIN{IGNORECASE=1}/'$mac'/{print $2}')
#踢除弱信号设备
if [ "$signal" -le "$WEAK" ] ; then
#匹配2次踢除设备记录,启动踢除设备
if [ -n "$(echo $kicklist | grep -i "$wlan-$mac.*$wlan-$mac")" ] ; then
ubus call hostapd.$wlan del_client '{"addr":"'"$mac"'", "reason": 5, "deauth": True, "ban_time": 3000}'
echo "$datetime: Kick $wlan MAC:$mac signal:$signal dBm tool weak!" >> $logfile
kicklist=$(echo $kicklist |sed "s/$wlan-$mac//g")
else
kicklist="$kicklist$wlan-$mac"
fi
#踢除强信号,终端尝试连接5G频段(判断条件:信号比STRONG值大,终端MAC记录在5G频段里,有2个相同SSID的AP)
elif [ "$signal" -gt "$STRONG" -a -n "$(echo $dev5gmac | grep -i "$mac")" -a -n "$( echo "$ssid2g5g" | grep -i "$(iw $wlan info | grep ssid | awk '{ print $2 }').*$(iw $wlan info | grep ssid | awk '{ print $2 }')" )" ] ; then
if [ -n "$(echo $kicklist | grep -i "$wlan-$mac.*$wlan-$mac")" ] ; then
ubus call hostapd.$wlan del_client '{"addr":"'"$mac"'", "reason": 5, "deauth": True, "ban_time": 100}'
echo "$datetime: Kick $wlan MAC:$mac signal:$signal dBm switch 5G!" >> $logfile
kicklist=$(echo $kicklist |sed "s/$wlan-$mac//g")
else
kicklist="$kicklist$wlan-$mac"
fi
else
kicklist=$(echo $kicklist |sed "s/$wlan-$mac//g")
fi
done
fi
done
#监测周期时间
sleep 2s
done
fi
解决设备连接上2.4G频段后,不能自动切换回5G频段。
解决思路:
- 移动终端近距离连接上无线AP的5G频段,脚本将5G频段的设备MAC记录保存下来;
- 当连接上2.4G频段的移动终端,靠近无线AP时,当2.4G的信号强度高于-60dBm,且设备MAC符合5G频段的设备MAC记录,自动踢除2.4G频段的连接,让移动终端自动切换回5G频段;
- 当连接上5G频段的移动终端,远离无线AP时,当5G信号强度低于-75dBm,自动踢除5G频段的连接,让移动终端自动切换回信号强度较高的2.4G频段。
使用方法:
- 将脚本保存在/usr/目录下,且赋予可执行权限;
- 计划任务添加: * /bin/sh /usr/kickass.sh
脚本下载地址:kickass.zip
你好。大神能否适配一下padavan。
/bin/sh /usr/kickass.sh
sh: restart: unknown operand
脚本里没restart操作,如果有修改,要注意编码格式。