Python Paramiko 目录遍历 SFTP [英] Python Paramiko directory walk over SFTP

查看:102
本文介绍了Python Paramiko 目录遍历 SFTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过 SSH 在另一台计算机上执行 os.walk() ?问题是 os.walk() 在本地机器上执行,我想通过 ssh 连接到另一台名为sunbeam"的计算机,遍历一个目录并为其中的每个文件生成 MD5 哈希值.

How to do os.walk() but on another computer through SSH? The problem is that os.walk() executes on a local machine and I want to ssh to another computer called 'sunbeam', walk through a directory and generate MD5 hashes for every file within.

到目前为止我写的看起来像这样(下面的代码),但它不起作用.任何帮助将不胜感激.

What I wrote so far looks like this (below code) but it doesn't work. Any help would be greatly appreciated.

try:
    hash_array = []
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('sunbeam', port=22, username='xxxx', password='filmlight')

    spinner.start()
    for root, dirs, files in os.walk(_path):
        for file in files:
            file_path = os.path.join(os.path.abspath(root), file)

            #  generate hash code for file
            hash_array.append(genMD5hash(file_path))

            file_nb += 1
    spinner.stop()
    spinner.ok('Finished.')

    return hash_array
except Exception as e:
    print(e)
    return None
finally:
    ssh.close() 

推荐答案

要使用 Paramiko 递归列出目录,使用标准文件访问接口 SFTP,您需要使用 SFTPClient.listdir_attr:

To recursively list a directory using Paramiko, with a standard file access interface, the SFTP, you need to implement a recursive function with a use of SFTPClient.listdir_attr:

from stat import S_ISDIR, S_ISREG

def listdir_r(sftp, remotedir):
    for entry in sftp.listdir_attr(remotedir):
        remotepath = remotedir + "/" + entry.filename
        mode = entry.st_mode
        if S_ISDIR(mode):
            listdir_r(sftp, remotepath)
        elif S_ISREG(mode):
            print(remotepath)

基于 Linux 中的 Python pysftp get_r 在 Linux 上运行良好,但在 Windows 上不运行.

或者,pysftp 实现了一个 os.walk 等价物:Connection.walktree.

Alternatively, pysftp implements an os.walk equivalent: Connection.walktree.

尽管使用 SFTP 协议获取远程文件的 MD5 会遇到麻烦.

Though you will have troubles getting MD5 of a remote file with SFTP protocol.

虽然 Paramiko 支持它的 SFTPFile.check,大多数 SFTP 服务器(尤其是最普遍的 SFTP/SSH 服务器 - OpenSSH)都没有.见:
如何检查Paramiko是否成功将文件上传到SFTP服务器?
如何在 SFTP 文件传输期间执行校验和以确保数据完整性?

While Paramiko supports it with its SFTPFile.check, most SFTP servers (particularly the most widespread SFTP/SSH server – OpenSSH) do not. See:
How to check if Paramiko successfully uploaded a file to an SFTP server? and
How to perform checksums during a SFTP file transfer for data integrity?

因此,您很可能不得不求助于使用 shell md5sum 命令(如果您甚至可以访问 shell).无论如何,一旦您必须使用 shell,请考虑使用 shell 列出文件,因为这将比通过 SFTP 快得多.

So you will most probably have to resort to using shell md5sum command (if you even have a shell access). And once you have to use the shell anyway, consider listing the files with shell, as that will be magnitudes faster then via SFTP.

请参阅md5 目录树中的所有文件.

使用 SSHClient.exec_command:
比较下载文件的 MD5 与 Python 中 SFTP 服务器上的文件

强制性警告:请勿使用 AutoAddPolicy – 您将失去针对 MITM 攻击 这样做.如需正确的解决方案,请参阅Paramiko未知服务器".

Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

这篇关于Python Paramiko 目录遍历 SFTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆