Skip to content

Instantly share code, notes, and snippets.

@nikvdp
Last active January 4, 2021 08:13
Show Gist options
  • Save nikvdp/68b7574db21f57186ab3 to your computer and use it in GitHub Desktop.
Save nikvdp/68b7574db21f57186ab3 to your computer and use it in GitHub Desktop.
Merges separated ovpn scripts into one
#!/bin/bash
#######################################################################
# Taken from: https://www.dropbox.com/s/v228zvccef9d10c/merge.sh
# Latest versions of Openvpn supports inline certs and keys
# so you have one client script, instead of script plus 4 keys and certs
#
# This tool assumes
# 1) Openvpn script and certs plus keys are in same directory
# 2) Certs are usually specified in Openvpn script like
# ca ca.crt
# or
# ca /etc/local/openvpn/ca.crt
########################################################################
# Name of certs and keys and client ovpn script
#
ca="ca.crt"
cert="client1.crt"
key="client1.key"
tlsauth="ta.key"
ovpndest="raspberrypi.ovpn"
########################################################################
# Backup to new subdirectory, just incase
#
mkdir -p backup
cp $ca $cert $key $tlsauth $ovpndest ./backup
########################################################################
# Delete existing call to keys and certs
#
sed -i \
-e '/ca .*'$ca'/d' \
-e '/cert .*'$cert'/d' \
-e '/key .*'$key'/d' \
-e '/tls-auth .*'$tlsauth'/d' $ovpndest
########################################################################
# Add keys and certs inline
#
echo "key-direction 1" >> $ovpndest
echo "<ca>" >> $ovpndest
awk /BEGIN/,/END/ < ./$ca >> $ovpndest
echo "</ca>" >> $ovpndest
echo "<cert>" >> $ovpndest
awk /BEGIN/,/END/ < ./$cert >> $ovpndest
echo "</cert>" >> $ovpndest
echo "<key>" >> $ovpndest
awk /BEGIN/,/END/ < ./$key >> $ovpndest
echo "</key>" >> $ovpndest
echo "<tls-auth>" >> $ovpndest
awk /BEGIN/,/END/ < ./$tlsauth >> $ovpndest
echo "</tls-auth>" >> $ovpndest
########################################################################
# Delete key and cert files, backup already made hopefully
#
rm $ca $cert $key $tlsauth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment