由于很多你懂得的原因,我们不得不通过一些技术手段来访问技术网站。由于线路不稳定或者很多其他的因素,会引起PPTP的假死和中断。所以我在服务器上写了一个PPTP Client的自动检测拉起脚本,来确保线路畅通。
不多说了,上代码。
#!/bin/bash SCRIPT_PIDS=$(pidof -x $0) if [ `echo $SCRIPT_PIDS | awk -F' ' '{print NF; exit}'` -gt 1 ]; then echo "VPN Guard is already running..." exit fi function select_fast_server() { LIST="" for ip in `cat /etc/ppp/peers/vpn-server` do LIST=$LIST"$ip `ping -c 3 -t 3 $ip | grep 'time=' | awk '{print $7}' | cut -f 2 -d '=' | awk '{sum += $1};END { if (NR > 0) print sum / NR }'`\n" done FAST_HOST=$(echo -e $LIST | awk 'NR == 1 {ip = $1; min = 99999} $2 > 0 && $2 < min {ip = $1; min = $2} END {print ip}') sed -i "s|pptp.*--nolaunchpppd|pptp ${FAST_HOST} --nolaunchpppd|g" /etc/ppp/peers/vpn echo "Fast vpn server is ${FAST_HOST}" } while [ true ]; do if [ `ps -ef | grep "pptp" | grep -v grep | wc -l` -lt 1 ]; then select_fast_server sleep 1 /usr/sbin/pppd call vpn sleep 15 else if [ `httping -G -c 3 -i 3 -t 2 http://www.google.com 2>&1 | grep "connected to" | wc -l` -lt 1 ]; then kill -9 `ps -ef | grep -E '(pptp|pppd)' | grep -v grep | awk '{print $2}'` fi fi sleep 3 done