Automate scp file transfer using a shell script

前端 未结 13 1017
误落风尘
误落风尘 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:09

    You can do it with ssh public/private keys only. Or use putty in which you can set the password. scp doesn't support giving password in command line.

    You can find the instructions for public/private keys here: http://www.softpanorama.org/Net/Application_layer/SSH/scp.shtml

    0 讨论(0)
  • 2020-11-30 18:10

    What about wildcards or multiple files?

    scp file1 file2 more-files* user@remote:/some/dir/
    
    0 讨论(0)
  • 2020-11-30 18:15

    This will work:

    #!/usr/bin/expect -f
    
    spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no file1 file2 file3 user@host:/path/
    expect "password:"
    send "xyz123\r"
    expect "*\r"
    expect "\r"
    interact
    
    0 讨论(0)
  • 2020-11-30 18:18

    Try lftp

    lftp -u $user,$pass sftp://$host << --EOF--
    
    cd $directory
    
    put $srcfile
    
    quit
    
    --EOF--
    
    0 讨论(0)
  • 2020-11-30 18:19
    #!/usr/bin/expect -f
    
    # connect via scp
    spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp
    #######################
    expect {
      -re ".*es.*o.*" {
        exp_send "yes\r"
        exp_continue
      }
      -re ".*sword.*" {
        exp_send "PASSWORD\r"
      }
    }
    interact
    

    http://blogs.oracle.com/SanthoshK/entry/automate_linux_scp_command

    0 讨论(0)
  • 2020-11-30 18:19

    here's bash code for SCP with a .pem key file. Just save it to a script.sh file then run with 'sh script.sh'

    Enjoy

    #!/bin/bash
    #Error function
    function die(){
    echo "$1"
    exit 1
    }
    
    Host=ec2-53-298-45-63.us-west-1.compute.amazonaws.com
    User=ubuntu
    #Directory at sent destination
    SendDirectory=scp
    #File to send at host
    FileName=filetosend.txt
    #Key file
    Key=MyKeyFile.pem
    
    echo "Aperture in Process...";
    
    #The code that will send your file scp
    scp -i $Key $FileName $User@$Host:$SendDirectory || \
    die "@@@@@@@Houston we have problem"
    
    echo "########Aperture Complete#########";
    
    0 讨论(0)
提交回复
热议问题