def kermit_connect(self, mach: linux.LinuxShell) -> channel.Channel: KERMIT_PROMPT = b"C-Kermit>" if self.name == 'imx8qxpmek': cfg_file = f"/home/{self.host.username}/kermrc_imx8qxpmek" elif self.name == 'wandboard': cfg_file = f"/home/{tbot.selectable.LabHost.username}/data/Entwicklung/messe/2019/kermrc_wandboard" else: raise RuntimeError("Board ", self.name, " console not configured") ch = mach.open_channel("kermit", cfg_file) try: try: ret = ch.read(150, timeout=2) buf = ret.decode(errors="replace") if "Locked" in buf: raise RuntimeError(f"serial line is locked {buf}") if "C-Kermit" in buf: raise RuntimeError(f"serial line is locked {buf}") except TimeoutError: pass yield ch finally: ch.send(chr(28) + "C") ch.sendline("exit") # give usb2serial adapter some time time.sleep(2)
def find_ip_address( lnx: linux.LinuxShell, route_target: typing.Optional[str] = None, force: bool = False, ) -> str: """ Find out an IP-address of a host. In times where most hosts have many IP addresses, this is not as trivial as one would like. This testcase approaches the problem by trying to find the IP-address, the host would use on a certain route. By default, this is the route to reach a theoretical host at ``1.0.0.0``. This will yield a sensible result in *most* cases but of course will not always be the address you want. For more fine-grained control you can pass a ``route_target``. This is the IP-address of this theoretical host to reach. :param linux.LinuxShell lnx: The host to work on. :param str route_target: An optional route target. Defaults to ``1.0.0.0``. :param bool force: By default, this testcase caches results for faster lookup when called multiple times. This parameter enforces a re-check which might be useful when the network configuration on ``lnx`` changed. :rtype: str :returns: The IP-address which was found. .. versionadded:: 0.8.3 .. versionchanged:: 0.9.2 Fixed ``find_ip_address()`` not working properly when the route target is a local address. """ if route_target is None: # 1 equals to 1.0.0.0 which will probably yield a sensible route in # most cases. route_target = "1" if (lnx, route_target) not in _IP_CACHE: if shell.check_for_tool(lnx, "ip"): output = strip_ansi_escapes( lnx.exec0("ip", "route", "get", route_target, linux.Pipe, "cat")) match = re.match( r"(?:local\s+)?\S+\s+(?:via\s+\S+\s+)?dev\s+\S+\s+src\s+(\S+).*", output, re.DOTALL, ) assert match is not None, f"Failed to parse `ip route` output ({output!r})!" ip = match.group(1) else: raise NotImplementedError("Found no way to find out ip-address") _IP_CACHE[(lnx, route_target)] = ip return _IP_CACHE[(lnx, route_target)]