【问题标题】:How to transfer a file to ssh server in an ssh-connection made by paramiko?如何在 paramiko 建立的 ssh 连接中将文件传输到 ssh 服务器?
【发布时间】:2012-07-15 00:24:54
【问题描述】:

我正在使用 Python 的 paramiko 数据包来保持与服务器的 ssh 连接:

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("xxx.xxx.xxx.xxx",22,username=xxx,password='',timeout=4)

我想用这个 ssh-connection 将文件传输到 ssh 服务器,我该怎么做?

就像使用 scp a-file xxx@xxx.xxx.xxx.xxx:filepath 命令?

【问题讨论】:

    标签: python ssh sftp paramiko


    【解决方案1】:

    这是https://www.programcreek.com/python/example/4561/paramiko.SSHClient的另一个例子

    def copy_file(hostname, port, username, password, src, dst):
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        print (" Connecting to %s \n with username=%s... \n" %(hostname,username))
        t = paramiko.Transport(hostname, port)
        t.connect(username=username,password=password)
        sftp = paramiko.SFTPClient.from_transport(t)
        print ("Copying file: %s to path: %s" %(src, dst))
        sftp.put(src, dst)
        sftp.close()
        t.close() 
    

    【讨论】:

      【解决方案2】:

      试试这个:

      s = paramiko.SSHClient()
      s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      s.connect("xxx.xxx.xxx.xxx",22,username=xxx,password='',timeout=4)
      
      sftp = s.open_sftp()
      sftp.put('/home/me/file.ext', '/remote/home/file.ext')
      

      【讨论】:

      • 关于上传本身的答案是正确的。 – 但是以这种方式使用AutoAddPolicy 会产生安全后果。这样做,您将失去对MITM attacks 的保护。有关正确的解决方案,请参阅Paramiko “Unknown Server”
      猜你喜欢
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2022-06-21
      相关资源
      最近更新 更多