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. 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"