Automate scp file transfer using shell script

I have several n files in a directory on my unix system. Is there a way to write shellscript that will transfer all of these files using scp to the specified remote system. I will specify the password in the script, so I do not need to enter it for each file.

+67
shell scp
Aug 28 '09 at 11:46
source share
12 answers

Instead of a hard-coded password in a shell script, use SSH keys, it is simpler and safer.

$ scp -i ~/.ssh/id_rsa devops@myserver.org:/path/to/bin/*.derp . 

if your private key is in ~/.ssh/id_rsa

To create a public / private key pair:

 $ ssh-keygen -t rsa 

The above will generate 2 files, ~/.ssh/id_rsa (private key) and ~/.ssh/id_rsa.pub (public key)

To configure SSH keys for use (one-time task): Copy the contents of ~/.ssh/id_rsa.pub and paste ~devops/.ssh/authorized_keys into the myserver.org server in a new line. If ~devops/.ssh/authorized_keys does not exist, feel free to create one.

A clear instruction manual is available here .

+68
Apr 24 '13 at 20:32
source share
 #!/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

+37
Dec 16 2018-11-11T00: 00Z
source share

you can also use rsync. It seems to work better for multiple files than scp IMHO.

  rsync -avzh / path / to / dir / user @ remote: / path / to / remote / dir / 

Update

You can use rsync via ssh by adding the '-e' switch:

  rsync -avzh -e ssh / path / do / dir / user @ remote: / path / to / remote / dir / 
+15
Aug 28 '09 at 11:51
source share
 #!/usr/bin/expect -f spawn scp -r BASE.zip abhishek@192.168.1.115:/tmp expect "password:" send "wifinetworks\r" expect "*\r" expect "\r" 
+8
Jul 12 2018-10-12T00:
source share

why don't you try?

 password="your password" username="username" Ip="<IP>" sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH> 
+8
Nov 16 '15 at 6:32
source share

What about wildcards or multiple files?

 scp file1 file2 more-files* user@remote:/some/dir/ 
+4
Aug 28 '09 at 11:49
source share

rsync is a program that behaves the same as rcp but has many other options and uses the rsync remote update protocol to significantly speed up file transfer when the destination file is updated.

The rsync remote update protocol allows rsync to transfer only the differences between two sets of files over a network connection using the efficient checksum algorithm described in the technical report that comes with this package.




Copy a folder from one place to another

  #!/usr/bin/expect -f spawn rsync -a -e ssh username@192.168.1.123:/cool/cool1/* /tmp/cool/ expect "password:" send "cool\r" expect "*\r" expect "\r" 
+3
Aug 17 '11 at 12:38
source share

If it is convenient for you to enter the password once for each script run, you can do it easily using the main SSH 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" 
+2
Jul 02 '14 at 18:20
source share

You can only do this with ssh public / private keys. Or use a putty in which you can set a password. scp does not support password transfers on the command line.

Instructions for public / private keys can be found here: http://www.softpanorama.org/Net/Application_layer/SSH/scp.shtml

0
Sep 15 '09 at 21:13
source share

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
Jan 20 '16 at 1:35
source share

Try lftp

 lftp -u $user,$pass sftp://$host << --EOF-- cd $directory put $srcfile quit --EOF-- 
0
Jul 05 '16 at 23:19
source share

The scp command can be used as a traditional UNIX cp . SO if you do this:

 scp -r myDirectory/ mylogin@host:TargetDirectory 

will work

-one
Aug 17 '11 at 13:13
source share



All Articles