3

I could use a little help on this one. I am a novice scripter at best. I am trying to write a bash script to connect to my multiple openvpn sites. I am trying to write the script to open in a detached screen. I have managed to write the script to connect to the different .ovpn via different variables. Getting them to run in the detached screen is what I am having trouble with. Hoping one of you guys might be able to help me. Currently I am just running

screen -S vpn

then once the screen opens up, I execute my script to connect to the openvpn sites. Here is my current vpn connection script:

#!/bin/bash

if [ "$1" = "seed-rl" ] ;
then
cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config seed-rl.ovpn
fi

if [ "$1" = "atl10" ] ;
then
cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config Atlanta-10.ovpn
fi

if [ "$1" = "atl11" ] ;
then
cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config Atlanta-11.ovpn
fi

if [ "$1" = "atl12" ] ;
then
cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config Atlanta-12.ovpn
fi

if [ "$1" = "nyc02" ] ;
then
cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config NewYork-02.ovpn
fi

if [ "$1" = "nyc10" ] ;
then
cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config NewYork-10.ovpn
fi

if [ "$1" = "nyc11" ] ;
then
cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config NewYork-11.ovpn
fi

What can I do to make this script run in a detached screen?

muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    does this helps? http://askubuntu.com/q/62562/294611 – Alex Jones Dec 27 '15 at 17:32
  • Not really. I'm still having to run a screen command before I run the script. What I'm trying to do is run screen from within the script. So that all I need to do is run the script, and the selected .ovpn is loaded into a detached screen. – RobbieL811 Dec 27 '15 at 17:47

1 Answers1

1

You can check if the script is being run inside screen, and if not, re-execute it in screen:

#! /bin/bash

[[ -z $STY ]] && screen -S vpn -d -m "$0" "$@"

if [ "$1" = "seed-rl" ] ;
then
    cd "/home/robbiel811/vpn configs"
    echo password | sudo -S openvpn --config seed-rl.ovpn
fi

...

STY is a variable set by screen, which we can use to detect if we're running in it. $0 is the current command being executed, and $@ all the arguments.

Also, consider simplifying your script using associative arrays:

#! /bin/bash

[[ -z $STY ]] && screen -S vpn -d -m "$0" "$@"


declare -A configs
config['seed-rl']='seed-rl.ovpn'
config['atl10']='Atlanta-10.ovpn'
# ... etc.
config['nyc11']='NewYork-11.ovpn'

cd "/home/robbiel811/vpn configs"
echo password | sudo -S openvpn --config "${config[$1]}.ovpn"

And using NOPASSWD sudoers rules instead of storing your password in clear text.

muru
  • 197,895
  • 55
  • 485
  • 740
  • thank you for your advice. Your script didn't work EXACTLY right like I wanted to. I modified it a little to what I have below and it's working like I want it!!! Also, thanks for rearranging my script! Much neater now!

    I can't figure out how to enter a stupid code block here, but anyways, here's a link to the pastebin of the current script that I am using.

    http://pastebin.com/ufLAaYVn

    Again, Thanks for you help! Couldn't have done it without you!

    – RobbieL811 Dec 29 '15 at 00:14