host.session¶
Persistent shell sessions for remote command execution.
ShellSession provides a unified, stateful command execution layer on top of SSH and telnet connections. All commands share a single shell — state (working directory, environment, user context) persists between calls.
Key features: - Sentinel-based output demarcation with exit code extraction - Expect-enhanced run_cmd for interactive commands (sudo, su, etc.) - Raw send/expect for driving non-shell interactive programs - Per-command timeout with Ctrl+C recovery
- class otto.host.session.ShellSession¶
Bases:
ABCAbstract base for persistent shell sessions.
Subclasses implement the I/O primitives (_write, _read_until_pattern, _open, close). The base class provides shared logic for sentinel-wrapped command execution, expect handling, and timeout recovery.
- property alive : bool¶
Whether the session is initialized and responsive.
- async send(text)¶
Send raw text to the session’s stdin.
Use this for driving interactive programs (REPLs, custom CLIs). The caller is responsible for including line endings.
- Return type:¶
None
-
async expect(pattern, timeout=
30.0)¶ Wait for a pattern in the output stream.
Returns captured data up to and including the match. Raises asyncio.TimeoutError if the pattern isn’t seen within timeout. Marks the session as dead if EOF is received.
- Return type:¶
str
-
async run_cmd(cmd, expects=
None, timeout=None)¶ Execute a shell command with sentinel-based output demarcation.
Output is streamed line-by-line to
_on_outputas it arrives. Sentinels and echoed command text are filtered out automatically.- Parameters:¶
cmd – The shell command to execute.
expects – Optional list of (pattern, response) tuples. If a pattern appears in the output before the end sentinel, the response is automatically sent. Expects are inherently optional — if the pattern never appears, the end sentinel matches normally.
timeout – Optional timeout in seconds. On expiry, the session attempts recovery via Ctrl+C and returns Status.Error.
- Return type:¶
- Returns:¶
CommandStatus with exit code extracted from the sentinel.
- class otto.host.session.SshSession(conn)¶
Bases:
ShellSessionSSH persistent shell session via asyncssh create_process().
-
class otto.host.session.TelnetSession(reader, writer, _owned_client=
None)¶ Bases:
ShellSessionTelnet persistent shell session via telnetlib3 streams.
- class otto.host.session.LocalSession¶
Bases:
ShellSessionLocal persistent bash shell session via asyncio subprocess.
Implements the ShellSession I/O primitives using a long-running bash process, giving LocalHost the same sentinel-wrapped execution, expect handling, and timeout recovery that remote sessions enjoy.
- class otto.host.session.HostSession(name, session, log_command, log_output, deregister)¶
Bases:
objectA named persistent shell session on any host type.
Obtained via
await host.open_session(name). Supports the async context manager protocol for automatic cleanup.Example:
async with (await host.open_session("monitor")) as mon: result = await mon.run("stat /tmp/file.bin")Or without a context manager:
mon = await host.open_session("monitor") try: result = await mon.run("stat /tmp/file.bin") finally: await mon.close()- property alive : bool¶
Whether the underlying shell session is still active.
-
async run(cmds, expects=
None, timeout=10.0)¶ Execute one or more commands on this named session.
Mirrors
Host.run(): accepts astr, aShellCommand, or a sequence mixing the two, and always returns aRunResult. Per-commandexpects/timeouton aShellCommandoverride the run-level defaults; a scalarExpecttuple at the run level is normalized to a one-element list.
- class otto.host.session.SessionManager(connections=None, name='', log_command=<function SessionManager.<lambda>>, log_output=<function SessionManager.<lambda>>, session_factory=None, oneshot_factory=None)¶
Bases:
objectManages persistent shell sessions for any host type.
Owns the default session (used by
run_cmd/send/expect) and all named sessions (created viaopen_session).Session creation is pluggable: provide a
session_factorycallable to control how sessions are created (e.g.LocalSessionfor local hosts), or pass aConnectionManagerto use the default SSH/Telnet dispatch. Similarly,oneshot_factorycontrols stateless command execution.- property has_live_sessions : bool¶
Whether any session (default or named) is currently alive.