Smart way to copy multiple files from different paths usinc scp

后端 未结 5 862
北荒
北荒 2021-02-08 13:04

I would like to know an easy way to use scp to copy files and folders that are present in different paths on my file system. The ssh destination server requests a password and I

相关标签:
5条回答
  • 2021-02-08 13:10

    Alternatively, if you cannot use public key authentication, you may add the following configuration to SSH (either to ~/.ssh/config or as the appropriate command-line arguments):

    ControlMaster auto
    ControlPath /tmp/ssh_mux_%h_%p_%r
    ControlPersist 2m
    

    With this config, the SSH connection will be kept open for 2 minutes so you'll only need to type the password the first time.

    This post has more details on this feature.

    0 讨论(0)
  • 2021-02-08 13:21

    If you can express all the names of the files you want to copy from the remote system using a single glob pattern, then you can do this in a single scp command. This usage will only support a single destination folder on the local system for all files though. For example:

    scp 'RemoteHost:/tmp/[abc]*/*.tar.gz' .
    

    copies all of the files from the remote system which are names (something).tar.gz and which are located in subdirectories of /tmp whose names begin with a, b, or c. The single quotes are to protect the glob pattern from being interpreted from the shell on the local system.

    If you cannot express all the files you want to copy as a single glob pattern and you still want the copy to be done using a single command (and a single SSH connection which will ask for your passsword only once) then you can either:

    • Use a different command than scp, like sftp or rsync, or
    • Open an SSH master connection to the remote host and run several scp commands as slaves of that master. The slaves will piggyback on the master connection which stays open throughout and won't ask you for a password. Read up on master & slave connections in the ssh manpage.
    0 讨论(0)
  • 2021-02-08 13:24

    From this site:

    Open the master

    SSHSOCKET=~/.ssh/myUsername@targetServerName ssh -M -f -N -o ControlPath=$SSHSOCKET myUsername@targetServerName

    Open and close other connections without re-authenticating as you like

    scp -o ControlPath=$SSHSOCKET myUsername@targetServerName:remoteFile.txt ./

    Close the master connection

    ssh -S $SSHSOCKET -O exit myUsername@targetServerName

    It's intuitive, safer than creating a key pair, faster than creating a compressed file and worked for me!

    0 讨论(0)
  • 2021-02-08 13:33

    in addition to the already mentioned glob:

    you can use {,} to define alternative paths/pathparts in one single statement

    e.g.: scp user@host:/{PATH1,PATH2} DESTINATION

    0 讨论(0)
  • 2021-02-08 13:36

    create a key pair, copy the public key to the server side.

    ssh-keygen -t rsa
    

    Append content inside the file ~/.ssh/identity.pub to file ~/.ssh/authorized_keys2 of server side user. You need not to type password anymore.

    However, be careful! anybody who can access your "local account" can "ssh" to the server without password as well.

    0 讨论(0)
提交回复
热议问题