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

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


当前回答

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

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

如果存在:

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

其他回答

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

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

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

如果存在:

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

只需生成一个SSH密钥:

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

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

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

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

下面是一个如何使用expect工具的示例:

sub copyover {
    $scp = Expect->spawn("/usr/bin/scp ${srcpath}/$file $who:${destpath}/$file");
    $scp->expect(30,"ssword: ") || die "Never got password prompt from $dest:$!\n";
    print $scp 'password' . "\n";
    $scp->expect(30,"-re",'$\s') || die "Never got prompt from parent system:$!\n";
    $scp->soft_close();
    return;
}