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
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
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