def _send(self, command): """Sends a command to the remote device and returns the response""" buff = StringIO() errored_response = None self._channel.sendall(str(command) + '\r') while True: try: response = self._channel.recv(1024) except socket.timeout: message = "% Timed out while running: {}".format(command) raise ExecuteFailed(message) buff.write(response) buff.seek(buff.tell() - 150) window = buff.read() if self._handle_errors(window): errored_response = buff.getvalue() # deal with interactive input self._handle_input(window, command.prompt, command.answer) if self._handle_prompt(window): data = buff.getvalue() data = self._clean_response(command, data) if errored_response: raise ExecuteFailed(errored_response) else: return (data)
def send(self, commands, **kwargs): results = [] for command in commands: if command.cmd in COMMANDS: try: results.append(COMMANDS[command.cmd](self._authorized)) except Exception as exc: raise ExecuteFailed('Command failed: {}'.format( exc.message)) else: raise ExecuteFailed('Invalid command {}'.format(command.cmd), command) return results
def _send(self, command): """Sends a command to the remote device and returns the response""" buff = StringIO() errored_response = None self._channel.sendall(str(command) + '\r') # wait for channel to be recv_ready (only seems to be a problem in py3) while not self._channel.recv_ready(): # waiting for channel to be recv_ready... time.sleep(.01) while True: try: response = self._channel.recv(1024).decode("utf-8") except socket.timeout: message = "% Timed out while running: {}".format(command) raise ExecuteFailed(message) buff.write(response) place = buff.tell() - 150 if place < 0: place = 0 buff.seek(place) window = buff.read() if self._handle_errors(window): errored_response = buff.getvalue() # deal with interactive input self._handle_input(window, command.prompt, command.answer) if self._handle_prompt(window): data = buff.getvalue() data = self._clean_response(command, data) if errored_response: raise ExecuteFailed(errored_response) else: return (data)
def send(self, commands, **kwargs): results = [] encoding = kwargs.get('encoding', 'text') timestamps = kwargs.get('timestamps', False) if self._authorize: commands = [self._authorize] + commands try: response = self._conn.send(_format_commands(commands), encoding=encoding, timestamps=timestamps) # print "RESPONSE:", response except (requests.HTTPError, requests.ConnectionError) as exc: raise ExecuteFailed(exc.message) data = response.json() if 'error' in data: err_code = data['error']['code'] err_msg = data['error']['message'] data = data['error']['data'] raise ExecuteFailed("[{}] {}".format(err_code, err_msg)) for item in data['result']: if encoding == 'text': output = item['output'] else: output = item results.append(output) if len(results) > 1 and self._authorize: results.pop(0) return results
def send(self, commands, **kwargs): # result = None results = [] response = None status_code = 0 status_message = None encoding = kwargs.get("encoding", kwargs.get("format", "text")) timestamps = kwargs.get("timestamps", False) timeout = kwargs.get("timeout", None) if self._authorize: commands = [self._authorize] + commands try: response = self._session.execute(prepare_commands(commands), encoding=encoding, timestamps=timestamps, timeout=timeout) except eapi_.EapiError as exc: raise ExecuteFailed(str(exc)) status_code = response.code status_message = response.message for command, result in zipnpad(commands, response.result): errored = None if result.dict: if "errors" in result.dict: errored = True else: errored = False else: errored = True results.append([command, result.result, errored]) if len(results) > 1 and self._authorize: results.pop(0) return (results, status_code, status_message)
def raise_for_error(self): errors = self.errored() if errors: raise ExecuteFailed(errors[0])