- #!/bin/bash
- ##############################################################################
- # NOTE:
- # The test system is Ubuntu12.04
- #
- # This Scripts all rights reserved deserved by MickeyZZC
- # Copyright © 2013
- #
- ##############################################################################
- PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
- export PATH
- clear;
- programs="awk sed grep cat wget cut touch proxychains openvpn mutt ps killall"
- for profile in $programs
- do
- which $profile > /dev/null 2>&1
- if [ "$?" != "0" ]; then
- echo -e "Your system do not have $profile , Please check your system ."
- exit 1
- fi
- done
- username="lontestt"
- pwd="c54239c4"
- xmlurl="http://lonlife.net/lonlife.xml"
- ss5cnf="/etc/proxychains.conf"
- tmppath="/home/path"
- statstime=$(date +"%Y-%m-%d-%H:%M")
- #myip=`wget -q -O - www.123cha.com|grep "www.123cha.com/ip"|cut -d\\> -f4|cut -d\\< -f1`
- myip=`curl -s http://220.181.11.98/cityjson?ie=utf-8|awk -F '\\"' '{print $4}'`
- [ -f $tmppath/auth.txt ] || touch $tmppath/auth.txt && echo -e "$username\\n$pwd">$tmppath/auth.txt
- function xmlload(){
- #用缓存文件方式存储数据
- # wget -q -O - $xmlurl|awk -F '\\"' ' {if(/proxytype="1"/){printf "socks5 %s %s\\n",$4,$6}else if(/proxytype="3"/){printf "remote %s %s\\n",$4,$6}}' > $tmppath/tmpxml.txt
- curl -s $xmlurl|awk -F '\\"' ' {if(/proxytype="1"/){printf "socks5 %s %s\\n",$4,$6}else if(/proxytype="3"/){printf "remote %s %s\\n",$4,$6}}' > $tmppath/tmpxml.txt
- }
- function socket_test(){
- cat $tmppath/tmpxml.txt| while read line
- do
- if [ "socks5" = ${line%% *} ] ; then
- txt="$line $username $pwd"
- sed -i /socks5/d $ss5cnf
- sed -i "N;/\\[ProxyList\\]/a $txt" $ss5cnf
- rmip=`proxychains curl -s http://220.181.11.98/cityjson?ie=utf-8|awk -F '\\"' '{print $4}'`
- if [ $myip != $rmip ] ; then
- echo "$statstime,ss5,${line#* },ok" >> $tmppath/sockslog.csv
- else
- echo "$statstime,ss5,${line#* },down" >> $tmppath/sockslog.csv
- fi
- elif [ "remote" = ${line%% *} ] ; then
- pid_value=$(ps -A|grep openvpn|awk '{print $1}')
- [ $pid_value = ""] || kill -15 $pid_value &&
- vpn_socket &
- fi
- done
- }
- function vpn_socket(){
- #检测证书是否存在
- [ -f "$tmppath/ca.crt" -a -f "$tmppath/lonlife.key" -a -f "$tmppath/lonlife.crt" ] || exit 0 &&
- #清空日志
- echo > $tmppath/vpnlog.txt
- #建立VPN连接
- openvpn --client --dev tap --persist-key --persist-tun --nobind --pull --comp-lzo --verb 3 --mute 10 --ns-cert-type server --replay-persist cur-replay-protection.cache --ca $tmppath/ca.crt --cert $tmppath/lonlife.crt --key $tmppath/lonlife.key --auth-user-pass $tmppath/auth.txt --auth-nocache --proto udp --route-method exe --route-delay 5 --mssfix 1450 --$line --log $tmppath/vpnlog.txt --daemon
- [ $? -ne 0 ] && echo 'run cmd error' && exit
- num=0
- while true;do
- if grep -q 'Initialization Sequence Completed' $tmppath/vpnlog.txt;then
- break
- elif grep -q 'AUTH_FAILED' $tmppath/vpnlog.txt;then
- num=-1
- break
- elif [ $num -lt 30 ];then
- let num++
- sleep 1
- else
- num=-2
- break
- fi
- done
- if [ $num == -1 ];then
- echo -e "$statstime,openvpn,${line#* },AUTH_FAILED"
- elif [ $num == -2 ];then
- echo -e "$statstime,openvpn,${line#* },Connect time out"
- else
- gw=$(route -n |awk '{print $1}'|grep '10.10.*'|sed 's/0$/1/')
- route add -host 220.181.11.98 gw $gw
- address=$(curl -s http://220.181.11.98/cityjson?ie=utf-8|awk '{print $9}'|sed 's/};$//')
- echo -e "$statstime,openvpn,${line#* },ok" >> $tmppath/sockslog.csv
- fi
- kill `pidof openvpn` &> /dev/null
- sleep 5
- }
- function mymail(){
- mailuser="mickey_zzc@163.com"
- txt=$(cat $tmppath/sockslog.csv |awk -F ',' '/down/ {printf "%s at %s is down.<br> \\n",$2,$1}')
- echo $txt |mutt -s "About the state of the socket from $statstime " -e 'set content_type="text/html"' $mailuser -a $tmppath/sockslog.csv
- }
- xmlload
- socket_test
- #该片段来自于http://www.codesnippet.cn/detail/290820135454.html
来源: http://www.codesnippet.cn/detail/290820135454.html