1. fixed for-loop iterations
2. fixed and separated making persistent SSH connections and exiting them
3. fixed making temporary directories, persistent SSH connections and exiting them only for hosts that are involved