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