openvpn 启动脚本
#!/bin/bash
#File: openvpn_turn
#Auth: Robin
#Date: 2013/01/17 10-30-10
#Desc: Start/stop openvpn client
#Vers: 1.0
#
# openvpn -s --> stop openvpn client
# openvpn -r --> start openvpn client
#配置文件所在目录
CONF_DIR="/etc/openvpn"
#配置文件
CONF_FILE="client.conf"
#存放用户名和密码,格式如下
# user_name
# password
AUTH_FILE="auth"
LOG_FILE="/tmp/openvpn.log"
PID_FILE="/tmp/openvpn.pid"
function do_success {
ps -ef|grep openvpn|grep -v grep|awk '{print $2}' >> $PID_FILE
rm $LOG_FILE
echo -e "Start OPENVPN \033[32msuccessfully\033[0m!"
exit 0
}
function do_fail {
echo -e "Start OPENVPN \033[41mfailure\033[0m!"
echo "Maybe you can get something useful from $LOG_FILE"
exit 1
}
function if_run {
if ;then
return 0
else
return 1
fi
}
function start_openvpn {
if_run
if ;then
ps -ef|grep '\<openvpn\>' |grep -v grep &> /dev/null
if ;then
echo -e "OPENVPN is \033[32malready running\033[0m!!!"
exit 1
else
rm $PID_FILE
fi
fi
if ;then
echo > $LOG_FILE
fi
cd $CONF_DIR
sudo openvpn --config $CONF_FILE --auth-user-pass $AUTH_FILE &> $LOG_FILE &
echo -e "\033[33mStarting\033[0m OPENVPN..."
sleep 5
cat $LOG_FILE |grep "Sequence Completed" 2> /dev/null && do_success || do_fail
}
function stop_openvpn {
if_run
if ;then
echo "OPENVPN is not running!"
exit 1
fi
for i in `ps -ef|grep '\<openvpn\>'|grep -v grep|awk '{print $2}'`;do
sudo kill -9 $i
done
rm $PID_FILE
echo -e "OPENVPN is already \033[41mstop\033[0m."
}
while getopts sr SW;do
case $SW in
s)
stop_openvpn
break
;;
r)
start_openvpn
break
;;
?)
echo "openvpn_turn "
esac
done