def clear(self): try: self._sftp.close() except Exception: logger.exception("Could not close sftp connection") try: self._ssh.close() except Exception: logger.exception("Could not close ssh connection")
def clear(self): try: self._sftp.close() except Exception: logger.exception("Could not close sftp connection") try: self._ssh.close() except Exception: logger.exception("Could not close ssh connection")
def close(self): """Close SSH and SFTP sessions""" with self.lock: # noinspection PyBroadException try: self.__ssh.close() self.__sftp = None except Exception: logger.exception("Could not close ssh connection") if self.__sftp is not None: # noinspection PyBroadException try: self.__sftp.close() except Exception: logger.exception("Could not close sftp connection")
def close(self): """Close SSH and SFTP sessions""" with self.lock: # noinspection PyBroadException try: self.__ssh.close() self.__sftp = None except Exception: logger.exception("Could not close ssh connection") if self.__sftp is not None: # noinspection PyBroadException try: self.__sftp.close() except Exception: logger.exception("Could not close sftp connection")
def connect(self, client, hostname=None, port=22, log=True): """Connect SSH client object using credentials :type client: paramiko.client.SSHClient paramiko.transport.Transport :type log: bool :raises paramiko.AuthenticationException """ kwargs = { 'username': self.username, 'password': self.__password} if hostname is not None: kwargs['hostname'] = hostname kwargs['port'] = port keys = [self.__key] keys.extend([k for k in self.__keys if k != self.__key]) for key in keys: kwargs['pkey'] = key try: client.connect(**kwargs) if self.__key != key: self.__key = key logger.debug( 'Main key has been updated, public key is: \n' '{}'.format(self.public_key)) return except paramiko.PasswordRequiredException: if self.__password is None: logger.exception('No password has been set!') raise else: logger.critical( 'Unexpected PasswordRequiredException, ' 'when password is set!') raise except (paramiko.AuthenticationException, paramiko.BadHostKeyException): continue msg = 'Connection using stored authentication info failed!' if log: logger.exception( 'Connection using stored authentication info failed!') raise paramiko.AuthenticationException(msg)
def connect(self, client, hostname=None, port=22, log=True): """Connect SSH client object using credentials :type client: paramiko.client.SSHClient paramiko.transport.Transport :type log: bool :raises paramiko.AuthenticationException """ kwargs = { 'username': self.username, 'password': self.__password} if hostname is not None: kwargs['hostname'] = hostname kwargs['port'] = port keys = [self.__key] keys.extend([k for k in self.__keys if k != self.__key]) for key in keys: kwargs['pkey'] = key try: client.connect(**kwargs) if self.__key != key: self.__key = key logger.debug( 'Main key has been updated, public key is: \n' '{}'.format(self.public_key)) return except paramiko.PasswordRequiredException: if self.__password is None: logger.exception('No password has been set!') raise else: logger.critical( 'Unexpected PasswordRequiredException, ' 'when password is set!') raise except (paramiko.AuthenticationException, paramiko.BadHostKeyException): continue msg = 'Connection using stored authentication info failed!' if log: logger.exception( 'Connection using stored authentication info failed!') raise paramiko.AuthenticationException(msg)
def __deserialize(self, fmt): """Deserialize stdout as data format :type fmt: str :rtype: object :raises: DevopsError """ try: if fmt == 'json': return json.loads(self.stdout_str, encoding='utf-8') elif fmt == 'yaml': return yaml.safe_load(self.stdout_str) except BaseException: tmpl = ("'{cmd}' stdout is not valid {fmt}:\n" '{{stdout!r}}\n'.format(cmd=self.cmd, fmt=fmt)) logger.exception(tmpl.format(stdout=self.stdout_str)) raise error.DevopsError(tmpl.format(stdout=self.stdout_brief)) msg = '{fmt} deserialize target is not implemented'.format(fmt=fmt) logger.error(msg) raise error.DevopsNotImplementedError(msg)