Skip to content

Instantly share code, notes, and snippets.

@MarcelFox
Last active October 19, 2020 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcelFox/2854f6b3884f0efb45a1d27889faedbd to your computer and use it in GitHub Desktop.
Save MarcelFox/2854f6b3884f0efb45a1d27889faedbd to your computer and use it in GitHub Desktop.
check_vpn.sh
#!/bin/bash
#
# Name: check_vpn
# Version: 1.0
# By MarcelFox
# COLORS & STYLES:
BOLD='\e[1m'
ITALIC='\e[3m'
NO_STL='\e[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COL='\033[0m'
# Configuration file and dir location:
vpn_file="FILE-NAME-EXAMPLE-HERE"
vpn_dir="/home/$USER/.openvpn"
if [ ! -f ${vpn_dir}/${vpn_file} ];
then
printf "\n${BOLD}vpn_file ${RED}not found${NO_COL}${BOLD} on ${vpn_dir}${NO_STL}\n\n";
exit;
fi;
vpn_pid=$(ps aux | awk '/openvpn --daemon/ && !/awk/ {print $2}');
if [ ! -z "$vpn_pid" ];
then
printf "\n${BOLD}VPN running under pid ${GREEN}${BOLD}${vpn_pid}${NO_COL}, ${RED}${BOLD}kill${NO_COL} ${BOLD}proccess? [y/n]: ${NO_STL}";
read var_decision;
if [ -z $var_decision ];
then
printf "\n${BOLD}Please inform only 'y' or 'n'. Closing...${NO_STL}\n\n";
exit;
fi;
if [ $var_decision == "y" ] || [ $var_decision == "Y" ];
then
sudo kill ${vpn_pid};
else
printf "${BOLD}Nothing done. VPN still running.${NO_STL}\n\n";
fi;
else
printf "\n${BOLD}VPN not running, start it? [y/n]: ${NO_STL}";
read var_decision;
if [ -z $var_decision ];
then
printf "\n${BOLD}Please inform only 'y' or 'n'. Closing...${NO_STL}\n\n";
exit;
fi;
if [ $var_decision == "y" ] || [ $var_decision == "Y" ];
then
sudo openvpn --daemon --config ${vpn_dir}/${vpn_file};
vpn_pid=$(ps aux | awk '/openvpn --daemon/ && !/awk/ {print $2}');
printf "${BOLD}VPN running under pid ${GREEN}${BOLD}${vpn_pid}${NO_STL}${NO_COL}\n\n";
else
printf "${BOLD}Ok, VPN not started.${NO_STL}\n\n";
fi;
fi;
@MarcelFox
Copy link
Author

MarcelFox commented Oct 19, 2020

Usage:

You must inform the location of the p12 & key file under your .ovpn file, including your username (since $USER will not be interpreted by openvpn):

pkcs12 /home/MYUSER/.openvpn/MY-FILE.p12
tls-crypt /home/MYUSER/.openvpn/MY-KEY.key


Add the following line on ~/.bashrc

alias check_vpn='bash /home/$USER/check_vpn.sh'

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