Pergunta

How would I create a simple batch script(windows) to close then current plink session if it times out and reconnect automatically?

something like this:

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

or maybe

if "plink.exe" == "false" ( "batch to open new plink instance" )

Foi útil?

Solução

Here you go.

@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

You know, if you have root access on the ssh server, you could modify sshd_config and have the server send no-op packets every few minutes to prevent connections from timing out due to inactivity. Here's an example snippet of my sshd_config:

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

Add that to your sshd_config and restart the ssh daemon. That might save you from having to do something so hackish on the client-side.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top