Skip to content

Instantly share code, notes, and snippets.

@Deltachaos
Created March 18, 2012 08:26
Show Gist options
  • Save Deltachaos/2069977 to your computer and use it in GitHub Desktop.
Save Deltachaos/2069977 to your computer and use it in GitHub Desktop.
Script to Setup OpenVPN
#!/bin/bash
if which lsb_release; then
DISTRIB_ID=$(lsb_release -i | sed -n 's/Distributor ID\:\t//p')
else
echo "You nead to install /etc/lsb-release to run this"
fi;
SERVER="vpn.xtain.net"
DNS="10.8.0.1"
CERTNAME="${1}"
if [ -z "${1}" ]; then
CERTNAME=$(hostname)
fi;
installpkg() {
case "${DISTRIB_ID}" in
archlinux)
pacman -S "${1}";;
Debian)
apt-get update
apt-get install "${1}"
;;
Ubuntu)
apt-get update
apt-get install "${1}"
;;
*)
echo "Unknown Distribution";
exit 1;;
esac
}
service_restart() {
case "${DISTRIB_ID}" in
archlinux)
/etc/rc.d/"${1}" restart
;;
Debian)
service "${1}" restart
;;
Ubuntu)
service "${1}" restart
;;
*)
echo "Unknown Distribution";
exit 1;;
esac
}
sshcmd() {
ssh root@${SERVER} "${1}"
}
sshcp() {
scp root@${SERVER}:"${1}" "${2}"
}
installpkg "openvpn"
sshcmd 'if [ ! -e "/etc/openvpn/easy-rsa/2.0/keys/'${CERTNAME}'.key" ]; then cd /etc/openvpn/easy-rsa/2.0; source ./vars; ./build-key "'${CERTNAME}'"; fi;'
sshcp '/etc/openvpn/easy-rsa/2.0/keys/'${CERTNAME}'.key' '/etc/openvpn/.'
sshcp '/etc/openvpn/easy-rsa/2.0/keys/'${CERTNAME}'.crt' '/etc/openvpn/.'
sshcp '/etc/openvpn/easy-rsa/2.0/keys/ca.crt' '/etc/openvpn/.'
id openvpn > /dev/null 1>&2
if [ "${?}" -gt "0" ]; then
adduser --system --home /etc/openvpn --no-create-home --disabled-password --disabled-login openvpn
fi;
GROUPNAME=$(id -g -n openvpn)
sshcmd '/etc/openvpn/gen-xtain-conf.sh "'${CERTNAME}'" openvpn "'${GROUPNAME}'"' > '/etc/openvpn/xtain.conf'
chmod 500 /etc/openvpn/${CERTNAME}.{crt,key}
chmod 500 /etc/openvpn/ca.crt
service_restart "openvpn"
RESOLVCONTENT=$(cat /etc/resolv.conf)
cat > /etc/resolv.conf <<EOF
nameserver ${DNS}
${RESOLVCONTENT}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment