Consegui combinar os comandos start
e taskkill
para integrar o plink ao meu script em lote:
@echo off
set local_port=1580
set remote_ip=10.19.0.241
set remote_port=1580
set wait_for_tunnel_seconds=4
set [email protected]
min.exe
echo --- Closing any active tunnels
taskkill /f /fi "imagename eq plink.exe"
echo --- Opening tunnel from local port %local_port% to remote %remote_ip%:%remote_port%
start /min plink -T -L %local_port%:%remote_ip%:%remote_port% %ssh_host%
ping -n %wait_for_tunnel_seconds% 127.0.0.1 >nul
tcping -n 1 localhost %local_port%
IF %ERRORLEVEL% neq 0 (
echo --- Failed to open tunnel. Canceling.
taskkill /f /fi "imagename eq plink.exe"
rem Use the port number as the exit code (makes it obvious in scheduled task last run result)
EXIT /b %local_port%
)
echo --- Starting synchronization
set error_=0
sync_command.exe
set error_=%ERRORLEVEL%
if %error_% neq 0 (
echo --- Sync completed, but with errors. Exit result: %error_%
) else (
echo --- Sync completed
)
echo --- Closing tunnel
taskkill /f /fi "imagename eq plink.exe"
exit /b %error_%
Outras ferramentas utilizadas:
- MIN.EXE para minimizar a janela de comando imediatamente (isso é chamado como uma tarefa agendada)
- TCPING.EXE para verificar se o túnel está ativo