0

Is there anyway to write batch file in windows to reconnect ssh connection automatically? I'm using ssh as vpn connection and it keeps giving error and disconnect. commad: ssh -p 22 -N -D 5060 username@server-ip

it would disconnect every 2 minutes and give this error

client_loop: send disconnect: Connection reset

I want to know if there is a way to make a batch file for reconnecting automatically?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
Mj Ebrahimzadeh
  • 587
  • 9
  • 22

1 Answers1

1
@echo off
:start
   ssh -p 22 -N -D 5060 username@server-ip
   echo Connection lost. Reconnecting...
   timeout /t 5 /nobreak > nul
goto start

This batch file uses a loop to continuously attempt to connect to the SSH server. When the connection is lost, the batch file will wait for 5 seconds (specified by the timeout command) before attempting to reconnect. The > nul part of the timeout command is used to hide the output message.

Save this script in a text editor as ssh_reconnect.bat, and then run it from the command prompt by navigating to the directory where the script is saved and typing ssh_reconnect.bat.

You can customize the timeout value as needed to adjust the time between reconnection attempts. Additionally, you can modify the echo message to display a different message when the connection is lost.

Aacini
  • 65,180
  • 12
  • 72
  • 108
Reily
  • 11
  • 1