Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ThomasG77/5c09eead1244f9d1d2bd7fa70b26fead to your computer and use it in GitHub Desktop.
Save ThomasG77/5c09eead1244f9d1d2bd7fa70b26fead to your computer and use it in GitHub Desktop.
Convert single OpenVPN file to multiple to be compatible for import in Gnome VPN manager (improved from https://askubuntu.com/questions/134918/setting-vpn-client)
#!/usr/bin/env bash
# Improved and automated version of https://askubuntu.com/questions/134918/setting-vpn-client
# First arg = directory where you want to collect files
# Second arg = filename input
# To illustrate
# ./generate-cleaned-openvpn.sh my_test_directory labtop.ovpn
# After run, import with
# sudo nmcli connection import type openvpn file my_test_directory/client.ovpn
# Script suppose that noclobber syntax is on e.g
# set -o | grep noclobber" return a line "noclobber on"
mkdir $1
filename=$(basename $2)
filename_no_extension="${filename%.*}"
grep -v "key-direction 1" $2 >| $1"/client.ovpn"
# Extract block
# ca
tr "\n" "|" < $1"/client.ovpn" | grep -o '<ca>.*</ca>' | sed 's/\(<ca>\|<\/ca>\)//g;s/|/\n/g' >| $1"/ca.crt"
# cert
tr "\n" "|" < $1"/client.ovpn" | grep -o '<cert>.*</cert>' | sed 's/\(<cert>\|<\/cert>\)//g;s/|/\n/g' >| $1"/client.crt"
# key
tr "\n" "|" < $1"/client.ovpn" | grep -o '<key>.*</key>' | sed 's/\(<key>\|<\/key>\)//g;s/|/\n/g' >| $1"/client.key"
# tls-auth
tr "\n" "|" < $1"/client.ovpn" | grep -o '<tls-auth>.*</tls-auth>' | sed 's/\(<tls-auth>\|<\/tls-auth>\)//g;s/|/\n/g' >| $1"/ta.key"
# connection
remote=$(tr "\n" "|" < $1"/client.ovpn" | grep -o '<connection>.*</connection>' | sed 's/\(<connection>\|<\/connection>\)//g;s/|/\n/g')
sed -i '/<ca>/,/<\/ca>/d' $1"/client.ovpn"
sed -i '/<cert>/,/<\/cert>/d' $1"/client.ovpn"
sed -i '/<key>/,/<\/key>/d' $1"/client.ovpn"
sed -i '/<tls-auth>/,/<\/tls-auth>/d' $1"/client.ovpn"
sed -i '/<dh>/,/<\/dh>/d' $1"/client.ovpn"
sed -i '/<connection>/,/<\/connection>/d' $1"/client.ovpn"
echo $remote >> $1"/client.ovpn"
sed -i 's/ remote/\nremote/g' $1"/client.ovpn"
echo '
ca ca.crt
cert client.crt
key client.key
tls-auth ta.key 1
dh dh.pem
' >> $1"/client.ovpn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment