【问题标题】:SCP in python by using passwordpython中的SCP使用密码
【发布时间】:2017-09-20 11:52:53
【问题描述】:

我一直在尝试使用密码将文件 scp 到远程计算机。我使用了这段代码:

import os
import scp
client = scp.Client(host="104.198.152.xxx", username="nxxx", password="xxxxxx")
client.transfer("script.py", "~/script.py")

正如How to scp in python? 中的建议,但它输出:

File "script.py", line 5, in <module>
    client = scp.Client(host="104.198.152.153", username="nazarihome", password="mohMOH13579")
AttributeError: 'module' object has no attribute 'Client'

我还尝试了人们建议的其他方法,但似乎都不起作用。有人有真正有效的建议吗?

附言如果您的答案取决于密码,我必须使用密码而不是密钥。

【问题讨论】:

  • 你试过scp.SCPClient而不是scp.Client吗?
  • 您从问题中复制了一个示例,而不是答案。再看一下您基于示例的帖子。
  • 您提到的帖子中的代码不是真实的。这是一种愿望清单。

标签: python scp


【解决方案1】:

scp.py GitHub page 具有以下示例,该示例将自身与 paramiko 库一起用于处理 SSL:

from paramiko import SSHClient
from scp import SCPClient

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname='ip', 
            port = 'port',
            username='username',
            password='password',
            pkey='load_key_if_relevant')


# SCPCLient takes a paramiko transport as its only argument
scp = SCPClient(ssh.get_transport())

scp.put('file_path_on_local_machine', 'file_path_on_remote_machine')
scp.get('file_path_on_remote_machine', 'file_path_on_local_machine')

scp.close()

所以你想要的实际类型是scp.SCPClient

【讨论】:

    【解决方案2】:

    这是 2019 年 1 月的工作:

    安装所需的 Python 包:

    pip install scp
    pip install paramiko
    

    在代码中包含库:

    from paramiko import SSHClient
    from scp import SCPClient
    

    为它写了一个函数:

    # SSH/SCP Directory Recursively     
    def ssh_scp_files(ssh_host, ssh_user, ssh_password, ssh_port, source_volume, destination_volume):
        logging.info("In ssh_scp_files()method, to copy the files to the server")
        ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.connect(ssh_host, username=ssh_user, password=ssh_password, look_for_keys=False)
    
        with SCPClient(ssh.get_transport()) as scp:
            scp.put(source_volume, recursive=True, remote_path=destination_volume)
    
    

    现在在代码中任意位置调用它:

    ssh_scp_files(ssh_host, ssh_user, ssh_password, ssh_port, source_volume, destination_volume)

    如果以上都正确实施,您将在控制台/日志中看到成功消息,如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2018-05-25
      • 2018-09-26
      • 2013-02-16
      • 2011-07-12
      相关资源
      最近更新 更多