问题描述
在这里,我试图列出我下载的文件的所有 MD5,并将它们与原始文件进行比较,看看它们是否是相同的文件.我现在无法访问服务器来测试此代码,但我真的很好奇它是否有效......有人有更好的解决方案或他们会改变的东西吗?
Here I am trying to list all the MD5's of the files I downloaded and compare them to the original to see if they are the same Files.I can't access a server to test this code right now but I was really curious if it would work...Does someone have a better solution or something they would change?
#!/usr/bin/python3
import paramiko
import pysftp
import os
import sys
print("Localpath eingeben: ")
localpath = input()
print("Remothpath eingeben: ")
remotepath = input()
k = paramiko.RSAKey.from_private_key_file("/home/abdulkarim/.ssh/id_rsa")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print("connecting")
c.connect(hostname = "do-test", username = "abdulkarim", pkey = k)
print("connected")
sftp = c.open_sftp()
sftp.Connection.get_d(localpath, remotepath)
#sftp.get_d(localpath, remotepath)
def hashCheckDir(f,r):
files = []
# r=root, d=directories, f=files
for r, d, f in os.walk(localpath):
for file in f:
if '.txt' in file:
files.append(os.path.join(r, file))
files1 = []
# r=root, d=directories, f=files
for r, d, f in os.walk(remotepath):
for file in f:
if '.txt' in file:
files.append(os.path.join(r, file))
for i in range(2):
for x in files:
localsum = os.system('md5sum ' + files)
remotesum = os.system('ssh do-test md5sum ' + files1)
if localsum == remotesum:
print ("The lists are identical")
else :
print ("The lists are not identical")
hashCheckDir(localpath,remotepath)
c.close()
我对 Python 还很陌生,所以.. 如果我犯了一些愚蠢的错误,请原谅我.也许我必须先对它们进行排序?
I am pretty new to Python so.. Bear with me if I did some stupid mistake.Maybe I have to sort them first?
推荐答案
启动外部控制台应用程序 (ssh
) 在服务器上执行 md5sum
(并为上面的每个文件打开一个新连接),如果您已经有到同一台服务器的本机 Python SSH 连接.
It's an overkill to launch an external console application (ssh
) to execute md5sum
on the server (and open a new connection for each and every file on top of that), if you already have a native Python SSH connection to the same server.
stdin, stdout, stderr = c.exec_command('md5sum '+ files1)
checksum = stdout.read()
请注意,MD5 已过时,请使用 SHA-256 (sha256sum
).
尽管问题是整个校验和检查是否过大,请参阅:
如何在 SFTP 文件传输期间执行校验和以确保数据完整性?
Though question is whether the whole checksum check isn't an overkill, see:
How to perform checksums during a SFTP file transfer for data integrity?
强制性警告:请勿使用 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 将下载文件的 MD5 与 SFTP 服务器上的文件进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!