2016-04-20 18:06:12 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
|
|
|
|
|
|
|
from base.Scope import Scope
|
2016-05-06 16:36:50 +03:00
|
|
|
import paramiko
|
2016-04-20 18:06:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
class FileTransfer(object):
|
|
|
|
def __init__(self, hostname, port, username, password, pkey=None):
|
|
|
|
|
|
|
|
scope = Scope().getInstance()
|
|
|
|
self.logger = scope.getLogger()
|
|
|
|
self.configuration_manager = scope.getConfigurationManager()
|
|
|
|
|
|
|
|
self.target_hostname = hostname
|
|
|
|
self.port = port
|
|
|
|
self.target_username = username
|
|
|
|
self.target_password = password
|
|
|
|
self.p_key = pkey
|
|
|
|
self.connection = None
|
|
|
|
|
|
|
|
self.logger.debug('[FileTransfer] Parameters set up')
|
|
|
|
|
|
|
|
def send_file(self, local_path, remote_path):
|
|
|
|
self.logger.debug('[FileTransfer] Sending file ...')
|
|
|
|
|
|
|
|
try:
|
|
|
|
sftp = paramiko.SFTPClient.from_transport(self.connection)
|
|
|
|
sftp.put(local_path, remote_path)
|
2016-05-06 16:36:50 +03:00
|
|
|
self.logger.debug('[FileTransfer] File was sent to {} from {}'.format(local_path, remote_path))
|
2016-04-20 18:06:12 +03:00
|
|
|
except Exception as e:
|
2016-05-06 16:36:50 +03:00
|
|
|
self.logger.error('[FileTransfer] A problem occurred while sending file. Exception message: {}'.format(str(e)))
|
2016-04-20 18:06:12 +03:00
|
|
|
finally:
|
|
|
|
self.connection.close()
|
|
|
|
self.logger.debug('[FileTransfer] Connection is closed successfully')
|
|
|
|
|
|
|
|
def get_file(self, local_path, remote_path):
|
|
|
|
self.logger.debug('[FileTransfer] Getting file ...')
|
|
|
|
try:
|
|
|
|
sftp = paramiko.SFTPClient.from_transport(self.connection)
|
|
|
|
sftp.get(remote_path, local_path)
|
2016-05-06 16:36:50 +03:00
|
|
|
self.logger.debug('[FileTransfer] File was downloaded to {} from {}'.format(local_path, remote_path))
|
2016-04-20 18:06:12 +03:00
|
|
|
except Exception as e:
|
2016-05-06 16:36:50 +03:00
|
|
|
self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: {}'.format(str(e)))
|
2016-05-17 12:29:51 +03:00
|
|
|
raise
|
2016-04-20 18:06:12 +03:00
|
|
|
finally:
|
|
|
|
self.connection.close()
|
2016-05-17 12:29:51 +03:00
|
|
|
self.logger.debug('[FileTransfer] Connection is closed successfully')
|
2016-04-20 18:06:12 +03:00
|
|
|
|
|
|
|
def connect(self):
|
2016-05-06 16:36:50 +03:00
|
|
|
self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.port))
|
2016-04-20 18:06:12 +03:00
|
|
|
try:
|
2016-05-06 16:36:50 +03:00
|
|
|
connection = paramiko.Transport((self.target_hostname, int(self.port)))
|
2016-04-20 18:06:12 +03:00
|
|
|
connection.connect(username=self.target_username, password=self.target_password, pkey=self.p_key)
|
|
|
|
self.connection = connection
|
|
|
|
self.logger.debug('[FileTransfer] Connected.')
|
|
|
|
except Exception as e:
|
2016-05-06 16:36:50 +03:00
|
|
|
self.logger.error('[FileTransfer] A problem occurred while connecting to {} . Exception message: {}'.format(self.target_hostname, str(e)))
|