Convert ovpn file to inline format (all in one ovpn client config file)

By devin, 22 October, 2015

Whenever I use openvpn I'm stuck carting around 5 files, sending them over Bluetooth and cluttering up my phone's downloads directory or confusing people I'm setting up with openvpn

I decided finally to figure out inline openvpn client config files (note: all-in-one files don't work on the Securepoint SSL windows client). I made this sed script/command that will automate the process. Imagine that the following five correctly configured openvpn files are in my current directory, with the openvpn file referencing the others:

  • devin.ovpn
  • ca.crt
  • devin.crt
  • devin.key
  • ta.key

Run export VAR=devin and then run this code:

sed -i "${VAR}.ovpn" -e '
	/ca ca.crt/ {
	a\<ca>
	r ca.crt
	a\</ca>
	d
}' -e "
	/cert ${VAR}.crt/ {
	a\<cert>
	r ${VAR}.crt
	a\</cert>
	d
}" -e "
	/key ${VAR}.key/ {
	a\<key>
	r ${VAR}.key
	a\</key>
	d
}" -e '
	/tls-auth ta.key 1/ {
	a\key-direction 1
	a\<tls-auth>
	r ta.key
	a\</tls-auth>
	d
}'

 

And blam, your file will be all in one.

Here's a rundown on the flags and commands I used in this code:

  • -i: edit the named file in place. I omitted the suffix; you can do -i'.sed' filename to create a sort of working copy called filename.sed
  • -e: pass the quoted expression to sed
  • a\: append the line to the output. On OS/X you need to put the a\ on its own line above the line you want to append, but on Linux this format works. You can do multiple lines by using \ but I didn't bother
  • r: append the contents of the named file to the output. Handy!
  • d: delete the "pattern space"

 

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.