Guest User

Untitled

a guest
Feb 11th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. def download():
  2. if os.path.exists( dst_dir_path ) == False:
  3. logger.error( "Cannot access destination folder %s. Please check path and permissions. " % ( dst_dir_path ))
  4. return 1
  5. elif os.path.isdir( dst_dir_path ) == False:
  6. logger.error( "%s is not a folder. Please check path. " % ( dst_dir_path ))
  7. return 1
  8.  
  9. file_list = None
  10. #transport = paramiko.Transport(( hostname, port))
  11. paramiko.util.log_to_file('paramiko.log')
  12. ssh = paramiko.SSHClient()
  13. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  14. #transport
  15. try:
  16. ssh.connect( hostname, username=username, password=password, timeout=5.0)
  17. #transport.connect(username=username, password=password )
  18. except Exception, err:
  19. logger.error( "Failed to connect to the remote server. Reason: %s" % ( str(err) ) )
  20. return 1
  21.  
  22. try:
  23. #sftp = paramiko.SFTPClient.from_transport(transport)
  24. sftp = ssh.open_sftp()
  25. except Exception, err:
  26. logger.error( "Failed to start SFTP session from connection to %s. Check that SFTP service is running and available. Reason: %s" % ( hostname, str(err) ))
  27. return 1
  28.  
  29. try:
  30. sftp.chdir(src_dir_path)
  31. #file_list = sftp.listdir(path="%s" % ( src_dir_path ) )
  32. file_list = sftp.listdir()
  33.  
  34. except Exception, err:
  35. logger.error( "Failed to list files in folder %s. Please check path and permissions. Reason: %s" % ( src_dir_path, str(err) ))
  36. return 1
  37. match_text = re.compile( file_mask )
  38. download_count = 0
  39. for file in file_list:
  40. # Here is an item name... but is it a file or directory?
  41. #logger.info( "Downloading file %s." % ( file ) )
  42. if not re.match( file_mask, file ):
  43. continue
  44. else:
  45. logger.info( "File "%s" name matched file mask "%s". matches %s.Processing file..." % ( file, file_mask, (match_text.match( file_mask ) ) ) )
  46. src_file_path = "./%s" % ( file )
  47. dst_file_path = "/".join( [ dst_dir_path, file] )
  48. retry_count = 0
  49. while True:
  50. try:
  51. logger.info( "Downloading file %s to %s." % ( file, dst_file_path ) )
  52. #sftp.get( file, dst_file_path, callback=printTotals ) #sftp.get( remote file, local file )
  53. sftp.get( file, dst_file_path) #sftp.get( remote file, local file )
  54. logger.info( "Successfully downloaded file %s to %s." % ( file, dst_file_path ) )
  55. download_count += 1
  56. break
  57. except Exception, err:
  58. if retry_count == retry_threshold:
  59. logger.error( "Failed to download %s to %s. Reason: %s." % ( file, dst_file_path, str(err) ) )
  60. sftp.close()
  61. #transport.close()
  62. return 1
  63. else:
  64. logger.error( "Failed to download %s to %s. Reason: %s." % ( file, dst_file_path, str(err) ) )
  65. retry_count +=1
  66.  
  67. sftp.close()
  68. transport.close()
  69. logger.info( "%d files downloaded." % ( download_count ) )
  70. return 0
Add Comment
Please, Sign In to add comment