Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active July 19, 2021 09:18
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RichardBronosky/331f975bba6697e5a15217233d280c06 to your computer and use it in GitHub Desktop.
Save RichardBronosky/331f975bba6697e5a15217233d280c06 to your computer and use it in GitHub Desktop.
Create a single-file "unified format" ovpn file from the legacy client.ovpn client.key client.crt ca.crt four-file format.

unify-ovpn.sh

  1. cd to the directory where your 4 files are. (client.ovpn, client.key, client.crt, and ca.crt)

  2. Call unify-ovpn.sh with the filename of your ovpn file

     unify-ovpn.sh client.ovpn
    
  3. A new file named client_unified.ovpn will be created

#!/bin/bash
src=$1
dst="$(basename $src .ovpn)_unified.ovpn"
gawk -f - $src > $dst << 'AWK'
BEGIN {
RS="\n|\r\n"
}
function readcert(file) {
while ((getline < file) > 0) {
contents = contents RT $0
if ($1 == "-----BEGIN")
contents = $0
if ($1 == "-----END")
break
}
close(file)
return contents
}
$1 ~ /^(ca|key|cert)$/ {
tag = $1
print "#" $0
print "<" tag ">"
print readcert($2)
print "</" tag ">"
next
}
{
print
}
AWK
@mfrade
Copy link

mfrade commented Nov 23, 2017

Thank you for your script. It would be nice to add support for the authentication key (diff):

25c25
< $1 ~ /^(ca|key|cert)$/ {
---
> $1 ~ /^(ca|key|cert|tls-auth)$/ {
27a28,32
>     
>     if (tag == "tls-auth"){
>         print "# for servers: key-direction 0"
>         print "key-direction 1"
>     }

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