diff --git a/netbox_sync/core/lancom.py b/netbox_sync/core/lancom.py index 992a2bf..3a54fa9 100644 --- a/netbox_sync/core/lancom.py +++ b/netbox_sync/core/lancom.py @@ -11,10 +11,10 @@ import time from netbox_sync.core.switches import ( CLI_ERROR, - _interface, + CLI_PROMPT, _compress_vlans, + _interface, _membership, - _run_cli_command as _run_with_newline, clean_output, parse_vlan_list, ) @@ -35,9 +35,33 @@ COMMANDS = ( ) +# The CLI pages long outputs ("-- more --, next page: Space, ..., q to quit") and cannot be told not to. +PAGER = re.compile(r"(?im)^[^\n]*(?:q to quit|-+\s*more\s*-+)[^\n]*\Z") + + 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") + """Run a command; this CLI executes it only on "\r" ("\n" is just echoed). + + Paging prompts are answered with Space and removed from the output. + """ + channel.send(command + "\r") + chunks, started = [], time.monotonic() + while time.monotonic() - started < timeout: + if channel.recv_ready(): + chunks.append(channel.recv(65535).decode("utf-8", errors="replace")) + text = clean_output("".join(chunks)) + if PAGER.search(text): + chunks = [PAGER.sub("", text)] + channel.send(" ") + started = time.monotonic() # every page gets the full timeout + continue + if CLI_PROMPT.search(text): + return text + time.sleep(0.05) + text = clean_output("".join(chunks)) + tail = " | ".join(line.strip() for line in text.splitlines()[-3:] if line.strip()) + raise TimeoutError(f"CLI-Befehl nicht vollständig abgeschlossen: {command}. Der Switch hat die CLI-Ausgabe nicht " + f"mit einem vollständigen Prompt abgeschlossen. Letzte Ausgabe: {tail[-300:] or '–'}") def probe(channel): diff --git a/tests/test_lancom.py b/tests/test_lancom.py index 3b775a1..4e1a33c 100644 --- a/tests/test_lancom.py +++ b/tests/test_lancom.py @@ -214,6 +214,9 @@ def _quick_reads(monkeypatch): real = switches._read_available monkeypatch.setattr(switches, "_read_available", lambda channel, timeout=20, initialize_session=False: real(channel, timeout=min(timeout, 0.3), initialize_session=initialize_session)) + command = lancom._run_cli_command + monkeypatch.setattr(lancom, "_run_cli_command", lambda channel, text, timeout=20: + command(channel, text, timeout=min(timeout, 0.3))) return switches @@ -244,3 +247,23 @@ def test_session_is_attached_to_errors(monkeypatch): with pytest.raises(TimeoutError) as caught: switches.run_switch_commands({"host": "x", "platform": "lancom_sx3"}) assert "SW-08#" in caught.value.transcript and "»» system" in caught.value.transcript + + +def test_paging_is_answered_with_space(): + class Paging(FakeChannel): + def send(self, data): + if data == " ": + self.sent.append("") + self.buffer += "\r \rFlash Size : 32\nSW-08(system)# " + elif data.strip() == "show" and self.mode == "system": + self.sent.append("show") + self.buffer += "show\nModel Name : LANCOM GS-2326P+\nRAM Size : 128\n -- more --, next page: Space, q to quit" + else: + super().send(data) + + channel = Paging() + channel.mode = "system" + output = lancom._run_cli_command(channel, "show", timeout=2) + assert channel.sent == ["show", ""] + assert "RAM Size : 128" in output and "Flash Size : 32" in output + assert "quit" not in output and "more" not in output