Automate scp file transfer using a shell script

前端 未结 13 1018
误落风尘
误落风尘 2020-11-30 17:24

I have some n number of files in a directory on my unix system. Is there a way to write a shellscript that will transfer all those files via scp to a specified remote system

相关标签:
13条回答
  • 2020-11-30 18:21

    If you are ok with entering your password once for every run of the script, you can do so easily using an SSH master connection.

    #!/usr/bin/env bash
    
    USER_AT_HOST="user@host"  # use "$1@$2" here if you like
    SSHSOCKET=~/".ssh/$USER_AT_HOST"
    
    # This is the only time you have to enter the password:
    # Open master connection:
    ssh -M -f -N -o ControlPath="$SSHSOCKET" "$USER_AT_HOST"
    
    # These do not prompt for your password:
    scp -o ControlPath="$SSHSOCKET" file1.xy "$USER_AT_HOST":remotefile1.xy
    scp -o ControlPath="$SSHSOCKET" file2.xy "$USER_AT_HOST":remotefile2.xy
    
    # You can also use the flag for normal ssh:
    ssh -o ControlPath="$SSHSOCKET" "$USER_AT_HOST" "echo hello"
    ssh -o ControlPath="$SSHSOCKET" "$USER_AT_HOST" "echo world"
    
    # Close master connection:
    ssh -S "$SSHSOCKET" -O exit "$USER_AT_HOST"
    
    0 讨论(0)
提交回复
热议问题