file transfer via ssh

This commit is contained in:
Volkan Şahin 2016-05-06 16:36:50 +03:00
parent a2676abea4
commit 46fbff6144

View file

@ -2,9 +2,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com> # Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import paramiko
from base.Scope import Scope from base.Scope import Scope
import paramiko
class FileTransfer(object): class FileTransfer(object):
@ -29,9 +28,9 @@ class FileTransfer(object):
try: try:
sftp = paramiko.SFTPClient.from_transport(self.connection) sftp = paramiko.SFTPClient.from_transport(self.connection)
sftp.put(local_path, remote_path) sftp.put(local_path, remote_path)
self.logger.debug('[FileTransfer] File was sent to %s from %s'.format(local_path,remote_path)) self.logger.debug('[FileTransfer] File was sent to {} from {}'.format(local_path, remote_path))
except Exception as e: except Exception as e:
self.logger.error('[FileTransfer] A problem occurred while sending file. Exception message: %s'.format(str(e))) self.logger.error('[FileTransfer] A problem occurred while sending file. Exception message: {}'.format(str(e)))
finally: finally:
self.connection.close() self.connection.close()
self.logger.debug('[FileTransfer] Connection is closed successfully') self.logger.debug('[FileTransfer] Connection is closed successfully')
@ -41,20 +40,19 @@ class FileTransfer(object):
try: try:
sftp = paramiko.SFTPClient.from_transport(self.connection) sftp = paramiko.SFTPClient.from_transport(self.connection)
sftp.get(remote_path, local_path) sftp.get(remote_path, local_path)
self.logger.debug('[FileTransfer] File was downloaded to %s from %s'.format(local_path,remote_path)) self.logger.debug('[FileTransfer] File was downloaded to {} from {}'.format(local_path, remote_path))
except Exception as e: except Exception as e:
self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: %s'.format(str(e))) self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: {}'.format(str(e)))
finally: finally:
self.connection.close() self.connection.close()
self.logger.debug('[FileTransfer] Connection is closed successfully') self.logger.debug('[FileTransfer] Connection is closed successfully')
def connect(self): def connect(self):
self.logger.debug('[FileTransfer] Connecting to %s via %d'.format(self.target_hostname,self.port)) self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.port))
try: try:
connection = paramiko.Transport((self.target_hostname, self.port)) connection = paramiko.Transport((self.target_hostname, int(self.port)))
connection.connect(username=self.target_username, password=self.target_password, pkey=self.p_key) connection.connect(username=self.target_username, password=self.target_password, pkey=self.p_key)
self.connection = connection self.connection = connection
self.logger.debug('[FileTransfer] Connected.') self.logger.debug('[FileTransfer] Connected.')
except Exception as e: except Exception as e:
self.logger.error('[FileTransfer] A problem occurred while connecting to %s . Exception message: %s'.format(self.target_hostname, str(e))) self.logger.error('[FileTransfer] A problem occurred while connecting to {} . Exception message: {}'.format(self.target_hostname, str(e)))