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

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

有帮助吗?

解决方案

您可以使用类似的工具编写脚本 预计 (也有方便的绑定,例如 预期 对于Python)。

其他提示

使用 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
    • apt install sshpass
  • Centos/软呢帽
    • yum install sshpass
  • 带 macports 的 mac
    • port install sshpass
  • 带有冲泡功能的Mac
    • brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master‌​/Library/Formula/ssh‌​pass.rb

只需生成一个 ssh 密钥,例如:

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

复制内容 ~/.ssh/id_rsa.pub最后将其添加到远程机器 ~/.ssh/authorized_keys

确保远程机器有权限 0700 for ~./ssh folder0600 for ~/.ssh/authorized_keys

如果您从 Windows 连接到服务器,Putty 版本的 scp(“pscp”)可让您通过 -pw 范围。

此处的文档中提到了这一点。

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

例如创建 'test.exp' :

#!/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 

我希望这有帮助。

curl 可以用作 scp 的替代方法来复制文件,并且它支持命令行上的密码。

curl --insecure --user username:password -T /path/to/sourcefile sftp://desthost/path/

这是一个如何执行此操作的示例 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;
}

没有人提到,但是 腻子 scp (pscp) 有一个 -pw 密码选项。

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

  1. 确保您之前有“预期”工具,如果没有,请执行此操作

    # apt-get install expect

  2. 创建包含以下内容的脚本文件。(# vi /root/脚本文件)

    spawn scp /path_from/file_name user_name_here@to_host_name:/path_to

    expect "password:"

    send put_password_here\n;

    interact

  3. 使用“expect”工具执行脚本文件

    # expect /root/scriptfile

设置完成后 ssh-keygen 如上所述,你可以做

scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/

如果你想减少每次打字的次数,你可以修改你的 .bash_profile 文件并放置

alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/

然后从你的终端做 source ~/.bash_profile. 。之后如果您输入 remote_scp 在你的终端中它应该运行 scp 没有密码的命令。

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

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

如果存在:

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

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

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

对于 rhel/centos 6 获取 sshpass 的步骤:

# wget http://epel.mirror.net.in/epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm
# yum install sshpass

来源: https://community.mapr.com/thread/9040

我发现这个答案非常有帮助 这里.

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

您不仅可以在此处传递密码,而且复制时还会显示进度条。非常棒。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top