Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def download():
- if os.path.exists( dst_dir_path ) == False:
- logger.error( "Cannot access destination folder %s. Please check path and permissions. " % ( dst_dir_path ))
- return 1
- elif os.path.isdir( dst_dir_path ) == False:
- logger.error( "%s is not a folder. Please check path. " % ( dst_dir_path ))
- return 1
- file_list = None
- #transport = paramiko.Transport(( hostname, port))
- paramiko.util.log_to_file('paramiko.log')
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- #transport
- try:
- ssh.connect( hostname, username=username, password=password, timeout=5.0)
- #transport.connect(username=username, password=password )
- except Exception, err:
- logger.error( "Failed to connect to the remote server. Reason: %s" % ( str(err) ) )
- return 1
- try:
- #sftp = paramiko.SFTPClient.from_transport(transport)
- sftp = ssh.open_sftp()
- except Exception, err:
- logger.error( "Failed to start SFTP session from connection to %s. Check that SFTP service is running and available. Reason: %s" % ( hostname, str(err) ))
- return 1
- try:
- sftp.chdir(src_dir_path)
- #file_list = sftp.listdir(path="%s" % ( src_dir_path ) )
- file_list = sftp.listdir()
- except Exception, err:
- logger.error( "Failed to list files in folder %s. Please check path and permissions. Reason: %s" % ( src_dir_path, str(err) ))
- return 1
- match_text = re.compile( file_mask )
- download_count = 0
- for file in file_list:
- # Here is an item name... but is it a file or directory?
- #logger.info( "Downloading file %s." % ( file ) )
- if not re.match( file_mask, file ):
- continue
- else:
- logger.info( "File "%s" name matched file mask "%s". matches %s.Processing file..." % ( file, file_mask, (match_text.match( file_mask ) ) ) )
- src_file_path = "./%s" % ( file )
- dst_file_path = "/".join( [ dst_dir_path, file] )
- retry_count = 0
- while True:
- try:
- logger.info( "Downloading file %s to %s." % ( file, dst_file_path ) )
- #sftp.get( file, dst_file_path, callback=printTotals ) #sftp.get( remote file, local file )
- sftp.get( file, dst_file_path) #sftp.get( remote file, local file )
- logger.info( "Successfully downloaded file %s to %s." % ( file, dst_file_path ) )
- download_count += 1
- break
- except Exception, err:
- if retry_count == retry_threshold:
- logger.error( "Failed to download %s to %s. Reason: %s." % ( file, dst_file_path, str(err) ) )
- sftp.close()
- #transport.close()
- return 1
- else:
- logger.error( "Failed to download %s to %s. Reason: %s." % ( file, dst_file_path, str(err) ) )
- retry_count +=1
- sftp.close()
- transport.close()
- logger.info( "%d files downloaded." % ( download_count ) )
- return 0
Add Comment
Please, Sign In to add comment