Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send password when using scp to copy files from one server to another [duplicate]

using scp to copy files from 1 unix server to another regularly and performing certain actions. to do this quickly I wish to use a unix script which does the scp and inputs the password required to complete the scp.

I have tried the expect command to send the password throught the unix command line however unable to achieve this so far.

sample commands

scp ./abc.txt hostname/abc.txt
expect "*password:*"
send "mypassword\r"

I get these errors:

couldn't read file "password: ": no such file or directory
myscript.sh[5]: send: not found [No such file or directory]

am I missing something?

like image 855
cherry2891 Avatar asked Dec 06 '13 17:12

cherry2891


People also ask

How do you securely transfer files between servers with SCP?

The scp tool relies on SSH (Secure Shell) to transfer files, so all you need is the username and password for the source and target systems. Another advantage is that with SCP you can move files between two remote servers, from your local machine in addition to transferring data between local and remote machines.

Can we pass password in SCP command?

The SCP command; on its own, is not sufficient enough to accommodate password authentication under a one-line command usage and therefore leads to a successive password prompt for the OS user to enter the required login passcode.

Can SCP transfer files?

Introduction. SCP stands for Secure Copy Protocol. It is a tool that can be used to transfer files from a local host to a remote host, from a remote host to a local host, or between two remote hosts.


2 Answers

Just pass with sshpass -p "your password" at the beginning of your scp command

sshpass -p "your password" scp ./abc.txt hostname/abc.txt
like image 146
Akki Avatar answered Sep 19 '22 12:09

Akki


// copy /tmp/abc.txt to /tmp/abc.txt (target path)

// username and password of 10.1.1.2 is "username" and "password"

sshpass -p "password" scp /tmp/abc.txt [email protected]:/tmp/abc.txt

// install sshpass (ubuntu)

sudo apt-get install sshpass
like image 34
KunMing Xie Avatar answered Sep 17 '22 12:09

KunMing Xie