Skip to content

Commit

Permalink
Merge pull request #1412 from NickCao/telnet
Browse files Browse the repository at this point in the history
remote/client: fallback to telnet when microcom is not available
  • Loading branch information
Emantor authored Jun 26, 2024
2 parents 1ea0fef + a5a8b65 commit b6ec6f1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ New Features in 24.0
switches running OpenWrt using the ubus interface.
- The pyproject.toml gained a config for `ruff <https://github.com/astral-sh/ruff>`_.
- ``setuptools_scm`` is now used to generate a version file.
- labgrid-client console will fallback to telnet if microcom is not available.


Bug fixes in 24.0
Expand Down
4 changes: 2 additions & 2 deletions doc/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ Now we can connect to the serial console:
labgrid-venv $ labgrid-client -p example-place console
.. note:: Using remote connection requires ``microcom`` installed on the host
where the labgrid-client is called.
.. note:: Using remote connection requires ``microcom`` or ``telnet`` installed
on the host where the labgrid-client is called.

See :ref:`remote-usage` for some more advanced features.
For a complete reference have a look at the :doc:`labgrid-client(1) <man/client>`
Expand Down
27 changes: 21 additions & 6 deletions labgrid/remote/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import signal
import sys
import shlex
import shutil
import json
from textwrap import indent
from socket import gethostname
Expand Down Expand Up @@ -826,18 +827,32 @@ async def _console(self, place, target, timeout, *, logfile=None, loop=False, li
# check for valid resources
assert port is not None, "Port is not set"

call = ["microcom", "-s", str(resource.speed), "-t", f"{host}:{port}"]
microcom_bin = shutil.which("microcom")

if listen_only:
call.append("--listenonly")
if microcom_bin is not None:
call = [microcom_bin, "-s", str(resource.speed), "-t", f"{host}:{port}"]

if listen_only:
call.append("--listenonly")

if logfile:
call.append(f"--logfile={logfile}")
else:
call = ["telnet", host, str(port)]

logging.info("microcom not available, using telnet instead")

if listen_only:
logging.warning("--listenonly option not supported by telnet, ignoring")

if logfile:
logging.warning("--logfile option not supported by telnet, ignoring")

if logfile:
call.append(f"--logfile={logfile}")
print(f"connecting to {resource} calling {' '.join(call)}")
try:
p = await asyncio.create_subprocess_exec(*call)
except FileNotFoundError as e:
raise ServerError(f"failed to execute microcom: {e}")
raise ServerError(f"failed to execute remote console command: {e}")
while p.returncode is None:
try:
await asyncio.wait_for(p.wait(), 1.0)
Expand Down

0 comments on commit b6ec6f1

Please sign in to comment.