Issue with PYSFTP - Working in WINSCP
Hi We have requirement where we need to get file from client path and then upload the same to vendor directory path. I am not able to upload the file to vendor directory path , however when I tried to use the WINSCP it worked fine. So I thought of checking with Gurus what is wrong I am doing in my script. Appreciate your input. I will attach my script. Here is what I am doing. Step1. Clear the client directory path Step2. Make a call to HVAC Vault to get the username and password for client and vendor server Step3. Use the username and password to establish connection using pysftp for client. Step4. Store the file in local path. Step5. Segregate the file into different path based on file type Step6 Establish a connection to vendor and copy the file to vendor. Step7 Close the client and Vendor connection Please see the file attached. Also below is the error which I am getting - ERROR:root:Error in getting the file from EBS Outbound Server Traceback (most recent call last): File "FILE_TRANSFER_PROCESS.py", line 191, in file_transfer vendor.put(src_file, dst_file) File "/d01/python3/lib64/python3.6/site-packages/pysftp/__init__.py", line 364, in put confirm=confirm) File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 759, in put return self.putfo(fl, remotepath, file_size, callback, confirm) File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 720, in putfo s = self.stat(remotepath) File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 493, in stat t, msg = self._request(CMD_STAT, path) File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 813, in _request return self._read_response(num) File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 865, in _read_response self._convert_status(msg) File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 894, in _convert_status raise IOError(errno.ENOENT, text) FileNotFoundError: [Errno 2] /custom/OWO/ECE_OWO_20200303_143895.dat #!/usr/bin/python # -*- coding: utf-8 -*- import csv # from pysftp import Connection, CnOpts import os import sys import pysftp import logging import hvac import datetime from datetime import datetime import ssl import smtplib import shutil from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders def file_delete (dirs): write_to_file('F', 'The directory is '+dirs) for files in os.listdir(dirs): filed = dirs + "/" +files write_to_file('F','File is to be deleted '+filed) os.remove(filed) def write_to_file(mode,filetext): log_path = "/home/kishnx/scripts/log/sftp.log" if mode == 'I': f = open(log_path,"w") else: f = open(log_path,"a") f.write(filetext) f.write("\n") f.close() def sendEmail(v_status): port = 587 # For starttls smtp_server = "smtp.gmail.com" receiver_email = "test_recei...@gmail.com" sender_email = "test_sen...@gmail.com" password = "Test1234" if v_status == 'S': message = """\ GXS_OUTBOUND Ran the Python SFTP program.""" elif v_status == 'E': message = """\ GXS_OUTBOUND Issue with the Python SFTP program.Check Log Files.""" context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.ehlo() # Can be omitted server.starttls(context=context) server.ehlo() # Can be omitted server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) timing=datetime.now() fcontent1 = 'Logging for today has started' write_to_file('I',"-") write_to_file('F',fcontent1) write_to_file('F',str(timing)) sl_cinfo = None ot_cinfo = None otsource = 'VENDOR' ebspath1 = 'UNKNOWN' otpath1 = 'UNKNOWN' ebssource = 'CLIENT' localfuspath = '/home/test/scripts/outbound/scm' localfuspath3 = '/home/test/scripts/outbound/outbound' shutil.rmtree(localfuspath) os.makedirs(localfuspath) vaultfile = open('/home/test/.vaulttoken', 'r') vault_token = vaultfile.read().strip() client = hvac.Client() client = hvac.Client(url='https://vault.comfort.com:8200', token=vault_token, verify='/home/test/vault-cert.crt') if client.is_authenticated(): print ('vault is authenticated') write_to_file('F','vault is authenticated') def file_transfer(sleep, vendor, paths): global localfusaropath1 global localfusowopath1 global localfusitempath1 global localfuspath1 # print("paths are ",paths[0],paths[1]) ebsBasePath = paths[0] localBasePath = path
Re: Pysftp Issue File Upload is not working - put command
> > > Hi >We have requirement where we need to get file from client path and then > upload the same to vendor directory path. I am not able to upload the file > to vendor directory path , however when I tried to use the WINSCP it worked > fine. So I thought of checking with Gurus what is wrong I am doing in my > script. Appreciate your input. > I will attach my script. Here is what I am doing. > > Step1. Clear the client directory path > Step2. Make a call to HVAC Vault to get the username and password for > client and vendor server > Step3. Use the username and password to establish connection using pysftp > for client. > Step4. Store the file in local path. > Step5. Segregate the file into different path based on file type > Step6 Establish a connection to vendor and upload the file to vendor > directory path. > Step7 Close the client and Vendor connection > > Note : In step 6 I am getting an error and I am getting below error > Please see the file attached for code. > > Also below is the error which I am getting > > - > > > > > > > > > > > > > > > > > > > > * ERROR:root:Error in getting the file from EBS Outbound Server Traceback > (most recent call last): File "FILE_TRANSFER_PROCESS.py", line 191, in > file_transfer vendor.put(src_file, dst_file) File > "/d01/python3/lib64/python3.6/site-packages/pysftp/__init__.py", line 364, > in put confirm=confirm) File > "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line > 759, in put return self.putfo(fl, remotepath, file_size, callback, > confirm) File > "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line > 720, in putfo s = self.stat(remotepath) File > "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line > 493, in stat t, msg = self._request(CMD_STAT, path) File > "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line > 813, in _request return self._read_response(num) File > "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line > 865, in _read_response self._convert_status(msg) File > "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line > 894, in _convert_status raise IOError(errno.ENOENT, text) > FileNotFoundError: [Errno 2] /custom/OWO/ECE_OWO_20200303_143895.dat* > __ > #!/usr/bin/python # -*- coding: utf-8 -*- import csv # from pysftp import Connection, CnOpts import os import sys import pysftp import logging import hvac import datetime from datetime import datetime import ssl import smtplib import shutil from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders def file_delete (dirs): write_to_file('F', 'The directory is '+dirs) for files in os.listdir(dirs): filed = dirs + "/" +files write_to_file('F','File is to be deleted '+filed) os.remove(filed) def write_to_file(mode,filetext): log_path = "/home/kishnx/scripts/log/sftp.log" if mode == 'I': f = open(log_path,"w") else: f = open(log_path,"a") f.write(filetext) f.write("\n") f.close() def sendEmail(v_status): port = 587 # For starttls smtp_server = "smtp.gmail.com" receiver_email = "test_recei...@gmail.com" sender_email = "test_sen...@gmail.com" password = "Test1234" if v_status == 'S': message = """\ GXS_OUTBOUND Ran the Python SFTP program.""" elif v_status == 'E': message = """\ GXS_OUTBOUND Issue with the Python SFTP program.Check Log Files.""" context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.ehlo() # Can be omitted server.starttls(context=context) server.ehlo() # Can be omitted server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) timing=datetime.now() fcontent1 = 'Logging for today has started' write_to_file('I',"-") write_to_file('F',fcontent1) write_to_file('F',str(timing)) sl_cinfo = None ot_cinfo = None otsource = 'VENDOR' ebspath1 = 'UNKNOWN' otpath1 = 'UNKNOWN' ebssource = 'CLIENT' localfuspath = '/home/test/scripts/outbound/scm' localfuspath3 = '/home/test/scripts/outbound/outbound' shutil.rmtree(localfuspath) os.makedirs(localfuspath) vaultfile = open('/home/test/.vaulttoken', 'r') vault_token = vaultfile.read().strip() client = hvac.Client() client = hvac.Client(url='https://vault.comfort.com:8200', token=vault_token, verify='/home/test/vault-cert.crt') if client.is_authenticated(): print ('vault is authenticated') write_to_file('F','vault is authenticated') def file_t