How to scp with a second remote host

前端 未结 7 637
终归单人心
终归单人心 2020-12-04 05:55

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

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 06:20

    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.

提交回复
热议问题