0

我是 Bash 新手,我试图让这个脚本在我连接和断开我的 VPN 时通知我。

我遇到的问题是,当我运行“openvpn”时,它会停止监听后面的其余行,所以我必须在登录之前放置“已连接”通知行。有没有更理想的方法可以写这个以便我的“已连接”线路仅在打开的 vpn 线路已连接时运行?

如果它有帮助,这适用于 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
4

2 回答 2

0

你可能想让 OpenVPN 自己处理这个通知。

OpenVPN 手册页

   --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.

在配置文件中,这只是up /path/to/script. 例如:

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

script-security一点很重要,因为(也来自手册页):

          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).

另请阅读关于--up-restart--down选项。

于 2018-07-20T02:56:38.753 回答
0

您可以附加&以从终端分离进程。否则 bash 只会在openvpn退出时继续执行脚本。

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

于 2018-07-20T02:48:59.300 回答