我想在机器启动时将Win机器隧道连接到SSH服务器。我还希望win机器重启SSH程序,如果它崩溃了。较轻的& SSH程序越稳定,我就越开心。
我有什么选择?
答案 0 :(得分:6)
使用srvany将SSH客户端作为Windows服务启动。然后,您可以将服务设置为在Windows启动时自动启动。
基本上,srvany.exe将允许您从Windows服务控制管理器启动任何程序。
答案 1 :(得分:4)
PuTTY suite可能是你最好的选择,IMO。 (免责声明:我很有偏见,因为Simon Tatham是我大学的朋友。请注意,这意味着我知道他是多么细致......)
答案 2 :(得分:1)
如果您希望程序重新启动以断开连接,我所知道的最好的应该是软件推荐:AlwaysUp(2015年5月不免费)。
仍然轻量级,4.2MB安装程序和不太大的内存消费者:
C:\>tasklist | find "always" /i
AlwaysUp.exe 3112 Console 1 17.388 KB
转换为服务时,使用SrvAny
或SC
稳定性(或只是使服务正常工作)可能是一场噩梦。 AlwaysUp ,只要我测试过,就可以解决大多数问题并为您提供额外选项,例如详细的日志,统计信息,电子邮件提醒等等。
答案 3 :(得分:0)
肯定是最好的选择,但是它不支持自动重启。我个人经常使用ssh隧道,我有30多个隧道。我想拥有一个易于配置和维护的脚本,因此我想到了一个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