{"id":9953,"library":"mock-ssh-server","title":"Mock SSH Server","description":"mock-ssh-server is a Python library providing a mock SSH server for testing purposes. It allows developers to create controlled SSH environments to test clients without needing a real SSH server. The current version is 0.9.1, and it generally follows an infrequent release cadence, driven by feature additions and bug fixes.","status":"active","version":"0.9.1","language":"en","source_language":"en","source_url":"https://github.com/carletes/mock-ssh-server","tags":["ssh","testing","mocking","server","paramiko"],"install":[{"cmd":"pip install mock-ssh-server","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core library for SSH protocol implementation.","package":"paramiko","optional":false},{"reason":"ASN.1 library, a dependency of paramiko.","package":"pyasn1","optional":false},{"reason":"Cryptographic primitives, a dependency of paramiko, often required for key generation.","package":"cryptography","optional":false}],"imports":[{"symbol":"MockSSH","correct":"from mockssh import MockSSH"}],"quickstart":{"code":"import paramiko\nfrom mockssh import MockSSH\nimport socket # For example error handling\n\n# 1. Define users for the mock SSH server\nusers = {\n    \"testuser\": \"testpassword\",\n    \"admin\": \"securepass\"\n}\n\n# 2. Generate a host key for the server (essential for real SSH clients)\n# In a real application, you might load a persistent key.\nserver_host_key = paramiko.RSAKey.generate(2048)\n\nprint(\"Starting Mock SSH server...\")\n# 3. Start the mock SSH server in a context manager\nwith MockSSH(users, host='127.0.0.1', rsa_key=server_host_key) as s:\n    server_port = s.port\n    print(f\"Mock SSH server running on 127.0.0.1:{server_port}\")\n\n    # 4. Demonstrate a client connection using paramiko\n    print(\"\\nAttempting to connect with Paramiko client...\")\n    client = paramiko.SSHClient()\n    # Automatically add new host keys (for testing, not recommended for production)\n    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\n    try:\n        client.connect(\n            hostname='127.0.0.1',\n            port=server_port,\n            username='testuser',\n            password='testpassword'\n        )\n        print(\"Client connected successfully to mock SSH server.\")\n\n        # Execute a simple command\n        stdin, stdout, stderr = client.exec_command('echo \"Hello from mock SSH client!\"')\n        output = stdout.read().decode().strip()\n        error = stderr.read().decode().strip()\n\n        print(f\"Command output: '{output}'\")\n        if error:\n            print(f\"Command error: '{error}'\")\n\n    except paramiko.AuthenticationException:\n        print(\"Authentication failed for 'testuser'! Check username/password.\")\n    except paramiko.SSHException as e:\n        print(f\"SSH connection failed: {e}\")\n    except socket.error as e:\n        print(f\"Socket error: {e}. Is the server running and accessible?\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n    finally:\n        if client.get_transport() and client.get_transport().is_active():\n            client.close()\n            print(\"Client connection closed.\")\n\nprint(\"Mock SSH server context manager exited. Server is stopped.\")","lang":"python","description":"This quickstart demonstrates how to set up a mock SSH server with defined users and then connects to it using a Paramiko SSH client to execute a simple command. This confirms the server is operational and authentication works."},"warnings":[{"fix":"Upgrade to mock-ssh-server version 0.3.0 or newer to resolve deadlock issues.","message":"Versions prior to 0.3.0 were known to experience occasional deadlocks, particularly when interacting with real OpenSSH clients. This could lead to testing hangs or instability.","severity":"gotcha","affected_versions":"<0.3.0"},{"fix":"Upgrade to version 0.9.0 or later to leverage the new authentication class override feature, allowing for more flexible authentication strategies.","message":"Before version 0.9.0, customizing the authentication class was not directly supported via the API. Users might have resorted to workarounds or found their customization options limited.","severity":"gotcha","affected_versions":"<0.9.0"},{"fix":"Ensure `cryptography` is correctly installed. If encountering issues, try `pip install cryptography` explicitly, or ensure your environment has the necessary build tools for it.","message":"When generating host keys (e.g., `paramiko.RSAKey.generate()`), `mock-ssh-server` implicitly relies on `paramiko`'s underlying dependencies like `cryptography`. Missing or misconfigured `cryptography` installations can lead to errors during key generation.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure the package is installed: `pip install mock-ssh-server`. The correct import is `from mockssh import MockSSH`.","cause":"The `mock-ssh-server` package is not installed or the import path is incorrect.","error":"ImportError: No module named 'mockssh'"},{"fix":"Verify that the `users` dictionary passed to `MockSSH` contains the correct `username: password` pair that your client is attempting to use.","cause":"The username or password provided by the client does not match any user configured in the `MockSSH` server instance.","error":"paramiko.ssh_exception.AuthenticationException: Authentication failed."},{"fix":"Check that the `MockSSH` server is started and running within its context manager. Verify that the client's `hostname` and `port` parameters exactly match those provided by the `MockSSH` instance (e.g., `s.port`).","cause":"The mock SSH server is either not running, or the client is attempting to connect to the wrong host or port.","error":"socket.error: [Errno 111] Connection refused"}]}