{"library":"ssh-python","title":"ssh-python Library","type":"library","description":"ssh-python provides robust Python bindings for the `libssh` C library, enabling programmatic interaction with SSH servers. It offers functionalities for connecting, authenticating, executing commands, and managing SSH channels. The library is actively maintained, with version 1.2.0.post1 being the latest stable release, typically releasing minor updates every few months with periodic patch versions.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install ssh-python"],"cli":null},"imports":["from ssh import Session","from ssh import AuthError","from ssh import SSHException","from ssh import SSH_OPTIONS_HOST","from ssh import SSH_AUTH_SUCCESS"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/ParallelSSH/ssh-python","docs":null,"changelog":null,"pypi":"https://pypi.org/project/ssh-python/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import ssh\nimport os\n\ntry:\n    # Initialize an SSH session\n    session = ssh.Session()\n    \n    # Set connection options\n    session.options_set(ssh.SSH_OPTIONS_HOST, os.environ.get('SSH_HOST', 'localhost'))\n    session.options_set(ssh.SSH_OPTIONS_PORT, int(os.environ.get('SSH_PORT', 22)))\n\n    # Connect to the SSH server\n    session.connect()\n    print(f\"Connected to {session.options_get(ssh.SSH_OPTIONS_HOST)}:{session.options_get_int(ssh.SSH_OPTIONS_PORT)}\")\n\n    # Authenticate using password\n    username = os.environ.get('SSH_USER', 'guest')\n    password = os.environ.get('SSH_PASSWORD', 'guestpass') # DO NOT hardcode passwords in production\n\n    if session.userauth_password(username, password) != ssh.SSH_AUTH_SUCCESS:\n        raise ssh.AuthError(\"Authentication failed\")\n    print(f\"Authenticated as {username}\")\n\n    # Open a channel for executing commands\n    channel = session.channel_session()\n    channel.open_session()\n    \n    # Execute a command\n    command = \"echo Hello from ssh-python!\"\n    print(f\"Executing command: '{command}'\")\n    channel.request_exec(command)\n    \n    # Read output\n    stdout = channel.read_stdout(2048)\n    stderr = channel.read_stderr(2048)\n    exit_status = channel.get_exit_status()\n\n    print(f\"--- STDOUT ---\\n{stdout.decode().strip()}\")\n    print(f\"--- STDERR ---\\n{stderr.decode().strip()}\")\n    print(f\"Exit Status: {exit_status}\")\n\n    # Close the channel and session\n    channel.close()\n    print(\"Channel closed.\")\n    session.disconnect()\n    print(\"Session disconnected.\")\n\nexcept ssh.AuthError as e:\n    print(f\"Authentication Error: {e}\")\nexcept ssh.SSHException as e:\n    print(f\"SSH Connection Error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to establish an SSH connection, authenticate with a password (using environment variables for safety), execute a simple command, capture its output, and properly close the session. It also includes basic error handling for common SSH exceptions. Replace `SSH_HOST`, `SSH_USER`, and `SSH_PASSWORD` with your actual server details.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}