来自 Windows 的自动 SSH 隧道

Dav*_*Yaw 42 windows ssh ssh-tunnel

我正在尝试将 Windows 计算机设置为始终有两个 SSH 隧道连接到我的 Linux 服务器。

目前,我正在使用 PuTTY 打开两个 SSH 隧道:我在 PuTTY 中登录到服务器,将其最小化,并且从不碰它。这很有效,除非 SSH 连接断开:PuTTY 显示错误消息,我需要手动关闭错误并重新连接到服务器。

我想要做的是有一个应用程序可以设置两个 SSH 隧道,并且可以自动重新连接,而无需手动执行任何操作,包括输入密码。我通过两条隧道发送的数据是 VNC 连接,所以我通常不会在机器前清除错误和输入密码。这两条隧道是一条本地隧道,一条远程隧道。

(是的,我知道自动登录到 SSH 的危害。我计划创建一个没有特权且不允许交互登录的专用用户,并使用它。)

我确实发现了这个问题:如何可靠地保持 SSH 隧道打开?,但那是使用 Linux 作为 SSH 客户端,而我使用的是 Windows。

Jea*_*ier 19

试试Bitvise Tunnelier - 它对我有用。我将它设置为建立 SSH 隧道,同时仅作为托盘图标可见。它在启动时建立 SSH 连接,并在切断后或系统进入睡眠状态后连接恢复后立即重新建立连接。我仍然更喜欢 Putty 控制台的外观,所以我一直在使用它 - 但为了保持隧道畅通,我现在使用 Tunnelier。我发现的唯一主要缺点是缺乏 IPv6 支持,Putty 无需用户操作即可提供。

  • 可能不清楚,但您在 *C2S* 选项卡中设置隧道并在 *S2C* 选项卡中设置反向隧道。它分别代表 *client2server* 和 *server2client*。 (2认同)

Ebr*_*owi 13

试试MyEnTunnel。它可以在连接失败时重新连接。

在此处输入图片说明

  • 也可以在这里找到 https://github.com/feralhosting/feralfilehosting/tree/master/Feral%20Wiki/SSH/SSH%20tunnels%20-%20MyEnTunnel (2认同)
  • 可以在这里下载:https://myentunnel.informer.com/ (2认同)

小智 6

我尝试了许多解决方案,例如 SSH 隧道管理器,但对我来说都很不方便:配置屏幕太多,有时还存在错误(有一次 SSH 隧道管理器清除了所有设置!我的设置!所以我必须恢复所有 30 个隧道的设置)。所以他们都失去了我的信任。这就是为什么我想出了自定义 Powershell 脚本,它易于配置、可更改、小但有效。发布在这里和下面:

要开始使用它,您需要这样的配置:

# LocalPort TargetHost  TargetPort  SshHost SshUsername SshKeyPath 
18080   google.com  80  bastion.example.com User    D:\secure\path\to\private_key.ppk
Run Code Online (Sandbox Code Playgroud)

将其另存为 config.csv。并使用 powershell 脚本来保持它是:

<#
.SYNOPSIS
  Powershell script for keeping ssh tunnel up and running

.DESCRIPTION
  This script uses configuration of tunnels located in config.csv. For more information visit http://tsherlock.tech/2019/03/13/simple-ssh-tunnel-auto-reconnect-using-putty-and-powershell/

.NOTES
  Version:        1.0
  Author:         Anton Shkuratov
  Creation Date:  2019-03-13
  Purpose/Change: Initial script development

#>

$currentDir = $PSScriptRoot
if (-not $env:PATH.Contains($currentDir)) {
  $env:PATH="$env:PATH;$currentDir"
}

# Check plink is accessible
try {
  Start-Process plink.exe -WindowStyle Hidden
} catch {
  Write-Host Error running plink.exe Please make sure its path is in PATH environment variable
  EXIT 1
}

# Parse config
$config = [System.IO.File]::ReadAllLines("$currentDir\config.csv");
$bindings = New-Object System.Collections.ArrayList
$regex = New-Object System.Text.RegularExpressions.Regex("(\d)+\s([^ ]+)\s(\d+)\s([^ ]+)\s([^ ]+)\s([^ ]+)", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase);
$keyPasswords = @{}
$procs = @{}

foreach($line in $config) {
  $match = $regex.Match($line)

  if ($match.Success) {
    $sshKey = $match.Groups[6];

    $bindings.Add(@{
      LocalPort = $match.Groups[1];
      TargetHost = $match.Groups[2];
      TargetPort = $match.Groups.Groups[3];
      SshHost = $match.Groups[4];
      SshUser = $match.Groups[5];
      SshKey = $match.Groups[6];
    });

    if (-not $keyPasswords.ContainsKey($sshKey)) {
      $pass = Read-Host "Please enter password for key (if set): $sshKey" -AsSecureString
      $keyPasswords.Add($sshKey, $pass);
    }
  }
}

# Starting Processes
function EnsureRunning($procs, $keyPasswords, $binding) {

  if ($procs.ContainsKey($binding) -and $procs[$binding].HasExited) {

    $proc = $procs[$binding]
    $sshKey = $binding.sshKey
    $out = $proc.StandardError.ReadToEnd()

    if ($out.Contains("Wrong passphrase")) {
      Write-Host "Wrong pass phrase for $sshKey, please re-enter"
      $pass = Read-Host "Please enter password for key: $sshKey" -AsSecureString
      $keyPasswords[$sshKey] = $pass;
    } else {
      $exitCode = $proc.ExitCode
      $tHost = $binding.sshHost

      Write-Host "Connection to $tHost is lost, exit code: $exitCode"
    }
  }

  if (-not $procs.ContainsKey($binding) -or $procs[$binding].HasExited) {
    $sshUser = $binding.SshUser
    $sshHost = $binding.SshHost
    $sshKey = $binding.SshKey
    $lPort = $binding.LocalPort
    $tPort = $binding.TargetPort
    $tHost = $binding.TargetHost
    $sshKeyPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($keyPasswords[$sshKey]))

    $psi = New-Object System.Diagnostics.ProcessStartInfo;
    $psi.FileName = "plink.exe";
    $psi.UseShellExecute = $false;

    $psi.CreateNoWindow = $true;
    $psi.RedirectStandardInput = $true;
    $psi.RedirectStandardError = $true;

    $psi.Arguments = "-ssh $sshUser@$sshHost -i `"$sshKey`" -batch -pw $sshKeyPass -L $lPort`:$tHost`:$tPort"

    $proc = [System.Diagnostics.Process]::Start($psi);

    Start-Sleep 1

    if (-not $proc.HasExited) {
      Write-Host Connected to $sshUser@$sshHost
    }

    $procs[$binding] = $proc;
  }
}

function EnsureAllRunning($procs, $keyPasswords, $bindings) {
  while($true) {
    foreach($binding in $bindings) {
      EnsureRunning $procs $keyPasswords $binding
    }
    Start-Sleep 1
  }
}


try {
  # Waiting for exit command
  Write-Host Working... Press Ctrl+C to stop execution...
  EnsureAllRunning $procs $keyPasswords $bindings
} finally {
  # Clean up
  Write-Host Clean up

  foreach($proc in $procs.Values) {
    if ($proc -ne $null -and -not $proc.HasExited) {
      $proc.Kill();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

配置完成后,只需运行它即可:

powershell -File autossh.ps1
Run Code Online (Sandbox Code Playgroud)


int*_*ika 5

两个很棒的工具:

两者都具有以下特点:

  • 可以在启动时自动化
  • 开源
  • 同时管理多条隧道
  • 可以驻留在系统托盘中
  • 免费(Mobaxterm 有免费版本)
  • 加密存储的密码

1.Mobaxterm

网站: http: //mobaxterm.mobatek.net/

捕获 :

在此输入图像描述

2.SSH隧道管理器

网站: https: //code.google.com/archive/p/ssh-tunnel-manager/

捕获 :

在此输入图像描述