{"library":"types-paramiko","code":"import paramiko\nimport os\nimport sys\n\ndef ssh_connect_and_execute(\n    hostname: str,\n    username: str,\n    command: str,\n    password: str = None,\n    port: int = 22\n) -> str:\n    client = paramiko.SSHClient()\n    # Automatically add new host keys (use with caution in production)\n    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n    \n    try:\n        # Load system host keys by default\n        client.load_system_host_keys()\n        \n        if password:\n            client.connect(hostname, port=port, username=username, password=password, timeout=10)\n        else:\n            # Assumes an SSH agent is running or keys are in default locations\n            client.connect(hostname, port=port, username=username, timeout=10)\n\n        # Execute a command\n        stdin, stdout, stderr = client.exec_command(command)\n        output = stdout.read().decode().strip()\n        error = stderr.read().decode().strip()\n        \n        if error:\n            print(f\"Error executing command: {error}\", file=sys.stderr)\n            return \"\"\n        return output\n    except paramiko.AuthenticationException:\n        print(\"Authentication failed, please verify your credentials (username/password/keys).\", file=sys.stderr)\n        return \"\"\n    except paramiko.SSHException as e:\n        print(f\"SSH connection or command execution failed: {e}\", file=sys.stderr)\n        return \"\"\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\", file=sys.stderr)\n        return \"\"\n    finally:\n        # Always close the client connection to prevent resource leaks\n        if client:\n            client.close()\n\nif __name__ == \"__main__\":\n    # Example usage with environment variables\n    HOST = os.environ.get(\"SSH_HOST\", \"your_ssh_server.com\")\n    USER = os.environ.get(\"SSH_USER\", \"your_username\")\n    PASS = os.environ.get(\"SSH_PASSWORD\", \"\") # Use SSH keys whenever possible\n    CMD = os.environ.get(\"SSH_COMMAND\", \"echo Hello from Paramiko!\")\n    PORT = int(os.environ.get(\"SSH_PORT\", 22))\n\n    if HOST == \"your_ssh_server.com\":\n        print(\"Please set SSH_HOST, SSH_USER, and optionally SSH_PASSWORD/SSH_PORT environment variables.\", file=sys.stderr)\n        sys.exit(1)\n\n    print(f\"Attempting to connect to {USER}@{HOST}:{PORT} and execute '{CMD}'\")\n    result = ssh_connect_and_execute(HOST, USER, CMD, PASS if PASS else None, PORT)\n    if result:\n        print(\"\\n--- Command Output ---\")\n        print(result)\n    else:\n        print(\"\\n--- Command execution failed ---\", file=sys.stderr)","lang":"python","description":"This quickstart demonstrates a basic SSH connection and command execution using Paramiko. With `types-paramiko` installed, a type checker can provide static analysis for the Paramiko calls and type hints in this code.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}