批处理文件以保持SSH重新连接

huangapple go评论45阅读模式
英文:

Batch file to keep reconnect ssh

问题

有没有办法在Windows中编写批处理文件,以自动重新连接SSH连接?
我正在将SSH用作VPN连接,但它一直出现错误并断开连接。
命令:ssh -p 22 -N -D 5060 username@server-ip
它每2分钟就会断开连接并显示以下错误信息:

client_loop: send disconnect: Connection reset

我想知道是否有办法创建一个批处理文件来实现自动重新连接?

英文:

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?

答案1

得分: 1

@echo off
:start
   ssh -p 22 -N -D 5060 username@server-ip
   echo 连接丢失,正在重新连接...
   timeout /t 5 /nobreak > nul
goto start

这个批处理文件使用一个循环来不断尝试连接到SSH服务器。当连接丢失时,批处理文件会在尝试重新连接之前等待5秒(由timeout命令指定)。timeout命令的" > nul"部分用于隐藏输出消息。

将此脚本保存为文本编辑器中的ssh_reconnect.bat,然后通过在命令提示符中导航到脚本保存的目录并键入ssh_reconnect.bat来运行它。

您可以根据需要自定义超时值以调整重新连接尝试之间的时间。此外,您可以修改echo消息以在连接丢失时显示不同的消息。

英文:
@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.

huangapple
  • 本文由 发表于 2023年2月23日 21:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定