我知道不建议这样做,但是是否有可能将用户的密码传递给scp?

我想通过scp复制一个文件,作为批处理作业的一部分,接收服务器当然需要密码,不,我不能轻易地将其更改为基于密钥的身份验证。


当前回答

将文件从一个服务器复制到另一个服务器(在脚本上)

在ubuntu或其他Linux机器上安装putty。Putty随pscp附带。我们可以用pscp复制文件。

apt-get update
apt-get install putty 
echo n |  pscp -pw "Password@1234" -r user_name@source_server_IP:/copy_file_path/files /path_to_copy/files

有关更多选项,请参阅pscp help。

其他回答

使用sshpass:

sshpass -p "password" scp -r user@example.com:/some/remote/path /some/local/path

所以密码不会显示在bash历史记录中

sshpass -f "/path/to/passwordfile" scp -r user@example.com:/some/remote/path /some/local/path

上面的操作将路径的内容从远程主机复制到本地。

安装:

ubuntu / debian 安装sshpass centos / fedora Yum安装sshpass MAC / macports 端口安装sshpass MAC / brew brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

我有一个简单的方法:

使用相同的scp cmd,因为您使用ssh密钥,即

scp -C -i <path_to打开sshkey> <'local file_path'> user@<ip_address_VM>: <'remote file_path'>

用于从本地传输文件到远程

但是不要提供正确的<path_to_opensshkey>,而是使用一些垃圾路径。由于错误的密钥路径,您将被要求输入密码,而不是你现在可以简单地通过密码来完成工作!

另一种方法是将用户密钥的公共部分添加到目标系统上的授权密钥文件中。在发起传输的系统上,可以运行ssh-agent守护进程,并将密钥的私有部分添加到代理中。然后可以将批作业配置为使用代理获取私钥,而不是提示输入密钥的密码。

这应该可以在UNIX/Linux系统或Windows平台上使用pageant和pscp实现。

您可以使用ssh-copy-id添加ssh密钥:

$which ssh-copy-id #check whether it exists

如果存在:

ssh-copy-id  "user@remote-system"

没有人提到它,但是Putty scp (pscp)有一个-pw选项作为密码。

文档可以在这里找到:https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter5.html#pscp