自动建立ssh-tunnel,等待ssh-tunnel建立后,再建立正常的VPN连接

Automatically establish ssh-tunnel, wait until ssh-tunnel is established, then establish normal VPN connection

我得到了这个脚本:

#!/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 "" ]; then
    if [ "" = "start" ]; then
        $SUDOAPP systemctl start openvpn@******
    elif [ "" = "stop" ]; then
        $SUDOAPP systemctl stop openvpn@******
    elif [ "" = "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 选项输出文本。