diff --git a/netbox_sync/core/lancom.py b/netbox_sync/core/lancom.py index 697dcad..992a2bf 100644 --- a/netbox_sync/core/lancom.py +++ b/netbox_sync/core/lancom.py @@ -14,7 +14,7 @@ from netbox_sync.core.switches import ( _interface, _compress_vlans, _membership, - _run_cli_command, + _run_cli_command as _run_with_newline, clean_output, parse_vlan_list, ) @@ -35,6 +35,19 @@ COMMANDS = ( ) +def _run_cli_command(channel, command, timeout=20): + """This CLI executes a command only on "\r"; "\n" is just echoed.""" + return _run_with_newline(channel, command, timeout=timeout, newline="\r") + + +def probe(channel): + """``True`` when the "system" mode shows a LANCOM model; leaves the mode again.""" + _run_cli_command(channel, "system") + system = _run_cli_command(channel, "show", timeout=30) + _run_cli_command(channel, "exit") + return is_lancom(system) + + def is_lancom(text): return bool(re.search(r"(?i)Model Name\s*:\s*LANCOM", text or "")) diff --git a/netbox_sync/core/switches.py b/netbox_sync/core/switches.py index 9b5d141..da9505b 100644 --- a/netbox_sync/core/switches.py +++ b/netbox_sync/core/switches.py @@ -175,8 +175,9 @@ def _read_available(channel, timeout=20, initialize_session=False): return output -def _run_cli_command(channel, command, timeout=20): - channel.send(command + "\n") +def _run_cli_command(channel, command, timeout=20, newline="\n"): + # LANCOM LCOS SX 3.x only executes a command on "\r" (like PuTTY's Enter); "\n" is just echoed. + channel.send(command + newline) try: return _read_available(channel, timeout=timeout) except TimeoutError as error: @@ -261,7 +262,16 @@ def detect_platform(channel, banner): """Identify the switch OS from prompt and ``show version`` output.""" if COMWARE_PROMPT.search(banner): return "hpe_comware" - _run_cli_command(channel, "no page") + try: + _run_cli_command(channel, "no page") + except TimeoutError: + # The command was only echoed: a CLI that needs "\r" as Enter (LANCOM LCOS SX 3.x). + from netbox_sync.core import lancom + channel.send("\r") + _read_available(channel, timeout=20) + if lancom.probe(channel): + return lancom.PLATFORM + raise version = _run_cli_command(channel, "show version", timeout=30) if re.search(r"(?i)ArubaOS-CX|AOS-CX|\b[A-Z]{2}\.10\.\d+", version): return "aruba_cx" @@ -270,10 +280,7 @@ def detect_platform(channel, banner): if CLI_ERROR.search(version): # LANCOM LCOS SX 3.x has no "show version"; its system information is in the "system" mode. from netbox_sync.core import lancom - _run_cli_command(channel, "system") - system = _run_cli_command(channel, "show", timeout=30) - _run_cli_command(channel, "exit") - if lancom.is_lancom(system): + if lancom.probe(channel): return lancom.PLATFORM display = _run_cli_command(channel, "display version", timeout=30) if re.search(r"(?i)Comware", display): diff --git a/tests/test_lancom.py b/tests/test_lancom.py index 4467fca..421687e 100644 --- a/tests/test_lancom.py +++ b/tests/test_lancom.py @@ -104,6 +104,9 @@ class FakeChannel: self.sent, self.mode, self.buffer = [], "", "" def send(self, data): + if not data.endswith("\r"): + self.buffer += data # like the real CLI: "\n" is only echoed, nothing is executed + return command = data.strip() self.sent.append(command) answers = {("system", "show"): SYSTEM, ("port", "show status"): STATUS, @@ -178,3 +181,17 @@ def test_enable_lldp_on_disabled_ports(): snapshot = build_snapshot(dict(outputs(), **result), "lancom_sx3") assert snapshot["lldp_enabled_automatically"] and not snapshot["lldp_disabled"] assert all(isinstance(value, str) for value in snapshot["raw"].values()) + + +def test_auto_detection_falls_back_to_carriage_return(monkeypatch): + from netbox_sync.core import switches + + real = switches._read_available + + def quick(channel, timeout=20, initialize_session=False): + return real(channel, timeout=min(timeout, 0.3), initialize_session=initialize_session) + + monkeypatch.setattr(switches, "_read_available", quick) + channel = FakeChannel() + assert switches.detect_platform(channel, "SW-08# ") == "lancom_sx3" + assert channel.mode == ""