Search code examples
bashrsync

Connection reset by <ip address> port 22 while getting files from a remote machine through a bridge machine via ssh, doing this operation in loop


I have developed a bash script that, given a list of file paths, runs a loop in which:

  1. it accesses a bridge machine via ssh and from here it uses rsync to get files from another machine into a temporary directory.
  2. from my local machine, it gets the file previously copied on the brige machine.

It does these two operation in a loop, each loop to get a different files, the loop is repeated to get 10 different files.

Sometimes the following error pops up. What could be the cause of it?

rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(228) [Receiver=3.2.3]
kex_exchange_identification: read: Connection reset by peer
Connection reset by <ip address> port 22

I have tryed to temporarily ecxclude some files from the list (e.g. excluding the first half of the list, then excluding the second half of the list) and I see that the error does not shows up anymore.

This makes me think it is the server that stops my script because it detects the script is doing too many operations/too quickly (this would explain the message Connection reset by <ip address> port 22).

So I have tryed to put some sleep commands in the loop, but the error still shows up.

What can I do?


Solution

  • As indicated in this thread, and this SO thread, it seems indeed to be an issue with the firewall of the machine-target-of-ssh.

    I have solved it by placing sleep commands between ssh commands and increasing the sleeping time until the error does not show up anymore (15 seconds in my case).

    So my guess is indeed that the firewall of the remote machine stops the executions of commands run by a remote machine via ssh, when these commands exceeds some kind of threshold like "number of commands per time", "memory dedicated to a command", or similars.