Skip to content

Instantly share code, notes, and snippets.

@liyu1981
Last active January 3, 2016 02:28
Show Gist options
  • Select an option

  • Save liyu1981/8395375 to your computer and use it in GitHub Desktop.

Select an option

Save liyu1981/8395375 to your computer and use it in GitHub Desktop.
my openvpn util script
THISDIR=`pwd`
CMD=$1
CONFNAME=$2
function usage {
echo "usage: sudo $0 start/status/stop <conf_name>"
}
function openvpn_start {
#echo "openvpn --daemon --writepid /var/run/openvpn/${CONFNAME}.pid --cd ${THISDIR}/${CONFNAME} --config ${CONFNAME}.ovpn"
openvpn --daemon --writepid /var/run/openvpn/${CONFNAME}.pid --cd ${THISDIR}/${CONFNAME} --config ${CONFNAME}.ovpn
}
function openvpn_test {
openvpn --writepid /var/run/openvpn/${CONFNAME}.pid --cd ${THISDIR}/${CONFNAME} --config ${CONFNAME}.ovpn
}
function openvpn_status {
if [ -f /var/run/openvpn/${CONFNAME}.pid ]; then
if ps -p `cat /var/run/openvpn/${CONFNAME}.pid` >/dev/null
then
echo "Process(" `cat /var/run/openvpn/${CONFNAME}.pid` ") is running..."
else
echo "/var/run/openvpn/${CONFNAME}.pid exist but process is dead."
fi
else
echo "Can not find pid file."
fi
}
function openvpn_stop {
kill `cat /var/run/openvpn/${CONFNAME}.pid`
rm /var/run/openvpn/${CONFNAME}.pid
}
if [ $# -lt 2 ]; then
usage
exit 1
fi
if [ $EUID -ne 0 ]; then
echo "This script need to be run as root."
usage
exit 1
fi
case $CMD in
start)
openvpn_start
;;
test)
openvpn_test
;;
status)
openvpn_status
;;
stop)
openvpn_stop
;;
*)
usage
exit 1
;;
esac
@liyu1981
Copy link
Copy Markdown
Author

Place this script in some folder with openvpn conf dirs, as follows

[liyu@localhost openvpn]$ tree .
.
├── ccbp
│   ├── ccbp.crt
│   ├── ccbp.ovpn
│   ├── clustertech.crt
│   └── clustertech.key
├── clustertech
│   ├── clustertech.crt
│   └── clustertech.ovpn
└── myopenvpn.sh

2 directories, 7 files

and then

sudo ./myopenvpn.sh start ccbp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment