如何将密码传递给scp?

I know it is not recommended, but is it at all possible to pass the user's password to scp?

I'd like to copy a file via scp as part of a batch job and the receiving server does, of course, need a password and, no, I cannot easily change that to key-based authentication.


See also (later) question: stackoverflow.com/questions/1462284/… where one answer mentions another possible way to do this. (NB: this is not a duplicate question - it is the original which the other duplicates.)

Link to expect.nist.gov is broken. Maybe this?: expect.sourceforge.net

sudo apt-get install expect

>> brew info sshpass Error: No available formula with the name "sshpass" We won't add sshpass because it makes it too easy for novice SSH users to ruin SSH's security. ;)

On Ubuntu 12.04 it only worked for me with single quotes over the password (e.g. 'password' instead of "password").

odedfos, yes you need to use single quotes because some password generated chars can have a special interpretation in double quoted string interpolation

This is how you install sshpass apt-get install sshpass

On CentOS it's yum -y install sshpass

The most secure way to do this is with a password file, like this: sshpass -f passwdfile` scp [...]. This way, the password won't show up in ps` listings, etc. and you can protect the password with file permissions.

As I wrote: No, I cannot easily switch to key-based authentication.

Do you mean .authorized_keys rather than . authorization_keys?

A valid use for this would be a bash script that does multiple scp/ssh calls to a server where you want to ask the user for the password for the remote server. The password will then not show in history and you still have the befit of challenging for the password... but only once. I don't want to use a key file because I want to still authenticate the script user.

Another valid use would be if you cannot easily enable key-based authentication on the target. Example: One of the main NAS producers - Synology with their DSM 6.0 - does not support it even in 2016. Sure, you could mess with configuration files and hope an update won't just overwrite it again (updating DSM frequently does break custom modifications). Sometimes - especially when the OP expressly writes they're already aware it's not optimal - people just need a solution.

You should use ssh-copy-id -i ~/.ssh/id_rsa.pub <remote_server> to copy the key on the remote server

Show an example, please.

Also, might want to add C:\Program Files (x86)\PuTTY to your path to use these commands.

SDsolar here's a syntax example: pscp -pw 'MyPa$$word' usernameservername:/somedir/somefile ~/somedest (I hate using putty like this, but it works)

It quite slower than 'scp' but it does the job fine.

I found this to be faster when the destination is configured with a huge password prompt delay. scp has to wait 30+ seconds to let me put in a password, but curl worked immediately.

Ghost8472 configured yes, but sometimes this delay is resolving the hostname, and also happens for sftp.

I got "curl: (1) Protocol "sftp" not supported or disabled in libcurl"

Prerequisite: sudo apt-get install expect

This is in perl, sorry i didn't write that. (5 years ago :) )

It was mentioned in user524607's answer 5 years ago.

Found it, and am adding C:\Program Files (x86)\PuTTY to my path.

The question said he can't use key-based authentication.

Also, if it were me, I assume I can't alter the other server - all I want is to copy a file.

and how exactly are you providing password here?