1

如果超时并自动重新连接,我将如何创建一个简单的批处理脚本(windows)来关闭当前的 plink 会话?

像这样的东西:

if "plink.exe" == "false" (
    "plink command to connect to SSH Server."
)

或者可能

if "plink.exe" == "false" ("批量打开新的 plink 实例")

4

1 回答 1

4

干得好。

@echo off
setlocal
:: modify this line as appropriate
set plink_args=-P 22 -i c:\path\to\private.ppk user@host
set errors=0

:loop

:: if "find" exits with a non-zero status, plink.exe isn't running.
( tasklist /fi "IMAGENAME eq plink.exe" | find /i "plink.exe" >NUL && (
    set errors=0
) ) || (
    start "" plink.exe %plink_args%
    set /a "errors+=1"
)

if %errors% geq 5 (
    echo Unable to connect %errors% times in a row.  Stopping.
    goto :EOF
)

:: pause for 10 seconds (-n seconds + 1)
ping -n 11 0.0.0.0 >NUL
goto loop

您知道,如果您在 ssh 服务器上具有 root 访问权限,则可以修改sshd_config并让服务器每隔几分钟发送一次 no-op 数据包,以防止连接由于不活动而超时。这是我的示例片段sshd_config

# noop anti-idle for 12 hours (10 minutes * 72)
ClientAliveInterval 600
ClientAliveCountMax 72

将其添加到您的sshd_config并重新启动 ssh 守护程序。这可能会使您不必在客户端做一些如此骇人听闻的事情。

于 2013-04-16T16:50:43.003 回答