Fix read_cmd to use interactive shell with enable mode

exec_command runs in user mode on BOSS v7.9.6; most show commands
(show vlan, show sys-info, show poe-main-status, show config, etc.)
require enable mode. Switch to invoke_shell per read command, sending
terminal length 0 and enable before each command.

https://claude.ai/code/session_01JR2EMK7rwrZJowpstcaxQ6
This commit is contained in:
Claude
2026-03-24 03:42:27 +00:00
parent b43eed0546
commit c4001b0465
+24 -3
View File
@@ -314,11 +314,32 @@ def _open_connection() -> paramiko.SSHClient:
def read_cmd(cmd: str) -> str: def read_cmd(cmd: str) -> str:
"""Run a read-only command via the pool. Invalidates pool on error.""" """Run a read-only command in enable mode via interactive shell channel."""
try: try:
conn = _pool.get() conn = _pool.get()
_, stdout, _ = conn.exec_command(cmd, timeout=10) ch = conn.invoke_shell()
return stdout.read().decode("utf-8", errors="replace") ch.settimeout(10)
time.sleep(0.4)
if ch.recv_ready():
ch.recv(4096) # drain login banner
for setup in ["terminal length 0", "enable"]:
ch.send(setup + "\n")
time.sleep(0.3)
if ch.recv_ready():
ch.recv(4096) # drain prompt output
ch.send(cmd + "\n")
out = ""
deadline = time.time() + 8
while time.time() < deadline:
if ch.recv_ready():
out += ch.recv(4096).decode("utf-8", errors="replace")
time.sleep(0.1)
else:
if out:
break
time.sleep(0.1)
ch.close()
return out
except HTTPException: except HTTPException:
raise raise
except Exception as e: except Exception as e: