示例#1
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)
示例#2
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
示例#3
0
 def do_exec(command_str, ssh):
     result = None
     try:
         utils.check_ssh_injection(command_str)
         if command_str is not None and ssh is not None:
             stdin, stdout, stderr = ssh.exec_command(command_str)
             res, err = stdout.read(), stderr.read()
             re = res if res else err
             result = re.decode()
     except paramiko.AuthenticationException as ae:
         LOG.error('doexec Authentication error:{}'.format(ae))
         raise exception.InvalidUsernameOrPassword()
     except Exception as e:
         err = six.text_type(e)
         LOG.error('doexec InvalidUsernameOrPassword error')
         if 'timed out' in err:
             raise exception.SSHConnectTimeout()
         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()
         else:
             raise exception.SSHException(err)
     return result
示例#4
0
文件: utils.py 项目: sfzeng/delfin
 def create(self):  # pylint: disable=method-hidden
     ssh = paramiko.SSHClient()
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     look_for_keys = True
     if self.path_to_private_key:
         self.path_to_private_key = os.path.expanduser(
             self.path_to_private_key)
         look_for_keys = False
     elif self.password:
         look_for_keys = False
     try:
         LOG.debug(
             "ssh.connect: ip: %s, port: %s, username: %s, "
             "password: %s, key_filename: %s, look_for_keys: %s, "
             "timeout: %s, banner_timeout: %s", self.ip, self.port,
             self.login, self.password, self.path_to_private_key,
             look_for_keys, self.conn_timeout, self.conn_timeout)
         ssh.connect(self.ip,
                     port=self.port,
                     username=self.login,
                     password=self.password,
                     key_filename=self.path_to_private_key,
                     look_for_keys=look_for_keys,
                     timeout=self.conn_timeout,
                     banner_timeout=self.conn_timeout)
         if self.conn_timeout:
             transport = ssh.get_transport()
             transport.set_keepalive(self.conn_timeout)
         return ssh
     except Exception as e:
         msg = _("Check whether private key or password are correctly "
                 "set. Error connecting via ssh: %s") % e
         LOG.error(msg)
         raise exception.SSHException(msg)
示例#5
0
 def do_exec(self, command_str):
     result = ''
     try:
         with self.item() as ssh:
             utils.check_ssh_injection(command_str)
             if command_str is not None and ssh is not None:
                 stdin, stdout, stderr = ssh.exec_command(command_str)
                 res, err = stdout.read(), stderr.read()
                 re = res if res else err
                 result = re.decode()
     except paramiko.AuthenticationException as ae:
         LOG.error('doexec Authentication error:{}'.format(ae))
         raise exception.InvalidUsernameOrPassword()
     except Exception as e:
         err = six.text_type(e)
         LOG.error(err)
         if 'timed out' in err \
                 or 'SSH connect timeout' in err\
                 or 'Unable to connect to port' in err:
             raise exception.ConnectTimeout()
         elif 'No authentication methods available' in err \
                 or 'Authentication failed' in err \
                 or 'Invalid username or password' in err:
             raise exception.InvalidUsernameOrPassword()
         elif 'not a valid RSA private key file' in err \
                 or 'not a valid RSA private key' in err:
             raise exception.InvalidPrivateKey()
         else:
             raise exception.SSHException(err)
     if 'invalid command name' in result or 'login failed' in result or\
             'is not a recognized command' in result:
         raise exception.StorageBackendException(result)
     return result
示例#6
0
 def exec_ssh_command(self, command):
     try:
         with self.ssh_pool.item() as ssh:
             ssh_info = NetAppHandler.do_exec(command, ssh)
         return ssh_info
     except Exception as e:
         msg = "Failed to ssh netapp_fas %s: %s" % \
               (command, six.text_type(e))
         raise exception.SSHException(msg)
示例#7
0
 def exec_ssh_command(self, command):
     try:
         with self.ssh_pool.item() as ssh:
             ssh_info = SSHHandler.do_exec(command, ssh)
         return ssh_info
     except Exception as e:
         msg = "Failed to ssh ibm storwize_svc %s: %s" % \
               (command, six.text_type(e))
         raise exception.SSHException(msg)
示例#8
0
 def do_exec_shell(self, command_list, exe_time):
     result = ''
     try:
         with self.item() as ssh:
             if command_list and ssh:
                 channel = ssh.invoke_shell()
                 for command in command_list:
                     utils.check_ssh_injection(command)
                     channel.send(command + '\r\n')
                     time.sleep(exe_time)
                 channel.send("exit" + "\r\n")
                 channel.close()
                 while True:
                     resp = channel.recv(9999).decode('utf8')
                     if not resp:
                         time.sleep(exe_time)
                         break
                     result += resp
         if 'is not a recognized command' in result \
                 or 'Unknown command' in result:
             raise exception.StorageBackendException(result)
     except paramiko.AuthenticationException as ae:
         LOG.error('doexec Authentication error:{}'.format(ae))
         raise exception.InvalidUsernameOrPassword()
     except Exception as e:
         err = six.text_type(e)
         LOG.error(err)
         if 'timed out' in err \
                 or 'SSH connect timeout' in err:
             raise exception.SSHConnectTimeout()
         elif 'No authentication methods available' in err \
                 or 'Authentication failed' in err \
                 or 'Invalid username or password' in err:
             raise exception.InvalidUsernameOrPassword()
         elif 'not a valid RSA private key file' in err \
                 or 'not a valid RSA private key' in err:
             raise exception.InvalidPrivateKey()
         elif 'Unable to connect to port' in err \
                 or 'Invalid ip or port' in err:
             raise exception.InvalidIpOrPort()
         else:
             raise exception.SSHException(err)
     return result
示例#9
0
    def list_alerts(self, context, query_para):
        try:
            # Get list of Hpe3parStor alerts
            try:
                reslist = self.ssh_handler.get_all_alerts()
            except Exception as e:
                err_msg = "Failed to ssh Hpe3parStor: %s" % \
                          (six.text_type(e))
                LOG.error(err_msg)
                raise exception.SSHException(err_msg)

            alertlist = reslist.split('\n')

            return self.handle_alters(alertlist, query_para)
        except exception.DelfinException as e:
            err_msg = "Get alerts failed: %s" % (e.msg)
            LOG.error(err_msg)
            raise e
        except Exception as e:
            err_msg = "Get alert failed: %s" % (six.text_type(e))
            LOG.error(err_msg)
            raise exception.InvalidResults(err_msg)