async def _run_command(self, session, command, timeout): output = await session.run_command(command.encode('utf8'), timeout) return ttypes.CommandResult(output=output.decode('utf8', errors='ignore'), status=session.exit_status or constants.SUCCESS_STATUS, command=command)
def command_failures(cmds): return [ ttypes.CommandResult( output=message, status=constants.FAILURE_STATUS, command=cmd ) for cmd in cmds ]
async def _run_commands( self, commands, device, timeout, open_timeout, client_ip, client_port, uuid, return_exceptions=False, ): options = self._get_options(device, client_ip, client_port, open_timeout, timeout) if device.command_prompts: options["command_prompts"] = { c.encode(): p.encode() for c, p in device.command_prompts.items() } command = commands[0] devinfo = None session = None try: devinfo = await self._lookup_device(device) async with devinfo.create_session(self.service, device, options, loop=self.loop) as session: results = [] for command in commands: result = await self._run_command(session, command, timeout, uuid) results.append(result) return results except Exception as e: self._record_error(e, command, uuid, options, devinfo, session) if not isinstance(e, ttypes.SessionException): e = ttypes.SessionException(message="%r" % e) if return_exceptions: e.message = self.add_debug_info_to_error_message( # noqa error_msg=e.message, uuid=uuid # noqa ) return [ ttypes.CommandResult(output="", status="%r" % e, command=command) ] else: # raise from the original place so we have full stacktrace raise e
async def _run_commands(self, commands, device, timeout, open_timeout, client_ip, client_port, return_exceptions=False): options = self._get_options(device, client_ip, client_port, open_timeout, timeout) if device.command_prompts: options['command_prompts'] = { c.encode(): p.encode() for c, p in device.command_prompts.items() } command = "" try: devinfo = await self._lookup_device(device) async with devinfo.create_session(self.service, device, options, loop=self.loop) as session: results = [] for command in commands: result = await self._run_command(session, command, timeout) results.append(result) return results except Exception as e: if not isinstance(e, ttypes.SessionException): e = ttypes.SessionException(message='%r' % e) if return_exceptions: return [ ttypes.CommandResult(output='', status='%r' % e, command=command) ] else: raise e from e
def build_result(self, output, status, command): return ttypes.CommandResult(output=output, status=status, command=command)