Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

ssh - Comparing MD5 of downloaded files against files on an SFTP server in Python

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()

I am pretty new to Python so.. Bear with me if I did some stupid mistake. Maybe I have to sort them first?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Instead use SSHClient.exec_command:

stdin, stdout, stderr = c.exec_command('md5sum '+ files1)
checksum = stdout.read()

Note that MD5 is obsolete, use SHA-256 (sha256sum).


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?


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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...