Issue with PYSFTP - Working in WINSCP
NAND KISHORE
kthakur.nand at gmail.com
Tue Mar 10 21:20:38 EDT 2020
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
-------------- next part --------------
#!/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_receiver at gmail.com"
sender_email = "test_sender at 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 = paths[1]
localfuspath1 = paths[2]
localpath3 = paths[3]
ebsArchivePath = os.path.join(ebsBasePath, 'archive')
localfusaropath1 = os.path.join(localpath3, 'aro')
localfusowopath1 = os.path.join(localpath3, 'owo')
localfusitempath1 = os.path.join(localpath3, 'item')
localfuspath2 = os.path.join(localfuspath1, '/')
otaropath1 = paths[5]
otowopath1 = paths[4]
otitempath1 = paths[6]
print ('client connection')
print (sleep)
print ('vendor connection ')
print (vendor)
print (localfuspath1, ' for SFTP ebs base path', ebsBasePath,' Sleep Connection ',sleep,' Vendor Connection ', vendor )
write_to_file('F', '*****************************************************************************')
write_to_file('F','Start of File Transfer Program')
write_to_file('F', '*****************************************************************************')
write_to_file('F', ' Directory Path Reference')
write_to_file('F','EBS Path for Outbound ' + ebsBasePath)
write_to_file('F','EBS Archive Path for Outbound ' + ebsArchivePath)
write_to_file('F', 'Server Path where file will be written from EBS ' + localfuspath1)
write_to_file('F','Server Path where file ARO OWO QGO File will be copied ' + localpath3)
write_to_file('F', 'Server Path for ARO ' + localfusaropath1)
write_to_file('F', 'Server Path for QGO ' + localfusitempath1)
write_to_file('F', 'Server Path for OWO ' + localfusowopath1)
write_to_file('F', 'Client Path for ARO ' + otaropath1)
write_to_file('F', 'Client Path for QGO ' + otitempath1)
write_to_file('F', 'Client Path for OWO ' + otowopath1)
write_to_file('F', '*****************************************************************************')
source_dir = localfuspath1 + "/"
print("source dir " + source_dir)
if ebsBasePath != 'UNKNOWN':
try:
#Initialize the directory by deleting the content
write_to_file('F','Initialize the directory by deleting the content')
print(' Content of directory ')
print(os.listdir(localfuspath1))
file_delete(localfuspath1)
file_delete(localfusaropath1)
file_delete(localfusowopath1)
file_delete(localfusitempath1)
write_to_file('F','End of Intialization Process ')
write_to_file('F', '*****************************************************************************')
write_to_file('F', 'Start of SFTP Process ')
sleep.get_d(ebsBasePath, localfuspath1, preserve_mtime=True)
# depending on file type move to various location
for file in os.listdir(localfuspath1):
src = os.path.join(source_dir, file)
if file.startswith("ECE_ARO"):
dst_dir=localfusaropath1 + "/"
dst=os.path.join(dst_dir,file)
print("ECE_ARO File--> "+file)
shutil.copyfile(src,dst)
if file.startswith("ECE_OWO"):
dst_dir = localfusowopath1 + "/"
dst = os.path.join(dst_dir, file)
print("ECE_OWO File--> "+file)
shutil.copyfile(src, dst)
if file.startswith("ECE_QGO"):
dst_dir = localfusitempath1 + "/"
dst = os.path.join(dst_dir, file)
print("ECE_QGO File--> "+file)
shutil.copyfile(src, dst)
sleep.put_d(localfuspath1, ebsArchivePath, preserve_mtime=True)
write_to_file('F','File is successfully Copied To Client Local Folder and Archive Location')
#print('File is successfully copied to Client')
except Exception:
# remote Archive
sendEmail('E')
logging.exception('Exception in sending files to eBS')
write_to_file('F','Exception in sending files to eBS')
try:
write_to_file('F'," File Contents for ARO "+localfusaropath1)
# Switch to a remote directory
vendor.cwd(otowopath1)
# Obtain structure of the remote directory '/var/www/vhosts'
directory_structure = vendor.listdir_attr()
# Print data
print("attributes")
for attr in directory_structure:
print ( attr.filename, attr )
for file in os.listdir(localfusaropath1):
write_to_file('F',file)
src_dir = localfusaropath1 + "/"
src_file = os.path.join(src_dir, file)
dst_dir = otaropath1 + "/"
dst_file = os.path.join(dst_dir, file)
print( " src and dst file ")
print(src_file)
print(dst_dir )
#vendor.cd(dst_dir)
#vendor.put(src_file)
#vendor.put(src_file,dst_file)
write_to_file('F'," ******************************* ")
write_to_file('F'," File Contents for OWO "+localfusowopath1)
for file in os.listdir(localfusowopath1):
write_to_file('F',file)
src_dir = localfusowopath1 + "/"
src_file = os.path.join(src_dir, file)
dst_dir = otowopath1 + "/"
dst_file = os.path.join(dst_dir, file)
vendor.put(src_file, dst_file)
write_to_file('F'," ******************************* ")
write_to_file('F'," File Contents for QGO "+localfusitempath1)
for file in os.listdir(localfusitempath1):
write_to_file('F',file)
src_dir = localfusitempath1 + "/"
src_file = os.path.join(src_dir, file)
dst_dir = otitempath1 + "/"
dst_file = os.path.join(dst_dir, file)
vendor.put(src_file, dst_file)
write_to_file('F'," ******************************* ")
print('Server Path for ARO ' + localfusaropath1)
print('Server Path for QGO ' + localfusitempath1)
print('Server Path for OWO ' + localfusowopath1)
print('Client Path for ARO ' + otaropath1)
print('Client Path for QGO ' + otitempath1)
print('Client Path for OWO ' + otowopath1)
#vendor.put_d(localfusaropath1, otaropath1, preserve_mtime=True)
#vendor.put_d(localfusaropath1,otaropath1, preserve_mtime=True)
#vendor.put_d(localfusowopath1, otowopath1, preserve_mtime=True)
print ('File is successfully Transferred to open Text')
write_to_file('F','File is successfully Transferred to open Text')
vendor.close()
sleep.close()
except Exception:
logging.exception('Error in getting the file from EBS Outbound Server')
sendEmail('E')
write_to_file('F','Error in getting the file from EBS Outbound Server')
def create_connection( osource, oshost, vusername, vpassword):
global sl_cinfo
global ot_cinfo
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# oshost='10.7.51.60'
write_to_file('F','Start of create_connection Program')
write_to_file('F','Source ' + osource)
print("source " +osource)
ebs_cinfo = pysftp.Connection(host=oshost, username=vusername,
password=vpassword, cnopts=cnopts)
print("Able to establish the connection")
write_to_file('F','Able to establish the connection')
if osource == 'CLIENT':
sl_cinfo = ebs_cinfo
if osource == 'VENDOR':
ot_cinfo = ebs_cinfo
if ebssource == 'CLIENT':
try:
vault = '/kv/opentext/tst2'
ebs_username = client.read(vault)['data']['ebs_username'].strip()
ebs_password = client.read(vault)['data']['ebs_password'].strip()
ebshost = client.read(vault)['data']['ebs_host'].strip()
ebshost = 'ebststapp11'
ebspath1 = client.read(vault)['data']['ebs_path'].strip()
create_connection(ebssource, ebshost, ebs_username, ebs_password)
print ('Worked Fine ', ebssource)
write_to_file('F',ebssource)
write_to_file('F','Connection Established')
except:
print ('Error in establishing the connection ', ebssource)
write_to_file('F',ebssource)
write_to_file('F','Connection Failed')
sendEmail('E')
if otsource == 'VENDOR':
try:
vault = '/kv/opentext/tst2'
ot_username = client.read(vault)['data']['ot_username'].strip()
ot_password = client.read(vault)['data']['ot_password'].strip()
othost = client.read(vault)['data']['ot_host'].strip()
ot_owo_path = client.read(vault)['data']['ot_owo_path'].strip()
ot_aro_path = client.read(vault)['data']['ot_aro_path'].strip()
ot_qgo_path = client.read(vault)['data']['ot_qgo_path'].strip()
otpath1 = client.read(vault)['data']['ot_owo_path'].strip()
create_connection(otsource, othost, ot_username,ot_password)
print('Connection Established ',otsource)
write_to_file('F',otsource)
write_to_file('F','Connection Established')
except:
print ('Error in establishing the connection ', otsource)
write_to_file('F',otsource)
write_to_file('F','Error in Connection Established')
sendEmail('E')
paths = [ebspath1, otpath1,localfuspath,localfuspath3,ot_owo_path,ot_aro_path,ot_qgo_path]
file_transfer(sl_cinfo, ot_cinfo, paths)
sendEmail('S')
write_to_file('F',"Email has been sent ")
print ("Email has been sent ")
timing=datetime.now()
write_to_file('F',str(timing))
write_to_file('F','the logging has ended')
write_to_file('F',"-------------------------------------------------------------------------------------------------------------")
More information about the Python-list
mailing list