Last active
October 19, 2020 17:48
-
-
Save MarcelFox/2854f6b3884f0efb45a1d27889faedbd to your computer and use it in GitHub Desktop.
check_vpn.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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