【问题标题】:scp between 2 remote hosts - without password2个远程主机之间的scp - 没有密码
【发布时间】:2018-06-05 21:14:30
【问题描述】:

我使用 paramiko 模块,我可以简单地将文件放入和从远程主机获取文件。有没有办法可以在 2 个远程主机之间复制文件? 我的本地主机上有 pem 文件,因此我可以为两台主机建立 ssh。有没有办法在没有(如果可能的话)远程主机上进行额外配置的情况下 - 使用 SSH 密钥并且没有密码?

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privkey = paramiko.RSAKey.from_private_key_file (path_to_priv_key_file)
ssh.connect(host, username = username,  key_filename=path_to_priv_key_file)

我已经连接到远程主机,但是当我尝试时

cmd = "scp –v /usr/local/1.py user1@hos1:/usr/local"
ssh.exec_command(cmd)

我收到Permission denied (publickey).

【问题讨论】:

  • 您是否能够在没有密码的情况下从第一台主机通过 SSH 连接到下一台主机?
  • 看起来您在尝试 scp 的计算机上没有正确的私钥
  • 但我可以在这些主机上执行其他命令
  • 2mariusnn 我无需密码就可以从本地主机连接到主机 1 或主机 2

标签: python ssh paramiko


【解决方案1】:

嘿弗兰切斯卡,

下面的 python 伪代码可能会对你有所帮助... 将 dest_server、source_file、dest_file 替换为您的适当... 它已经过测试并且可以正常工作...

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('dest_server-IP', username='user', password='passwd')

print "connected successfully!"

sftp = ssh.open_sftp() 
sftp.put('source_file', 'dest_file_path_in_dest_server') 
sftp.close() 
print "copied successfully!"

干杯,

苏曼思

【讨论】:

    【解决方案2】:

    基本上,您希望在两台机器之间建立无密码连接。 除非您可以确保没有其他人可以访问客户端计算机,否则这很危险。

    看看如何创建我多年前在我的博客中描述过的无密码连接。 它也将解决 scp 问题。

    http://greg-n-blog.blogspot.com/2009/10/ssh-and-scp-without-being-asked-for.html

    【讨论】:

      【解决方案3】:

      试试这个将目录和嵌套的子目录从本地复制到远程:

      cmd = "sshpass -p {} scp -r {}/* root@{}://{}".format(
          remote_root_pass, 
          local_path, 
          remote_ip, 
          remote_path)
      os.system(cmd)
      

      别忘了导入os,可以查看返回的exitcode(0表示成功)

      您可能还需要“yum install sshpass”

      并将 /etc/ssh/ssh_config StrictHostKeyChecking ask 更改为:StrictHostKeyChecking no

      【讨论】:

        猜你喜欢
        • 2011-03-25
        • 2011-11-29
        • 2012-04-05
        • 2012-06-05
        • 2016-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-06
        相关资源
        最近更新 更多