Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
rsa_key_path = ICPSudo.get_param('saas_server.rsa_key_path', None)
rsa_key_passphrase=ICPSudo.get_param('saas_server.rsa_key_passphrase')
sftp_public_key=ICPSudo.get_param('saas_server.sftp_public_key')
params = {
"host": server,
"username": username,
}
if rsa_key_path:
params["private_key"] = self.rsa_key_path
if rsa_key_passphrase:
params["private_key_pass"] = rsa_key_passphrase
else:
params["password"] = password
cnopts = pysftp.CnOpts()
if sftp_public_key:
key = paramiko.RSAKey(data=base64.b64decode(sftp_public_key))
cnopts.hostkeys.add(server, 'ssh-rsa', key)
else:
cnopts.hostkeys = None
with pysftp.Connection(**params, cnopts=cnopts) as sftp:
# set keepalive to prevent socket closed / connection dropped error
sftp._transport.set_keepalive(30)
try:
sftp.chdir(path)
except IOError:
# Create directory and subdirs if they do not exist.
currentDir = ''
for dirElement in path.split('/'):
makedirs_safe(parse_path(file_out))
with open(file_out, 'w') as save_file:
save_file.write(rows_to_csv(rows).read())
if 'storage' in destination and destination['storage'].get('bucket') and destination['storage'].get('path'):
# create the bucket
bucket_create(auth, project.id, destination['storage']['bucket'])
# put the file
file_out = destination['storage']['bucket'] + ':' + destination['storage']['path'] + variant
if project.verbose: print('SAVING', file_out)
object_put(auth, file_out, rows_to_csv(rows))
if 'sftp' in destination:
try:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
path_out, file_out = destination['sftp']['file'].rsplit('.', 1)
file_out = path_out + variant + file_out
sftp = pysftp.Connection(host=destination['sftp']['host'], username=destination['sftp']['username'], password=destination['sftp']['password'], port=destination['sftp']['port'], cnopts=cnopts)
if '/' in file_out:
dir_out, file_out = file_out.rsplit('/', 1)
sftp.cwd(dir_out)
sftp.putfo(rows_to_csv(rows), file_out)
except e:
print(str(e))
traceback.print_exc()
def sftp_copy_subject_files(subject, necessary_files, username, domain, local_subjects_dir, remote_subject_dir,
password='', overwrite_files=False, print_traceback=True, port=22):
import pysftp
local_subject_dir = op.join(local_subjects_dir, subject)
if password == '':
password = ask_for_sftp_password(username)
try:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp_con = pysftp.Connection(domain, username=username, password=password, cnopts=cnopts, port=port)
except:
try:
sftp_con = pysftp.Connection(domain, username=username, password=password, port=port)
except:
print("Can't connect via sftp!")
if print_traceback:
print(traceback.format_exc())
return False
with sftp_con as sftp:
for fol, files in necessary_files.items():
fol = fol.replace(':', op.sep)
if not op.isdir(op.join(local_subject_dir, fol)):
os.makedirs(op.join(local_subject_dir, fol))
os.chdir(op.join(local_subject_dir, fol))
def get_file_sftp(host, path_to_file, sftp_configuration):
"""
Copy an existing file from SFTP via sftp://*host*/*path_to_file* link to a home directory.
The function return the full path to the file that has been downloaded.
"""
# disable host key checking
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# construct SFTP object and get the file on a server
with pysftp.Connection(host, username = sftp_configuration["user"], password = sftp_configuration["psswd"],
cnopts = cnopts) as sftp:
sftp.get(path_to_file)
file_location = gc_write_dir + "/" + re.findall("[^/]*$", path_to_file)[0]
print("File " + path_to_file + " has got successfully.")
return file_location
def connect(self):
""" Establish connection with the server """
if (self.connection_flag == False):
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp = pysftp.Connection(self.mySFTP_config.URL, username=self.mySFTP_config.USER,
password=self.mySFTP_config.PASSWORD, cnopts = cnopts)
self.connection_flag = True
self.sftp = sftp
self.sftp_ask = (
'Please enter your login details for %s\n' % self.host
if self.sftp_ask is None else
self.sftp_ask
)
self.sftp_passwd_file = (
os.path.join('cache', '%s.login' % self.sftp_host)
if self.sftp_passwd_file is None else
self.sftp_passwd_file
)
if self.sftp_user is None:
self.ask_passwd()
while True:
self.sftp_passwd = self.sftp_passwd or None
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(
host = self.sftp_host,
username = self.sftp_user,
password = self.sftp_passwd,
port = self.sftp_port,
cnopts = cnopts
) as con:
try:
con.get(self.sftp_filename, self.cache_file_name)
break
except IOError:
def create_session(hostname, username, password):
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
return pysftp.Connection(hostname, username=username, password=password, cnopts=cnopts)