我想分享我自己的wifi,但是又不能让他人影响到自己。

发现信号弱的客户端连接到WiFi后,如果有传输数据,会导致整个2.4G的WiFi延迟急剧增加。所以希望路由自动剔除弱信号的设备,然而小米路由器和openwrt还是有较大的差距的,相关命令有所不同。就自己捣鼓了个ash脚本。

Github: https://github.com/XinRoom/banwctr

#!/bin/ash

# 禁止信号弱的客户端联网,防止发生全局性的拥塞
# 使用macfilter和parentalctl的firewall规则进行限制

maclist=`iwinfo wl1 assoclist | sed -n '8,$p' | cut -f1 -d " "`  #wl1为2.4G的信号
len=`iwinfo wl1 assoclist | sed -n '8,$p'  | sed -n '$='`

rxlist=`iwinfo wl1 assoclist | sed -n '8,$p' | cut -f7 -d " "`
rxlistsum=`iwinfo wl1 assoclist | sed -n '8,$p' | cut -f7 -d " " | sed -n '$='`

rxref="-80" #小于改值的客户端会被禁止连网

for i in `seq $len`;
do
	let "ii =  $i - 1"
	let "macpos =  $ii  * 17 + $ii"
	mac=${maclist:$macpos:17}
	
	let "rxpos =  $ii  * 3+ $ii"
	rx=${rxlist:$rxpos:3}

	unset macth

        if [[ $rx -le $rxref ]]; then
		echo $mac $rx
		
		let "j = 0"
		while  [ "`uci get macfilter.@mac[${j}]`" ==  "mac" ];
		do
			if  [ "`uci get macfilter.@mac[${j}].mac |  tr '[a-z]' '[A-Z]'`" ==  "$mac" ]; then
				let "macth = $j"
			fi
			let "j = $j + 1"
		done

		if [ -z $macth ];then
			let "macth = $j"
			uci add macfilter mac
		fi

		if  [ "`uci get macfilter.@mac[${macth}].wan`" ==  "no" ]; then
			continue
		fi

		uci set macfilter.@mac[$macth].mac=$mac
		uci set macfilter.@mac[$macth].lan=no
		uci set macfilter.@mac[$macth].wan=no
		uci set macfilter.@mac[$macth].admin=yes
		uci set macfilter.@mac[$macth].pridisk=no

		if  [ "`uci get parentalctl.$(echo $mac | tr -d ':')`" ==  "summary" ]; then
			uci set parentalctl.$(echo $mac | tr -d ':').mode=limited
		else
			uci set parentalctl.$(echo $mac | tr -d ':')=summary
			uci set parentalctl.$(echo $mac | tr -d ':').mac=$mac
			uci set parentalctl.$(echo $mac | tr -d ':').disabled=0
			uci set parentalctl.$(echo $mac | tr -d ':').mark=1
			uci set parentalctl.$(echo $mac | tr -d ':').mode=limited
		fi
		
		uci commit
	else
		echo $mac $rx
		
		let "j = 0"
		while  [ "`uci get macfilter.@mac[${j}]`" ==  "mac" ];
		do
			if  [ "`uci get macfilter.@mac[${j}].mac |  tr '[a-z]' '[A-Z]'`" ==  "$mac" ]; then
				let "macth = $j"
			fi
			let "j = $j + 1"
		done

		if [ -z "$macth" ];then
			continue
		fi

		if  [ "`uci get macfilter.@mac[${macth}].wan`" ==  "yes" ]; then
			continue
		fi

		uci set macfilter.@mac[$macth].lan=yes
		uci set macfilter.@mac[$macth].wan=yes
		
		uci set parentalctl.$(echo $mac | tr -d ':').mode=none

		uci commit
	fi
	
	# 重载规则
	/lib/firewall.sysapi.loader parentalctl
	/lib/firewall.sysapi.loader macfilter

done

Usage

  • 将脚本放入小米路由器可写文件夹( eg: /etc/banwctr.sh )中,并且赋予文件可执行的权限
  • 使用crontab自动化执行(每过30s执行一次)
    * * * * * sleep 30; /etc/banwctr.sh >/dev/null 2>&1

更新于:2018-08-20