我得到了这个脚本:
#!/usr/bin/env bash
if [ ! "$UID" = 0 ]; then
if [ `type -P gksu` ]; then
SUDOAPP="gksu"
elif [ `type -P kdesu` ]; then
SUDOAPP="kdesu"
else
SUDOAPP="sudo"
fi
fi
if [ -n "$1" ]; then
if [ "$1" = "start" ]; then
$SUDOAPP systemctl start openvpn@******
elif [ "$1" = "stop" ]; then
$SUDOAPP systemctl stop openvpn@******
elif [ "$1" = "restart" ]; then
$SUDOAPP systemctl restart openvpn@******
else
echo "Invalid command"
exit 1
fi
else
echo "Run 'start', 'stop' or 'restart' as an argument to start, stop or restart the ******"
exit 1
fi
它工作正常。但是,我还需要建立 ssh 隧道。- 在openvpn连接到我的VPN之前。我有一个脚本可以做到这一点:
#!/bin/bash
# --------------------------------------------------------
# ******* | https://******.org | ****************************************
# SSH Client Configuration, Linux/OSX
# ******_*************
# --------------------------------------------------------
chmod 600 /etc/openvpn/sshtunnel.key
while :
do
echo ""; echo "****** SSH Tunnel"
ssh -i /etc/openvpn/sshtunnel.key -L ****:127.0.0.1:**** sshtunnel@**.**.**.* -p ** -N -T -v
read -t 5 -p "Retry? (or wait 5 sec for Y)" yn
if [[ $yn == "n" || $yn == "N" ]]; then break; fi
done
如何将其添加到第一个脚本中,以使 openvpn 部分等到 ssh 客户端启动?
第一个脚本可以循环检查隧道,直到成功。 您可以使用 nc (netcat) 来执行此操作,并在 shell 变量中捕获输出:
while [[ -z "$nc_output" ]]; do
read -r nc_output < <(nc -v -d -u localhost openvpn 2>&1)
sleep 2
done
这将每 2 秒检查一次 UDP 端口"openvpn"(替换您实际隧道的内容)是否可以连接到,如果成功,则依靠 -v
选项输出文本。