0

I am new to Bash and I am trying to have this script notify me when I am connected and disconnected to my VPN.

The problem I have is when I run my "openvpn" it will stop listening to the rest of the lines that follow so I had to put my "connected" notification line before I even log in. Is there a more ideal way that I can write this so that my "connected" line will only run when the open vpn line has connected?

If it helps this is for Ubuntu.

#!/bin/bash

set -e

function discon {
  notify-send -i /usr/share/icons/Adwaita/32x32/devices/network-vpn.png "Home Network" "Disconnected"
}

notify-send -i /usr/share/icons/Adwaita/32x32/devices/network-vpn.png "Home Network" "Connected"

openvpn --config /home/matthew/Documents/vpn/MatthewLaptop.ovpn

trap discon EXIT
ghoti
  • 45,319
  • 8
  • 65
  • 104
L0val
  • 47
  • 6

2 Answers2

0

You can append & to detach the process from the terminal. Otherwise bash will only continue the script when openvpn exits.

openvpn --config /home/matthew/Documents/vpn/MatthewLaptop.ovpn &

Johannes Riecken
  • 2,301
  • 16
  • 17
  • this did the trick. i had to move things around a little bit bit if i made it "&&" instead of "&" then it waited for the openvpn script to come back with the fact that it is done and then it would display that it is connected. thanks heaps for your help! – L0val Jul 20 '18 at 04:12
0

You probably want to have OpenVPN handle this notification itself.

From the OpenVPN man page:

   --up cmd
          Run command  after successful TUN/TAP device open (pre --
          UID change).

           consists of  a  path  to  script  (or  executable  program),
          optionally  followed by arguments. The path and arguments may be
          single- or double-quoted and/or escaped using a  backslash,  and
          should be separated by one or more spaces.

In a configuration file, this is just up /path/to/script. For example:

user loval
group loval
script-security 2
up /home/loval/bin/vpn_is_up.sh

The script-security bit is important, because (also from the man page):

          0 -- Strictly no calling of external programs.
          1  -- (Default) Only call built-in executables such as ifconfig,
          ip, route, or netsh.
          2 -- Allow calling  of  built-in  executables  and  user-defined
          scripts.
          3  --  Allow passwords to be passed to scripts via environmental
          variables (potentially unsafe).

Also read about --up-restart and --down options.

ghoti
  • 45,319
  • 8
  • 65
  • 104