Created
July 12, 2016 15:51
-
-
Save twang2218/08bee95be386e241be2620a91e653c0b to your computer and use it in GitHub Desktop.
Script to create an OpenVPN service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Before run this script, a docker host in a public cloud should be available | |
# The host can be created by the following commands: | |
# | |
# docker-machine create -d digitalocean dev | |
# eval $(docker-machine env dev) | |
# | |
# Then you can run this command simply by : | |
# | |
# ./prepare-openvpn-service.sh all <server_address> <username> | |
# | |
function create { | |
if [ -z "$1" ]; then | |
echo "Usage: $0 create <server_address>" | |
exit 1 | |
fi | |
VPN_SERVER=$1 | |
docker volume create --name openvpn | |
docker run -v openvpn:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://${VPN_SERVER} | |
docker run -v openvpn:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki nopass | |
} | |
function destroy { | |
docker rm --volumes --force openvpn | |
} | |
function run { | |
docker run -v openvpn:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN --name openvpn kylemanna/openvpn | |
} | |
function generate_config { | |
if [ -z "$1" ]; then | |
echo "Usage: $0 generate <username>" | |
exit 1 | |
fi | |
USER_NAME=$1 | |
docker run -v openvpn:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full ${USER_NAME} nopass | |
} | |
function export_config { | |
if [ -z "$1" ]; then | |
echo "Usage: $0 export <username>" | |
exit 1 | |
fi | |
USER_NAME=$1 | |
docker run -v openvpn:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient ${USER_NAME} > ${USER_NAME}.ovpn | |
} | |
function usage { | |
echo "Usage: $0 {create|generate|export|run|destroy}" | |
} | |
function run_all { | |
if [ ! "$#" -eq "2" ]; then | |
echo "Usage: $0 all <server_address> <username>" | |
exit 1 | |
fi | |
VPN_SERVER=$1 | |
USER_NAME=$2 | |
create ${VPN_SERVER} | |
run | |
generate_config ${USER_NAME} | |
export_config ${USER_NAME} | |
} | |
function main { | |
Command=$1 | |
shift | |
case "${Command}" in | |
create) create $@ ;; | |
destroy) destroy ;; | |
run) run ;; | |
generate) generate_config $@ && export_config $@ ;; | |
export) export_config $@ ;; | |
all) run_all $@ ;; | |
*) usage ;; | |
esac | |
} | |
main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment