2016-04-20 18:06:12 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
|
|
|
|
2016-06-29 12:41:31 +03:00
|
|
|
from base.util.util import Util
|
|
|
|
from base.system.system import System
|
2016-04-20 18:06:12 +03:00
|
|
|
from base.Scope import Scope
|
2016-05-06 16:36:50 +03:00
|
|
|
import paramiko
|
2016-04-20 18:06:12 +03:00
|
|
|
|
|
|
|
|
2016-06-29 12:41:31 +03:00
|
|
|
class Ssh(object):
|
|
|
|
def __init__(self, parameter_map):
|
2016-04-20 18:06:12 +03:00
|
|
|
|
|
|
|
scope = Scope().getInstance()
|
|
|
|
self.logger = scope.getLogger()
|
|
|
|
self.configuration_manager = scope.getConfigurationManager()
|
|
|
|
|
2016-06-29 12:41:31 +03:00
|
|
|
try:
|
2016-06-30 12:40:29 +03:00
|
|
|
self.target_hostname = parameter_map['host']
|
2016-07-01 16:58:20 +03:00
|
|
|
self.target_port = parameter_map['port']
|
2016-06-29 12:41:31 +03:00
|
|
|
self.target_username = parameter_map['username']
|
|
|
|
self.target_path = parameter_map['path']
|
|
|
|
self.target_password = None
|
|
|
|
self.p_key = None
|
2016-07-01 16:58:20 +03:00
|
|
|
if Util.has_attr_json(parameter_map, 'password'):
|
2016-06-29 12:41:31 +03:00
|
|
|
self.target_password = parameter_map['password']
|
|
|
|
else:
|
|
|
|
self.p_key = parameter_map['pkey']
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error('[Ssh] A problem occurred while parsing ssh connection parameters. Error Message: {}'.format(str(e)))
|
2016-04-20 18:06:12 +03:00
|
|
|
|
2016-06-29 12:41:31 +03:00
|
|
|
self.connection = None
|
2016-07-01 16:58:20 +03:00
|
|
|
self.logger.debug('[Ssh] Parameters set up')
|
2016-04-20 18:06:12 +03:00
|
|
|
|
2016-07-01 16:58:20 +03:00
|
|
|
def send_file(self, local_path, md5):
|
2016-04-20 18:06:12 +03:00
|
|
|
|
2016-07-01 16:58:20 +03:00
|
|
|
self.logger.debug('[Ssh] {0} is sending to {1}'.format(local_path, self.target_path + md5))
|
2016-04-20 18:06:12 +03:00
|
|
|
try:
|
|
|
|
sftp = paramiko.SFTPClient.from_transport(self.connection)
|
2016-07-01 16:58:20 +03:00
|
|
|
try:
|
|
|
|
sftp.chdir(self.target_path) # Test if remote_path exists
|
|
|
|
except IOError:
|
2016-07-13 15:56:42 +03:00
|
|
|
print(self.target_path)
|
|
|
|
sftp.mkdir(self.target_path) # Create remote_path
|
2016-07-01 16:58:20 +03:00
|
|
|
sftp.chdir(self.target_path)
|
|
|
|
|
|
|
|
sftp.put(local_path, self.target_path + md5)
|
|
|
|
self.logger.debug('[Ssh] File was sent to {0} from {1}'.format(local_path, self.target_path))
|
|
|
|
return True
|
2016-04-20 18:06:12 +03:00
|
|
|
except Exception as e:
|
2016-07-01 16:58:20 +03:00
|
|
|
self.logger.error('[Ssh] A problem occurred while sending file. Exception message: {}'.format(str(e)))
|
|
|
|
return False
|
2016-04-20 18:06:12 +03:00
|
|
|
|
2016-06-29 12:41:31 +03:00
|
|
|
def get_file(self):
|
2016-07-01 16:58:20 +03:00
|
|
|
self.logger.debug('[Ssh] Getting file ...')
|
2016-06-29 12:41:31 +03:00
|
|
|
file_md5 = None
|
2016-04-20 18:06:12 +03:00
|
|
|
try:
|
2016-06-29 12:41:31 +03:00
|
|
|
tmp_file_name = str(Util.generate_uuid())
|
|
|
|
local_full_path = System.Ahenk.received_dir_path() + tmp_file_name
|
2016-04-20 18:06:12 +03:00
|
|
|
sftp = paramiko.SFTPClient.from_transport(self.connection)
|
2016-06-29 12:41:31 +03:00
|
|
|
sftp.get(self.target_path, local_full_path)
|
|
|
|
file_md5 = str(Util.get_md5_file(local_full_path))
|
|
|
|
Util.rename_file(local_full_path, System.Ahenk.received_dir_path() + file_md5)
|
2016-07-01 16:58:20 +03:00
|
|
|
self.logger.debug('[Ssh] File was downloaded to {0} from {1}'.format(local_full_path, self.target_path))
|
2016-04-20 18:06:12 +03:00
|
|
|
except Exception as e:
|
2016-07-01 16:58:20 +03:00
|
|
|
self.logger.error('[Ssh] A problem occurred while downloading file. Exception message: {}'.format(str(e)))
|
2016-05-17 12:29:51 +03:00
|
|
|
raise
|
2016-06-29 12:41:31 +03:00
|
|
|
return file_md5
|
2016-04-20 18:06:12 +03:00
|
|
|
|
|
|
|
def connect(self):
|
2016-07-01 16:58:20 +03:00
|
|
|
self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.target_port))
|
2016-04-20 18:06:12 +03:00
|
|
|
try:
|
2016-07-01 16:58:20 +03:00
|
|
|
connection = paramiko.Transport(self.target_hostname, int(self.target_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)))
|
2016-06-29 12:41:31 +03:00
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
self.connection.close()
|
|
|
|
self.logger.debug('[FileTransfer] Connection is closed.')
|
|
|
|
# TODO
|
|
|
|
pass
|
|
|
|
|
|
|
|
def is_connected(self):
|
|
|
|
# TODO
|
|
|
|
pass
|