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

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


当前回答

您可以使用像expect这样的工具编写脚本(也有方便的绑定,比如Python的Pexpect)。

其他回答

使用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

Make sure password authentication is enabled on the target server. If it runs Ubuntu, then open /etc/ssh/sshd_config on the server, find lines PasswordAuthentication=no and comment all them out (put # at the start of the line), save the file and run sudo systemctl restart ssh to apply the configuration. If there is no such line then you're done. Add -o PreferredAuthentications="password" to your scp command, e.g.: scp -o PreferredAuthentications="password" /path/to/file user@server:/destination/directory

上面提到的所有解决方案都可以工作,只有当你安装了应用程序,或者你应该有管理权限安装除sshpass。

我发现这个非常有用的链接可以简单地在后台启动scp。

$ nohup scp file_to_copy user@server:/path/to/copy/the/file > nohup.out 2>&1

https://charmyin.github.io/scp/2014/10/07/run-scp-in-background/

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

在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。

确保你有“期望”工具之前,如果没有,就这样做 # 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