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

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


当前回答

只需生成一个SSH密钥:

ssh-keygen -t rsa -C "your_email@youremail.com"

复制~/.ssh/id_rsa.pub的内容 最后将它添加到远程计算机~/.ssh/authorized_keys

确保远程机器拥有~的权限0700。~/.ssh/authorized_keys文件夹为0600

其他回答

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

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

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

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

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

您可以在unix/terminal上使用'expect'脚本

例如,创建'test。经验值:

#!/usr/bin/expect
        spawn scp  /usr/bin/file.txt root@<ServerLocation>:/home
        set pass "Your_Password"
        expect {
        password: {send "$pass\r"; exp_continue}
                  }

运行脚本

expect test.exp 

我希望这对你有所帮助。

我有一个简单的方法:

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

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

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

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