Linux 如何将密码传递给scp?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:24:02  来源:igfitidea点击:

How to pass password to scp?

linuxscpio-redirection

提问by Argelbargel

I know it is not recommended, but is it at all possible to pass the user's password to scp?

我知道不推荐这样做,但是完全可以将用户的密码传递给 scp 吗?

I'd like to copy a file via scp as part of a batch job and the receiving server does, of course, need a password and, no, I cannot easily change that to key-based authentication.

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

采纳答案by Pat Notz

You can script it with a tool like expect(there are handy bindings too, like Pexpectfor Python).

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

回答by Espo

Here is an example of how you do it with expecttool:

以下是如何使用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;
}

回答by rjray

An alternative would be add the public half of the user's key to the authorized-keys file on the target system. On the system you are initiating the transfer from, you can run an ssh-agent daemon and add the private half of the key to the agent. The batch job can then be configured to use the agent to get the private key, rather than prompting for the key's password.

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

This should be do-able on either a UNIX/Linux system or on Windows platform using pageant and pscp.

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

回答by gWay

If you are connecting to the server from Windows, the Putty version of scp ("pscp") lets you pass the password with the -pwparameter.

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

This is mentioned in the documentation here.

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

回答by mustafaturan

just generate a ssh key like:

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

ssh-keygen -t rsa -C "[email protected]"

copy the content of ~/.ssh/id_rsa.puband lastly add it to the remote machines ~/.ssh/authorized_keys

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

make sure remote machine have the permissions 0700 for ~./ssh folderand 0600 for ~/.ssh/authorized_keys

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

回答by KevinS

Use sshpass:

使用sshpass

sshpass -p "password" scp -r [email protected]:/some/remote/path /some/local/path

or so the password does not show in the bash history

或者密码没有显示在 bash 历史记录中

sshpass -f "/path/to/passwordfile" scp -r [email protected]:/some/remote/path /some/local/path

The above copies contents of path from the remote host to your local.

以上将 path 的内容从远程主机复制到本地。

Install :

安装 :

  • ubuntu/debian
    • apt install sshpass
  • centos/fedora
    • yum install sshpass
  • mac w/ macports
    • port install sshpass
  • mac w/ brew
    • brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
  • ubuntu/debian
    • apt install sshpass
  • Centos/Fedora
    • yum install sshpass
  • mac 带 macports
    • port install sshpass
  • mac 带 brew
    • brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

回答by Prabhjeet

You can use the 'expect' script on unix/terminal

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

For example create 'test.exp' :

例如创建 '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}
                  }

run the script

运行脚本

expect test.exp 

I hope that helps.

我希望这有帮助。

回答by valk

I found this really helpful answer here.

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

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

Not only you can pass there the password, but also it will show the progress bar when copying. Really awesome.

您不仅可以将密码传递给那里,还可以在复制时显示进度条。非常棒。

回答by Kevin Chui

  1. make sure you have "expect" tool before, if not, do it

    # apt-get install expect

  2. create the a script file with following content. (# vi /root/scriptfile)

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

    expect "password:"

    send put_password_here\n;

    interact

  3. execute the script file with "expect" tool

    # expect /root/scriptfile

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

    # apt-get install expect

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

    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

回答by galushka

Nobody mentioned it, but Putty scp(pscp) has a -pw option for password.

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

Documentation can be found here: https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter5.html#pscp

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