反向SSH隧道监视器

问题描述 投票:12回答:5

我已经使用PuTTY设置了一个反向ssh隧道,允许我将VNC转换为家用计算机而无需启用NAT端口转发。效果很好,没问题。

我想将隧道设置为“持久服务”,它将在启动时连接并在删除时重新连接。 PS。这是在Windows上。

详尽的谷歌搜索发现了一些产品,但许多似乎已被抛弃,似乎没有一个主要的“街头信誉”。

有没有人有这种类型的东西或任何这些产品的经验?我不需要所有的花里胡哨,只需要可靠性。

windows ssh monitoring tunnel
5个回答
2
投票

wikipedia's comparison of ssh clients有隧道列,SOCKS等可以帮助你找到合适的东西


1
投票

您是否考虑过使用plink并将其作为srvany的服务?


1
投票

使用PuTTY中的plink并在批处理文件中运行。当连接真的死了,plink将退出,这意味着你可以在循环中运行plink。

像这样:

  :: This is a batch file. Save with file name: plink_forever.bat
  :restart
  plink saved_session_name
  goto restart

最后用srvany包装它以使其在登录时开始。

或者更简单:将.bat放在Windows调度程序中并设置为在每次启动时运行一次。

文件:http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter7.html


0
投票

您可以设置任何应用程序以启动Windows并在启动时自动连接您的隧道。我个人使用Easytunnel ...刚刚检查了启动时连接所有隧道的选项,并设置了启动Easytunnel的窗口。它工作得很好,你需要设置你的服务器的不活动超时,否则你将每10分钟左右断开连接。

希望你能运作!


0
投票

我经常使用ssh隧道,但所有管理员都不方便我(UI屏幕太多,不稳定)。我想要一个易于配置和维护的脚本,所以我想出了一个PowerShell脚本。发表here。 SO规则要求我在答案中发布解决方案,很高兴这样做:

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

# LocalPort TargetHost  TargetPort  SshHost SshUsername SshKeyPath 
18080   google.com  80  bastion.example.com User    D:\secure\path\to\private_key.ppk

将其另存为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();
    }
  }
}

然后运行它:

powershell -File autossh.ps1

要使用Windows启动自动启动它,请使用Windows调度程序。

© www.soinside.com 2019 - 2024. All rights reserved.