0

I want to use a bash script to start an openvpn connection and once I got the tunnel established start an SSH terminal connection to a server and once I close the SSH connection also close the openvpn connection.

For the start, I tried implementing the solution proposed at https://stackoverflow.com/questions/21001220/bash-sequence-wait-for-output-then-start-next-program as a shell script

#!/bin/bash
exec 3< <(sudo openvpn --config  /etc/openvpn/server.ovpn)
sed '/Initialization Sequence Completed$/q' <&3 ; cat <&3 &
ssh server

I run this script as sh ssh-vpn.sh in the fish shell.

As a script, this gives me a syntax error line 2: syntax error near unexpected token '<'. outside of the script, I do not get this error.

Also, not using these command as a script, I occasionally seemed to have gotten error messages because the file descriptor 3 was already existing.

  1. How to avoid the syntax error in the script?

  2. How to extend the script so that it automatically closes the openvpn connection afterwards?

  3. How to ensure the file descriptor gets closed at end of script?

I apologise if this is too much for one question - I guess this is one of my first scripting questions for a long time and I still have to learn much here.

  • thanks @roaima, I run this script as sh ssh-vpn.sh in the fish shell. Is this the information you requested? any more helpful question are very appreciated. – ingli Apr 8 at 20:40
  • having implemented @roaima's take, I do not get file descriptor and syntax error issues anymore. – ingli Apr 19 at 10:49
1

The problem is that the code is written for the bash shell but you are explicitly using sh to run it. On a growing number of platforms these are not the same.

Instead of sh ssh-vpn.sh you have two better options

  1. Make the script executable and let the #! line define the shell to be used

    # just once
    chmod a+rx ssh-vpn.sh
    
    # thereafter
    ./ssh-vpn.sh
    
  2. Run the script with the correct shell

    bash ssh-vpn.sh
    
| improve this answer | |
  • thanks, @roaima very much for code & especially for the explanation. The executable script now runs as expected. However, the second subquestion is still open: how to extend the script such that the openvpn connection is closed after I close the ssh connection? – ingli Apr 9 at 19:53
0

Implementing https://unix.stackexchange.com/users/100397/roaima's answer and knowing that I only have a single openvpn connection open allows me to reuse the pkill -9 -x $process_name command from Best way to kill processes created by bash script? - and that get's me to this code

#!/bin/bash
exec 3< <(sudo openvpn --config  /etc/openvpn/server.ovpn)
sed '/Initialization Sequence Completed$/q' <&3 ; cat <&3 &
ssh server
sudo pkill -9 -x openvpn 

So, in sum

  1. generate script ssh-vpn.sh with the code above (added some printf information on what is happening)

    #!/bin/bash
    printf "Script to initialise openvpn connection to XYZ and open ssh channel into XYZ Server.\n"
    printf "Starting openvpn to YXZ...\n\n"
    exec 3< <(sudo openvpn --config  /etc/openvpn/server.ovpn)
    sed '/Initialization Sequence Completed$/q' <&3 ; cat <&3 &
    printf "VPN now initialised.\nstarting ssh connection to XYZ...\n"
    ssh server
    printf "\nKilling openvpn connection...\n"
    sudo pkill -9 -x openvpn 
    # end of script
    
  2. in shell

    # just once
    chmod a+rx ssh-vpn.sh
    
  3. run command in shell

    # thereafter
    ./ssh-vpn.sh
    
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.