英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论