jolausa

vpn_script

Sep 10th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. RED='\033[1;31m'
  4. BLUE='\033[1;34m'
  5. GREEN='\033[1;32m'
  6. NC='\033[0m' # No Color
  7.  
  8. function start() {
  9. p=`ps aux|grep openvpn|grep SUSE|grep -v grep|awk '{print $2}'`
  10. if [[ "$p" != "" ]]; then
  11. printf "${RED}OpenVPN is already running with process $p. Will not start again...${NC}\n\n"
  12. #ps aux|grep [o]penvpn
  13. else
  14. user=`cat /etc/openvpn/user.txt`
  15. printf "${GREEN}Starting OpenVPN for user ${user}. Please enter password...${NC}\n\n"
  16. sudo systemctl start [email protected]
  17. sleep 4
  18. for i in `seq 1 10`; do
  19. p=`ps aux|grep openvpn|grep SUSE|grep -v grep|awk '{print $2}'`
  20. if [[ "$p" == "" ]]; then
  21. printf "${RED}There is some problem starting the openvpn process${NC}\n\n"
  22. exit 1
  23. fi
  24. sleep 1
  25. done
  26. printf "${GREEN}OpenVPN running with pid=${p}${NC}\n\n"
  27.  
  28. fi
  29. }
  30.  
  31. function stop() {
  32. p=`ps aux|grep openvpn|grep SUSE|grep -v grep|awk '{print $2}'`
  33. if [[ "$p" != "" ]]; then
  34. ps aux|grep [o]penvpn
  35. printf "${GREEN}Killing process $p ${NC}\n\n"
  36. sudo kill -9 $p
  37. printf "${GREEN}Process OpenVPN with PID $p killed${NC}\n\n"
  38. else
  39. printf "${RED}Can't kill process. OpenVPN is not running${NC}\n\n"
  40. fi
  41. }
  42.  
  43. function check() {
  44. p=`ps aux|grep openvpn|grep SUSE|grep -v grep|awk '{print $2}'`
  45. if [[ "$p" != "" ]]; then
  46. printf "${GREEN}OpenVPN process running with PID $p ${NC}\n\n"
  47. #ps aux|grep [o]penvpn
  48. else
  49. printf "${RED}No OpenVPN process runnign ${NC}\n\n"
  50. fi
  51. }
  52.  
  53.  
  54.  
  55. if [[ "$1" == "stop" ]] || [[ "$1" == "s" ]] || [[ "$1" == "--stop" ]] || [[ "$1" == "-s" ]]; then
  56. stop
  57. elif [[ "$1" == "check" ]] || [[ "$1" == "c" ]] || [[ "$1" == "--check" ]] || [[ "$1" == "-c" ]]; then
  58. check
  59. elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "h" ]] || [[ "$1" == "help" ]]; then
  60. printf "${BLUE}usage${NC}: myvpn [stop|s|check|c] or [--stop|-s|--check|-c], with no args to start\n\n"
  61. else
  62. start
  63. fi
Advertisement
Add Comment
Please, Sign In to add comment