标签云

微信群

扫码加入我们

WeChat QR Code

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.)

2018年08月14日49分07秒

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

2018年08月14日49分07秒

sudo apt-get install expect

2018年08月15日49分07秒

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

2018年08月15日49分07秒

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

2018年08月14日49分07秒

This is how you install sshpass apt-get install sshpass

2018年08月15日49分07秒

On CentOS it's yum -y install sshpass

2018年08月14日49分07秒

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.

2018年08月14日49分07秒

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

2018年08月14日49分07秒

Argelbargel - They answered 4 years after you accepted an answer, I don't think they were particularly concerned with your requirements from 4 years ago so much as wanting to share how future people who have the same question and arrive here via Google can do this.

2018年08月14日49分07秒

Do you mean .authorized_keys rather than . authorization_keys?

2018年08月15日49分07秒

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.

2018年08月14日49分07秒

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.

2018年08月15日49分07秒

Show an example, please.

2018年08月15日49分07秒

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

2018年08月15日49分07秒

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

2018年08月15日49分07秒

Thank you very much!

2018年08月15日49分07秒

Prerequisite: sudo apt-get install expect

2018年08月15日49分07秒

Being a Linux noob, what does this run in? Bash doesn't accept sub...

2018年08月15日49分07秒

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

2018年08月15日49分07秒

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

2018年08月15日49分07秒

Please show an example.

2018年08月14日49分07秒

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

2018年08月15日49分07秒

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

2018年08月15日49分07秒

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

2018年08月14日49分07秒

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

2018年08月15日49分07秒

and how exactly are you providing password here?

2018年08月14日49分07秒