Skip to content

Auto Switch Servers

Jian Chang edited this page Oct 18, 2018 · 5 revisions

0.说明

如果你有多个服务器, 并希望在当前服务器连接失败后自动切换下一个服务器, 那么可以使用此脚本.

1.准备工作

opkg update
opkg install wget ca-bundle ca-certificates

2.保存以下内容为 shell 脚本文件

#!/bin/sh

NAME=shadowsocks

wget --spider --quiet --timeout=10 https://www.facebook.com/
if [ "$?" != "0" ]; then
    wget --spider --quiet --timeout=10 https://www.baidu.com/
    if [ "$?" = "0" ]; then
        servers=$(uci -n export $NAME | awk '$2=/servers/{print substr($3,2,9)}')
        next_server=$(uci get $NAME.@transparent_proxy[0].main_server | awk -v servers="$servers" '{split(servers,V);for (i in V) {if (V[i] == $1) {print V[i % 11 + 1];break}}}')
        if [ -n "$next_server" ]; then
            uci delete $NAME.@transparent_proxy[0].main_server
            for c in $(seq 2); do # 默认会启动两个进程,可以更改数字调整
                uci add_list $NAME.@transparent_proxy[0].main_server=$next_server
            done
            uci commit $NAME
            /etc/init.d/$NAME restart
        fi
    fi
fi

3.添加以下计划任务(每分钟)

*      *     *     *     *     /path/to/shell-script >/dev/null 2>&1