Automatic OpenVPN User Creation with Email Alert
Normally, what we do is:
- use
easy-rsa
commandline tool to generate the.crt
,.key
files and the.ovpn
file, - copy the required files to the desktop using WinSCP or FileZilla or any other software.
The above seems very tedious task to perform manually everytime a user ID has to be created. In this blog post, I’ll share a Shell Script to automate this process, as well as send an email with a .zip
file attached, containing all the relevant files.
I am assuming the following is already in place:
- OpenVPN Server on Linux (Version
OpenVPN 2.3.6 x86_64-redhat-linux-gnu
) - Easy RSA 3
- OS:
Amazon Linux AMI 2016.03
The script uses the following command line tools to work:
- zip (to zip all the necessary files)
- mutt (to send email with attachment)
Usage and Working
Usage:
./createvpnuser.sh "OpenVPN User" [To Email ID (without spaces)]
Working:
The script works in the following way:
- The script takes 2 arguments for userID and email address, respectively.
- Password of the user is generated using
/dev/urandom
, which is a pseudo random number generator. ./easyrsa build-client-full <USERID> nopass
command is used to generate the required files.- The files are zipped to a predefined location.
- An email is sent to address given in Step 1.
Full Script
1 |
|
List of Files included in zip
<USERID>.key
<USERID>.crt
credentials.txt
ca.crt
openvpn_<USERID>.ovpn
Notes
- The above works for me. Cannot guarantee for you.
- Go through the script carefully before executing.
- You can download the
easyrsa
file from Github (link below). - Happy to help in any issues.