关于linux:如何将密码传递给scp?

关于linux:如何将密码传递给scp?

How to pass password to scp?

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

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


使用sshpass:

1
sshpass -p"password" scp -r user@example.com:/some/remote/path /some/local/path

否则密码不会显示在bash历史记录中

1
sshpass -f"/path/to/passwordfile" scp -r user@example.com:/some/remote/path /some/local/path

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

安装:

  • Ubuntu的/ Debian的

    • apt install sshpass
  • centos / fedora

    • yum install sshpass
  • mac w / macports

    • port install sshpass
  • 带酿造机的mac

    • brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master??/Library/Formula/ssh??pass.rb

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

1
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参数传递密码。

这里的文档中提到了这一点。


您可以使用诸如Expect之类的工具编写脚本(也有便捷的绑定,例如Pexpect for Python)。


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

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

您可以在Unix /终端上使用'expect'脚本

例如创建'test.exp':

1
2
3
4
5
6
7
#!/usr/bin/expect
        spawn scp  /usr/bin/file.txt root@<ServerLocation>:/home
        set pass"Your_Password"
        expect {
        password: {send"$pass
"; exp_continue}
                  }

运行脚本

1
expect test.exp

希望对您有所帮助。


以下是使用expect工具的方法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sub copyover {
    $scp=Expect->spawn("/usr/bin/scp ${srcpath}/$file $who:${destpath}
+/$file");
    $scp->expect(30,"ssword:") || die"Never got password prompt from
+ $dest:$!
";
    print $scp 'password' ."
";
    $scp->expect(30,"-re",'$\s') || die"Never got prompt from parent
+system:$!
";
    $scp->soft_close();
    return;
}

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

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


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

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

如果存在:

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

按照上述说明设置ssh-keygen后,您可以

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

如果要减少每次键入的次数,则可以修改.bash_profile文件并放入

1
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命令。


  • 确保您拥有"期望"工具,如果没有,请先做

    # apt-get install expect

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

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

    expect"password:"

    send put_password_here
    ;

    interact

  • 使用"期望"工具执行脚本文件

    # expect /root/scriptfile


  • 仅当您安装了该应用程序或您应该具有管理员权限才能安装上述所有解决方案,或者sshpass除外。

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

    1
    $ 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/


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

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


    sshpass的步骤对于rhel / centos 6:

    1
    2
    3
    # 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


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

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

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


    推荐阅读