How to scp with a second remote host

前端 未结 7 639
终归单人心
终归单人心 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:18

    Double ssh

    Even in your complex case, you can handle file transfer using a single command line, simply with ssh ;-)
    And this is useful if remote1 cannot connect to localhost:

    ssh user1@remote1 'ssh user2@remote2 "cat file"' > file
    

    tar

    But you loose file properties (ownership, permissions...).

    However, tar is your friend to keep these file properties:

    ssh user1@remote1 'ssh user2@remote2 "cd path2; tar c file"' | tar x
    

    You can also compress to reduce network bandwidth:

    ssh user1@remote1 'ssh user2@remote2 "cd path2; tar cj file"' | tar xj
    

    And tar also allows you transferring a recursive directory through basic ssh:

    ssh user1@remote1 'ssh user2@remote2 "cd path2; tar cj ."' | tar xj
    

    ionice

    If the file is huge and you do not want to disturb other important network applications, you may miss network throughput limitation provided by scp and rsync tools (e.g. scp -l 1024 user@remote:file does not use more than 1 Mbits/second).

    But, a workaround is using ionice to keep a single command line:

    ionice -c2 -n7 ssh u1@remote1 'ionice -c2 -n7 ssh u2@remote2 "cat file"' > file
    

    Note: ionice may not be available on old distributions.

提交回复
热议问题