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

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


当前回答

确保你有“期望”工具之前,如果没有,就这样做 # apt-get install expect 创建具有以下内容的脚本文件。(# vi /root/脚本文件) 生成SCP /path_from/file_name user_name_here@to_host_name:/path_to 预计“密码: 发送put_password_here \ n; 交互 使用“expect”工具执行脚本文件 # expect /root/scriptfile

其他回答

下面是基于这篇博文的一个穷人的Linux/Python/ expect样例:将简单的shell升级为完全交互式 tty。对于无法安装Expect或向Python添加模块的旧机器,我需要这个功能。

代码:

(
    echo 'scp jmudd@mysite.com:./install.sh .'
    sleep 5
    echo 'scp-passwd'
    sleep 5
    echo 'exit'
) |

python -c 'import pty; pty.spawn("/usr/bin/bash")'

输出:

scp jmudd@mysite.com:install.sh .
bash-4.2$ scp jmudd@mysite.com:install.sh .
Password: 
install.sh                                 100%   15KB 236.2KB/s   00:00    
bash-4.2$ exit
exit

如果您从Windows连接到服务器,Putty版本的scp(“pscp”)允许您使用-pw参数传递密码。

这里的文档中提到了这一点。

从Windows以非交互方式使用SCP:

安装社区版netcmdlets 导入模块 使用Send-PowerShellServerFile -AuthMode password -User MyUser -Password not-secure -Server YourServer -LocalFile C:\downloads\test.txt -RemoteFile C:\temp\test.txt发送非交互式密码的文件

我在这里找到了一个非常有用的答案。

rsync -r -v --progress -e ssh user@remote-system:/address/to/remote/file /home/user/

你不仅可以在那里传递密码,而且它还会在复制时显示进度条。真的太棒了。

您可以使用以下步骤。这对我很管用!

步骤1 - 创建一个普通的文件,假设“fileWithScpPassword”,其中包含目标服务器的ssh密码。

Step2-使用sshpaas -f后跟密码文件名,然后是正常的scp命令。

sshpass -f“scppassword”scp / filepathtou上传user@ip:/目的地目的地/