{"library":"ssh2-python","title":"ssh2-python","type":"library","description":"ssh2-python provides Python bindings for the `libssh2` C library, offering a low-level, high-performance interface for SSHv2 protocol operations. It is currently at version 1.2.0.post1 and is actively maintained with regular releases that include updates to embedded libraries like `libssh2` and OpenSSL, and support for newer Python versions.","language":"python","status":"active","last_verified":"Mon May 18","install":{"commands":["pip install ssh2-python"],"cli":null},"imports":["from ssh2.session import Session","from ssh2.sftp_handle import SFTPHandle","from ssh2.channel import Channel","from ssh2 import exceptions"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/ParallelSSH/ssh2-python","docs":null,"changelog":null,"pypi":"https://pypi.org/project/ssh2-python/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import socket\nimport os\nfrom ssh2.session import Session\n\nHOST = os.environ.get('SSH_HOST', 'localhost')\nPORT = int(os.environ.get('SSH_PORT', '22'))\nUSERNAME = os.environ.get('SSH_USERNAME', 'user')\nPASSWORD = os.environ.get('SSH_PASSWORD', 'password')\n\ntry:\n    # 1. Create a socket and connect\n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    sock.connect((HOST, PORT))\n\n    # 2. Initialize an SSH2 session\n    session = Session()\n    session.handshake(sock)\n\n    # 3. Authenticate with password\n    session.userauth_password(USERNAME, PASSWORD)\n\n    # 4. Check if authenticated\n    if not session.is_userauth_authenticated():\n        print(\"Authentication failed!\")\n        exit(1)\n\n    print(f\"Successfully authenticated to {HOST} as {USERNAME}\")\n\n    # 5. Open a channel and execute a command\n    channel = session.open_session()\n    channel.execute('echo Hello from ssh2-python; uname -a')\n\n    # 6. Read output\n    size, data = channel.read()\n    while size > 0:\n        print(data.decode('utf-8').strip())\n        size, data = channel.read()\n\n    # 7. Get exit status\n    print(f\"Command exit status: {channel.get_exit_status()}\")\n\n    # 8. Close the channel and session\n    channel.close()\n    session.disconnect()\n    sock.close()\n\nexcept exceptions.SSH2Error as e:\n    print(f\"SSH2 Error: {e}\")\nexcept socket.error as e:\n    print(f\"Socket Error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n","lang":"python","description":"This quickstart demonstrates how to establish an SSH connection, authenticate with a username and password, execute a remote command, read its output, and gracefully close the connection. Ensure `SSH_HOST`, `SSH_PORT`, `SSH_USERNAME`, and `SSH_PASSWORD` environment variables are set for actual use.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-18","installed_version":"1.2.0.post1","pypi_latest":"1.2.0.post1","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":50,"avg_install_s":1.8,"avg_import_s":0.01,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"ssh2-python","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"ssh2-python","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.01,"mem_mb":0.8,"disk_size":"33M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"ssh2-python","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"ssh2-python","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.9,"import_time_s":0.02,"mem_mb":0.7,"disk_size":"35M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"ssh2-python","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"ssh2-python","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"28M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"ssh2-python","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"ssh2-python","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"27M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"ssh2-python","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"ssh2-python","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.1,"import_time_s":0.01,"mem_mb":0.8,"disk_size":"32M"}]}}