[OpenWrt] 自动检测丢包率并切换 Shadowsocks Server

手上有两个 VPS,有时候日本丢包会丢到生活不能自理,希望 Openwrt 能够自动把 shadowsocks 切换到美西的 VPS,于是,写了一个每 10 分钟测一次丢包率的 bash,如果其中一个 VPS 丢包率超过 10%,就切换到另一个。

2016/12/13 更新:

做了个简化版,不需要两个丢保值进行对比,只要有一个丢包,就切到另一个。

#!/bin/sh
## Auto-switch Shadowsocks Server
 
ABLENET="your ip"
 
NOW=`date +%Y/%m/%d-%H:%M:%S`
 
COUNT=40
 
P1=$(ping -c $COUNT $ABLENET | grep 'loss' | awk -F ',' '{ print $3 }' | awk -F "%" '{ print $1 }')
 
echo "Testing $COUNT pings"
 
CURRENT=$(cat /etc/ss/current.log)

echo "$P1% loss"
 
if [[ $P1 -lt 5 ]]; then
	if [[ $CURRENT -eq 1 ]]; then
		echo "$NOW: Kdata restores, $P1% loss. Will switch to back to kdata." >> /etc/ss/shadow.log
		/etc/init.d/shadowsocks stop
		sleep 1
		/etc/init.d/shadowsocks2 start
		echo "0" > /etc/ss/current.log
	fi
else
	if [[ $CURRENT -eq 0 ]]; then
		echo "$NOW: Kdata down, $P1% loss. Will switch to vnet." >> /etc/ss/shadow.log
		/etc/init.d/shadowsocks2 stop
		sleep 1
		/etc/init.d/shadowsocks start
		echo "1" > /etc/ss/current.log
	fi
fi
 
exit 0

原版:

#!/bin/sh
## Auto-switch Shadowsocks Server

ABLENET="218.251.113.22"
RAMNODE="168.235.77.18"

NOW=`date +%Y/%m/%d-%H:%M:%S`

COUNT=10

P1=$(ping -c $COUNT $ABLENET | grep 'loss' | awk -F ',' '{ print $3 }' | awk -F "%" '{ print $1 }')
P2=$(ping -c $COUNT $RAMNODE | grep 'loss' | awk -F ',' '{ print $3 }' | awk -F "%" '{ print $1 }')

echo "Testing $COUNT pings"

CURRENT=$(cat /etc/shadowsocks/current.log)

if [ "$P1" -eq "$P2" ]; then
	
	if [ $CURRENT -eq "1" ]; then
		echo "$NOW:BOTH are ok with loss of $P1%. Changing to ABLENET" >> /etc/shadowsocks/shadow.log
		/etc/init.d/shadowsocks2 stop
		sleep 1
		/etc/init.d/shadowsocks start
		echo "0" > /etc/shadowsocks/current.log
#	else
#		echo "$NOW:BOTH are ok with loss of $P1%. No change at the moment, stay with ABLENET" >> /etc/shadowsocks/shadow.log
	fi

else
	if [ "$P1" -lt "$P2" ]; then
		if [ "$P1" -le "10" ]; then
			if [ $CURRENT -eq "1" ]; then
				echo "$NOW:ABLENET is better, loss of ablenet is $P1%, loss of ramnode is $P2%. Will switch to ABLENET" >> /etc/shadowsocks/shadow.log
				/etc/init.d/shadowsocks2 stop
				sleep 1
				/etc/init.d/shadowsocks start
				echo "0" > /etc/shadowsocks/current.log
#			else
#				echo "$NOW:ABLENET is better, loss of ablenet is $P1%, loss of ramnode is $P2%. Will stay with ABLENET" >> /etc/shadowsocks/shadow.log
			fi
		fi

	else
		if [ "$P1" -gt "10" ]; then
			if [ $CURRENT -eq "0" ]; then
				echo "$NOW:RAMNODE is better, loss of ablenet is $P1%, loss of ramnode is $P2%. Will switch to RAMNODE" >> /etc/shadowsocks/shadow.log
				/etc/init.d/shadowsocks stop
				sleep 1
				/etc/init.d/shadowsocks2 start
				echo "1" > /etc/shadowsocks/current.log
#			else
#				echo "$NOW:RAMNODE is better, loss of ablenet is $P1%, loss of ramnode is $P2%. Will stay with RAMNODE" >> /etc/shadowsocks/shadow.log
			fi
		fi
	fi
fi


exit 0

以上代码放入/etc/shadowsocks/test2.sh 中,添加 0755 执行权限,再在文件夹下创建两个 log,一个叫 current.log,里面输入 0,一个叫 shadow.log,里面留空,权限不用改。

current.log 用于记录当前是用的第几个 Shadowsocks Server,shadow.log 用于记录切换 Server 的记录,暂时还不知道怎样增量写入并且限制 log 文件大小,决定交给 Michelle 帮忙。

然后加入到 crontab -e 中,注意必须紧跟最后一行,写完这行之后再加一个回车。

*/10 * * * * /etc/shadowsocks/test2.sh

启用计划任务:

/etc/init.d/cron enable
/etc/init.d/cron restart

检测 crontab 是否正常运行,用于 debug:

ps aux | grep cron
cat /etc/crontabs/root

跑了差不多一周,切换次数并不多,还算理想,主要是周末的晚上偶尔会抽一下:

2015/09/02-09:50:01:RAMNODE is better, loss of ablenet is 20%, loss of ramnode is 10%. Will switch to RAMNODE
2015/09/02-10:00:02:BOTH are ok with loss of 0%. Changing to ABLENET
2015/09/03-06:30:01:RAMNODE is better, loss of ablenet is 20%, loss of ramnode is 0%. Will switch to RAMNODE
2015/09/03-06:40:02:BOTH are ok with loss of 0%. Changing to ABLENET
2015/09/03-17:30:01:RAMNODE is better, loss of ablenet is 30%, loss of ramnode is 10%. Will switch to RAMNODE
2015/09/03-17:40:02:BOTH are ok with loss of 0%. Changing to ABLENET
2015/09/03-20:30:01:RAMNODE is better, loss of ablenet is 30%, loss of ramnode is 0%. Will switch to RAMNODE
2015/09/03-20:40:03:ABLENET is better, loss of ablenet is 0%, loss of ramnode is 10%. Will switch to ABLENET
2015/09/03-23:00:02:RAMNODE is better, loss of ablenet is 30%, loss of ramnode is 0%. Will switch to RAMNODE
2015/09/03-23:20:02:BOTH are ok with loss of 0%. Changing to ABLENET
2015/09/04-13:10:01:RAMNODE is better, loss of ablenet is 20%, loss of ramnode is 0%. Will switch to RAMNODE
2015/09/04-13:30:01:BOTH are ok with loss of 0%. Changing to ABLENET
2015/09/04-14:00:01:RAMNODE is better, loss of ablenet is 20%, loss of ramnode is 0%. Will switch to RAMNODE
2015/09/04-14:10:01:BOTH are ok with loss of 0%. Changing to ABLENET
2015/09/06-13:10:01:RAMNODE is better, loss of ablenet is 30%, loss of ramnode is 10%. Will switch to RAMNODE
2015/09/06-13:40:02:ABLENET is better, loss of ablenet is 0%, loss of ramnode is 10%. Will switch to ABLENET
2015/09/06-19:00:01:RAMNODE is better, loss of ablenet is 50%, loss of ramnode is 0%. Will switch to RAMNODE
2015/09/06-19:10:01:BOTH are ok with loss of 0%. Changing to ABLENET

1 thought on “[OpenWrt] 自动检测丢包率并切换 Shadowsocks Server”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.