I wonder if there is a way for me to SCP the file from remote2 host directly from my local machine by going through a remote1 host.
The networks only allow connectio
Small addition to Olibre's solution here, which I worked with using this source.
Just as you have the three ways to use tar for copying from remote host to local, the following works for local host to remote host copying in double ssh situations: (run them in the directory where the files have to be copied from, otherwise use fullpath/filename)
Transfer single file without compression:
tar c filename | ssh user1@remote1 'ssh -Y user2@remote2 "path2 && tar x"'
Transfer single file with compression:
tar cj filename | ssh user1@remote1 'ssh -Y user2@remote2 "path2 && tar xj"'
Recursive directory transfer:
tar cj . | ssh user1@remote1 'ssh -Y user2@remote2 "path2 && tar xj"'
The && here prevents the command from running if the first half of the command does not work - for example if the directory is missing or there is an error in the source/destination path names.