MockSSH is a Python library and DSL (Domain Specific Language) designed to mock an SSH server and its command-line interface. It is primarily used to emulate network devices or other operating systems for testing task automation and performing end-to-end SSH unit tests.
- Purpose: Emulate SSH-enabled devices to test automation without access to physical hardware.
- Core Technologies:
- Python 3.12+: The project has been modernized from Python 2 and supports Python 3.12 and 3.13.
- Twisted (Conch): Used for the underlying SSH transport and protocol implementation.
- Hy (LISP on Python): Provides a DSL in
mocksshy/for configuring servers with minimal code. - Cryptography: Handles secure host key generation (Ed25519, ECDSA, RSA-SHA2).
- Paramiko: Used primarily within the test suite for client-side SSH interactions.
- Ruff: Used for lightning-fast linting and auto-formatting.
- mypy: Used for static type checking.
- pytest & pytest-twisted: Used for modern, robust testing with native Twisted integration.
- build: Used for generating modern source distributions and wheels via
pyproject.toml.
src/MockSSH.py: The main library containing the SSH realm, avatar, and protocol logic (~650 lines).SSHCommand: Base class for custom commands.PromptingCommand: Handles multi-step interactions (e.g., password prompts).ArgumentValidatingCommand: Validates command arguments before execution.SSHShell: Implements the interactive shell and command matching.
src/mocksshy/: Contains the Hy DSL implementation (language.hy).examples/: Provides reference implementations for emulating Cisco (mock_cisco.py) and F5 (mock_F5.py) devices.tests/: Unit tests using pytest and a session-wide Twisted reactor managed inconftest.py.docs/wiki/: Comprehensive project documentation (Foundations, Architecture, Testing, DSL).pyproject.toml: Modern PEP 517/518 compliant configuration for package metadata and dependencies.
The project includes a Makefile that automatically detects the virtual environment (.venv).
| Command | Description |
|---|---|
make |
Default target; executes the full test suite. |
make test |
Runs unit tests using pytest. |
make build |
Generates source distribution and wheel packages. |
make fix |
Automatically fixes linting errors and formats code using Ruff. |
make lint |
Checks code formatting and lints the code using Ruff. |
make typecheck |
Runs static type checking using mypy. |
make clean |
Removes build artifacts, cached files, and generated host keys. |
To start a mock server (e.g., F5):
PYTHONPATH=src .venv/bin/python examples/mock_F5.py- Python 3 Compatibility: Adhere to strict
bytesandstrseparation. Twisted transport methods expectbytes, while command logic typically usesstr. - Security Defaults:
- Supported Host Keys:
ssh-ed25519,ecdsa-sha2-nistp256,rsa-sha2-512, andrsa-sha2-256. - Strong Ciphers: Only modern ciphers like
aes256-ctrare enabled. - Disabled: Weak protocols like
ssh-rsa(SHA-1) anddiffie-hellman-group-exchange-sha1(when moduli are missing).
- Supported Host Keys:
- Host Key Generation: The server automatically generates missing host keys in the
keypathdirectory (defaultgenerated-keys/). - Coding Style: Enforced by Ruff and mypy. Use
make fixto format andmake typecheckto verify types. - Testing: New commands or features should be verified with end-to-end tests in the
tests/directory.
MockSSH is undergoing a significant modernization phase (v2.0.0+). Key focus areas include:
- Modularization: Refactoring the monolithic
src/MockSSH.pyinto a modern package structure. - CI/CD: Implementing robust GitHub Actions workflows for multi-version testing (Python 3.12, 3.13).
- Logging: Replacing standard
printstatements with structured logging for better production-grade observability. - Documentation: Modernizing the wiki (architecture overview created, navigation links fixed).
Refer to TODO.md for the complete roadmap and granular task tracking.
A security audit (v2.0.0) identified several areas for improvement:
- Logging: Avoid using
print()for logging, as it can leak PII (passwords, commands) to stdout. A migration to structured logging is planned. - Host Keys: Generated host keys in
generated-keys/must be restricted to owner-only permissions (0600). - Secrets Management: NEVER commit service account keys or secrets.
This project integrates the Gemini CLI via GitHub Actions to automate development workflows.
- Gemini Dispatch: Orchestrates Gemini tasks via comments.
- Gemini Review: Automated PR reviews.
- Gemini Triage: Issue analysis and labeling.
- Gemini Plan Execution: Autonomous implementation of approved plans.
gemini-invoke.toml: Manual invocations.gemini-review.toml: PR reviews.gemini-triage.toml: Issue triage.gemini-plan-execute.toml: Autonomous task execution.