12

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

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

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

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

4

7 回答 7

2

维基百科对 ssh 客户端的比较有关于隧道、SOCKS 等的列,可以帮助您找到合适的东西

于 2008-12-16T20:33:20.183 回答
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

于 2012-10-23T04:53:47.523 回答
1

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

于 2008-12-16T20:21:03.373 回答
0

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

希望你让它工作!

于 2009-05-04T17:50:22.807 回答
0

我经常使用 ssh 隧道,但所有管理器都对我不方便(UI 屏幕太多,不太稳定)。我想要一个可以轻松配置和维护的脚本,所以我想出了一个 PowerShell 脚本。张贴在这里。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 调度程序。

于 2019-03-19T03:52:09.953 回答
0

永久隧道是安全漏洞。只要您在网络上,我就已经设置了一个安全的开放服务并打开。它也有内置超时,没有活动 2 分钟,否则 10。超过 https 并且顶部有一些 XTEA 加密。可在 mylinuz.com 获得

截屏

于 2020-09-04T22:09:45.273 回答
0

我有两个主要建议:

  • Teleport:很棒的工具,开源且相对易于使用
  • Ngrok:简单,做你想做的事

我建议使用其中一项服务,而不是自己做。自行设置此类设置可能很危险,因为任何错误配置都会导致攻击者获得对所有连接设备的完全访问权限。

于 2021-11-29T13:44:58.773 回答