#! /bin/bashconf=/etc/config/passwall
# 获取所有节点
function getNodes(){f=$1;ret=`cat $f | grep "config nodes" | awk '{print $3}' | sed -r "s/'//g"`;echo $ret | tr " " " ";
}
# 移动旧的自动切换节点
function removeAutoSwitchNode(){f=$1;p=1;stp=0;while IFS= read -r line; doif [[ $stp == "1" ]]; thenret=`echo "$line" | grep 'list tcp_node'`;if [[ $ret != "" ]]; thencontinue;fistp=0;fiecho $line >> "$f.$p";ret=`echo "$line" | grep 'config auto_switch'`;if [[ $ret != "" ]]; thenp=$((p+1));stp=1;fidone < "$f"mv $f "$f.bak"cat "$f.1" > "$f";rm "$f.1" -rf;cat "$f.2" >> "$f";rm "$f.2" -rf;
}
# 添加自动切换节点
function addAutoSwitchNode(){f="$1";n="$2";mv $f "$f.bak1";cat "$f.bak1" | sed -r "s/(config auto_switch)/\1\n list tcp_node '$n'/g" > $f;rm "$f.bak1" -rf;
}
# 主入口程序
function main(){f=$1;nodes=$(getNodes "$f");removeAutoSwitchNode "$f";for n in ${nodes[@]}; doaddAutoSwitchNode "$f" "$n";donesh /usr/share/passwall/app.sh stop;lua /usr/share/passwall/subscribe.lua startsh /usr/share/passwall/app.sh start;
}main "$conf"
将脚本上传到openwrt服务器上。然后通过corntab -e 定时启动