Guest User

Untitled

a guest
Nov 9th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import paramiko, sys, hashlib, datetime
  2.  
  3. hostname="127.0.0.1"
  4. password="rhrlehd"
  5. path = "/home/keikun"
  6. filename = "test.txt"
  7. source = path+"/"+filename
  8. dest = "test_dest.txt"
  9. username="keikun"
  10. port=22
  11. command = 'md5sum '+source
  12. trycount = 3
  13.  
  14.  
  15. def getHash(hostname, port, username, password):
  16. client = paramiko.SSHClient()
  17. client.load_system_host_keys()
  18. client.set_missing_host_key_policy(paramiko.WarningPolicy)
  19. client.connect(hostname, port=port, username=username, password=password)
  20. stdin, stdout, stderr = client.exec_command(command)
  21. return stdout.read()
  22.  
  23.  
  24. def getFile(hostname, port,username,password, srcFile, destFile):
  25. retVal = ""
  26. client = paramiko.Transport((hostname, port))
  27. try:
  28. client.connect(username=username, password=password)
  29. sftp = paramiko.SFTPClient.from_transport(client)
  30. sftp.get(source,destFile)
  31. finally:
  32. client.close()
  33.  
  34. #f = open(destFile, "a")
  35. #f.write("TEST")
  36. #f.close()
  37.  
  38. retVal = destFile
  39. return retVal
  40.  
  41. def overWrite(source, destFile):
  42. pass
  43.  
  44.  
  45. hashkey = getHash(hostname, port, username, password).decode("utf-8").split(' ')
  46.  
  47. if hashkey[0] :
  48. print(" source :" + hashkey[0])
  49. file = getFile(hostname, port, username, password , source, dest)
  50. destKey = hashlib.md5(open(file, "rb").read()).hexdigest()
  51. print(" destfile :" + destKey)
  52. if hashkey[0] == destKey:
  53. print(str(datetime.datetime.now())," : SUCCESS")
  54. else:
  55. print(str(datetime.datetime.now())," : FAIL")
  56. for i in (range(trycount)):
  57. print(str(datetime.datetime.now())," : RETRY - %d" % i)
  58. file = getFile(hostname, port, username, password , source, dest)
  59. destKey = hashlib.md5(open(file, "rb").read()).hexdigest()
  60.  
  61. else :
  62. print ("fail")
Add Comment
Please, Sign In to add comment