Ejemplo n.º 1
0
    def do_exec(self, command_str):
        """Execute command"""
        re = None
        try:
            if command_str is not None:
                self.connect()
                re = self.exec_command(command_str)
        except paramiko.AuthenticationException as ae:
            LOG.error('doexec Authentication error:{}'.format(ae))
            raise exception.InvalidUsernameOrPassword()
        except Exception as e:
            LOG.error('doexec InvalidUsernameOrPassword error:{}'.format(e))
            if 'WSAETIMEDOUT' in str(e):
                raise exception.SSHConnectTimeout()
            elif 'No authentication methods available' in str(e) \
                    or 'Authentication failed' in str(e):
                raise exception.SSHInvalidUsernameOrPassword()
            elif 'not a valid RSA private key file' in str(e):
                raise exception.InvalidPrivateKey()
            elif 'not found in known_hosts' in str(e):
                raise exception.SSHNotFoundKnownHosts(self.ssh_host)
            else:
                raise exception.SSHException()

        finally:
            self.close()
        return re
Ejemplo n.º 2
0
    def create(self):
        ssh = paramiko.SSHClient()
        try:
            if self.ssh_pub_key is None:
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            else:
                host_key = '%s %s %s' % \
                           (self.ssh_host, self.ssh_pub_key_type,
                            self.ssh_pub_key)
                self.set_host_key(host_key, ssh)

            ssh.connect(hostname=self.ssh_host, port=self.ssh_port,
                        username=self.ssh_username,
                        password=cryptor.decode(self.ssh_password),
                        timeout=self.ssh_conn_timeout)
            if self.conn_timeout:
                transport = ssh.get_transport()
                transport.set_keepalive(self.SOCKET_TIMEOUT)
            return ssh
        except Exception as e:
            err = six.text_type(e)
            LOG.error('doexec InvalidUsernameOrPassword error')
            if 'timed out' in err:
                raise exception.InvalidIpOrPort()
            elif 'No authentication methods available' in err \
                    or 'Authentication failed' in err:
                raise exception.InvalidUsernameOrPassword()
            elif 'not a valid RSA private key file' in err:
                raise exception.InvalidPrivateKey()
            elif 'not found in known_hosts' in err:
                raise exception.SSHNotFoundKnownHosts(self.ssh_host)
            else:
                raise exception.SSHException(err)