{"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install mock-ssh-server"],"cli":null},"imports":["from mockssh import MockSSH"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}