Mac OSX – openvpn setup from cli

1.  Install openvpn from terminal

$ brew install openvpn

2.   Download the config files for the connection and copy it to/usr/local/etc/openvpn/

eg: it will have the .ovpn file , crt, key files

2.1 eg: of connection.ovpn config file

client
dev tun
proto tcp
remote vpn.server.com 443
route remote_host 255.255.255.255 net_gateway
resolv-retry infinite
nobind
persist-key
persist-tun
ca vpn.server.com.ca.crt
cert vpn.server.com.user.crt
key vpn.server.com.user.key
auth-user-pass auth.txt
cipher AES-128-CBC
auth SHA1
comp-lzo
route-delay 4
verb 3
reneg-sec 0

2.2 for autologin using username and password, create auth.txt file in same folder /usr/local/etc/openvpn/, with username and password in  two lines.

cat auth.txt

username
password

3.  Script to start the vpn connection, if the openvpn connection is not running [ run the script as root user, or with sudo ]

 

#!/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
RESULT=`ps aux |grep openvpn |grep -v ‘grep’ |awk ‘{print $2}’`
# Uncomment following line to stop and start openvpn
#kill -9 ${RESULT}

if [ “${RESULT}” == ” ]; then
cd /usr/local/etc/openvpn/
/usr/local/Cellar/openvpn/2.4.4/sbin/openvpn connection.ovpn &
echo “NOT Running at `date`” >> /var/log/vpnclient.log
mail -s “VPN not running at `date`” -F email@emaildomain.com <<< ” vpn not working message at `date`”
else
echo “Running at `date`” >> /var/log/vpnclient.log
fi

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *