I am new to pysftp I am trying to do the following task
Can someone help me with this script?
I tried connecting to the server but I am unsure how to go about downloading the files and generating a checksum for the files on the downloaded files using python.
As of now I have connected to the server with the below code: *
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
myHostname = "abc.org"
myUsername = "username"
myPassword = "password"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
print ("Connection succesfully stablished ... ")
directory_structure = sftp.listdir_attr()
for attr in directory_structure:
print (attr.filename, attr)
**
The psftp get()
method is documented here.
The python hashlib
module is documented here.
It provides many hash algorithms including MD5 and SHA256.
Example :
import hashlib
content = "header\foobar\nfooter\n" # your file content as a string
h = hashlib.sha256(content.encode())
tampered_content = "header\foobar2\nfooter\n" # your file content (modified) as a string
h2 = hashlib.sha256(tampered_content.encode())
h.digest() == h2.digest() # False
print(h.hexdigest()) # an hexadecimal human-readable string to provide on the server
# 17f1212df75eac78cd7c01c19ea44823add3f778ebe39b22cb5d7415c94c8395