def upload_files(files): uploadConfig = load_config(CONFIG_DIR + "ftp_upload_creds.yaml") ftpServer = uploadConfig["ftpServer"] ftpPort = uploadConfig["port"] ftpUser = uploadConfig["user"] ftpPassword = uploadConfig["password"] ftpUploadDir = uploadConfig["uploadDir"] ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(ftpServer, username=ftpUser, password=ftpPassword, port=ftpPort) except paramiko.SSHException: log_action("ERROR", "Failed to establish %s connection to server %s" % ("SFTP", ftpServer), "SFTP_upload") raise # change to appropriate directory for server sftp = ssh.open_sftp() try: sftp.chdir(ftpUploadDir) except: log_action("ERROR", "Failed to change directory to %s on %s" %(ftpUploadDir, ftpServer), "SFTP_upload") raise for f in files: if not f.endswith(".uploaded"): remoteFilePath = os.path.basename(f) # if f.endswith('.zip'): # if zipfile.is_zipfile(f): # fz = zipfile.ZipFile(f, 'r') # fz.extractall() # # get names of files in archive: # filesInZipFile = fz.namelist() # # rename unzipped files # for fu in filesInZipFile: # if not fu.startswith(gateway): # os.rename(f, gateway + "_" + fu) # fz.close() # # delete zip file # os.remove(localFileName) # else: # log_action("ERROR", "Invalid attachment file %s received from %s" % (localFileName, gateway), "Email") try: sftp.put(f, remoteFilePath) except: log_action("ERROR", "Error uploading file " + f, "SFTP_upload") raise else: log_action("SUCCESS", f + " uploaded successfully", "SFTP_upload") os.rename(f, f + ".uploaded") ssh.close() return
def connect_sftp(): uploadConfig = load_config(CONFIG_DIR + "ftp_upload_creds.yaml") ftpServer = uploadConfig["ftpServer"] ftpPort = uploadConfig["port"] ftpUser = uploadConfig["user"] ftpPassword = uploadConfig["password"] ftpUploadDir = uploadConfig["uploadDir"] cnopts = pysftp.CnOpts() cnopts.hostkeys = None try: sftp = open(pysftp.Connection(host=ftpServer, username=ftpUser, password=ftpPassword, port=ftpPort, cnopts=cnopts)) except: log_action("ERROR", "Failed to establish %s connection to server %s" % ("SFTP", ftpServer), "SFTP_upload") raise # change to appropriate directory for server try: sftp.cwd(ftpUploadDir) except: log_action("ERROR", "Failed to change directory to %s on %s" %(ftpUploadDir, ftpServer), "SFTP_upload") raise return sftp
sftp.get(f) except: log_action("ERROR", "Error downloading file " + f, "SFTP_download") raise else: os.rename(f, gateway + "_" + f) else: log_action("ERROR", "File %s not found on server %s" % (f, gatewayServer), "SFTP_download") except: log_action("ERROR", "Failed to establish %s connection to server %s" % ("SFTP", gatewayServer), "SFTP_download") raise return if __name__ == "__main__": yamlConfig = load_config(CONFIG_DIR + "download_settings.yaml") sftpGateways = list(yamlConfig["SFTP"]) for gateway in sftpGateways: if yamlConfig["SFTP"][gateway]["active"] == "True": files = construct_filename(yamlConfig, gateway, "SFTP") localDir = <YOUR_REPORTS_DIR> + gateway + "/" # change working directory to per gateway Dir if not os.path.isdir(localDir): os.makedirs(localDir) os.chdir(localDir) get_files_using_pysftp(gateway, yamlConfig, files)
os.remove(localFileName) else: log_action("ERROR", "Invalid attachment file %s received from %s" % (localFileName, gateway), "Email") else: log_action("INFO","is already uploaded" % uploadedFileName, "Email") else: print("Invalid mailboxFilename") # WebMoney sends mail to one email address only, so we fwd those mails from here to whoever needs them: if gateway == "WebMoney": toAddress = <OTHER_RCPT> forward_email(mailboxConfig, toAddress, msg) else: log_action("ERROR", "Not a Multipart message") return if __name__ == "__main__": gatewayConfig = load_config(CONFIG_DIR + "download_settings.yaml") mailboxConfig = load_config(CONFIG_DIR +"email-creds.yaml") emailGateways = list_email_gateways(gatewayConfig) conn = connect_IMAP(mailboxConfig) for gw in emailGateways: localDir = create_local_dirs_if_missing(gw) os.chdir(localDir) parse_emails(conn, gw, gatewayConfig, mailboxConfig) conn.logout()
sftp.get(f) except: log_action("ERROR", "Error downloading file " + f, "SFTP_download") raise else: os.rename(f, gateway + "_" + f) else: log_action("ERROR", "File %s not found on server %s" % (f, gatewayServer), "SFTP_download") except: log_action("ERROR", "Failed to establish %s connection to server %s" % ("SFTP", gatewayServer), "SFTP_download") raise return if __name__ == "__main__": yamlConfig = load_config("download_settings.yaml") sftpGateways = list(yamlConfig["SFTP"]) for gateway in sftpGateways: if yamlConfig["SFTP"][gateway]["active"] == "True": files = construct_filename(yamlConfig, gateway, "SFTP") localDir = <YOUR_REPORTS_DIR> + gateway + "/" # change working directory to per gateway Dir if not os.path.isdir(localDir): os.makedirs(localDir) os.chdir(localDir) get_files_using_pysftp(gateway, yamlConfig, files)
os.remove(localFileName) else: log_action("ERROR", "Invalid attachment file %s received from %s" % (localFileName, gateway), "Email") else: log_action("INFO","is already uploaded" % uploadedFileName, "Email") else: print("Invalid mailboxFilename") # WebMoney sends mail to one email address only, so we fwd those mails from here to whoever needs them: if gateway == "WebMoney": toAddress = <OTHER_RCPT> forward_email(mailboxConfig, toAddress, msg) else: log_action("ERROR", "Not a Multipart message") return if __name__ == "__main__": gatewayConfig = load_config("download_settings.yaml") mailboxConfig = load_config("email-creds.yaml") emailGateways = list_email_gateways(gatewayConfig) conn = connect_IMAP(mailboxConfig) for gw in emailGateways: localDir = create_local_dirs_if_missing(gw) os.chdir(localDir) parse_emails(conn, gw, gatewayConfig, mailboxConfig) conn.logout()