Reputation: 61
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?
Upvotes: 1
Views: 3677
Reputation: 202222
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".
Upvotes: 4