Skip to main content

Alternate bindings for libssh2 C library

Project description

Super fast SSH2 protocol library. redlibssh2 provides Python bindings for libssh2. Forked from ssh2-python.

License Latest Version https://travis-ci.org/Red-M/redlibssh2.svg?branch=master https://img.shields.io/pypi/wheel/redlibssh2.svg https://coveralls.io/repos/github/Red-M/Redlibssh2/badge.svg?branch=master https://img.shields.io/pypi/pyversions/redlibssh2.svg Latest documentation

Installation

Binary wheel packages are provided for Linux, OSX and Windows, all Python versions. Wheel packages have no dependencies.

pip may need to be updated to be able to install binary wheel packages - pip install -U pip.

pip install redlibssh2

For from source installation instructions, including building against system provided libssh2, see documentation.

For creating native system packages for Centos/RedHat, Ubuntu, Debian and Fedora, see instructions in the documentation.

Who Should Use This

Developers of bespoke SSH clients.

Who Should Not Use This

Developers looking for ready made SSH clients.

This library is not an SSH client.

Developers looking for high level easy to use clients based on this library should use RedSSH.

This library provides bindings to libssh2 and its API closely matches libssh2.

If the examples seem long, this is not the right library. Use RedSSH.

API Feature Set

At this time all of the libssh2 API has been implemented up to the libssh2 version in the repository. Please report any missing implementation.

Complete example scripts for various operations can be found in the examples directory.

In addition, as redlibssh2 is a thin wrapper of libssh2 with Python semantics, its code examples can be ported straight over to Python with only minimal changes.

Library Features

The library uses Cython based native code extensions as wrappers to libssh2.

Extension features:

  • Thread safe - GIL is released as much as possible. Note that libssh2 does not support sharing sessions across threads

  • Very low overhead

  • Super fast as a consequence of the excellent C library it uses and prodigious use of native code

  • Object oriented - memory freed automatically and safely as objects are garbage collected by Python

  • Use Python semantics where applicable, such as context manager and iterator support for opening and reading from SFTP file handles

  • Raise errors as Python exceptions

  • Provide access to libssh2 error code definitions

Quick Start

Both byte and unicode strings are accepted as arguments and encoded appropriately. To change default encoding, utf-8, change the value of ssh2.utils.ENCODING. Output is always in byte strings.

See Complete Example for an example including socket connect.

Please use either the issue tracker for reporting issues with code.

Contributions are most welcome!

Authentication Methods

Connect and get available authentication methods.

from __future__ import print_function

from ssh2.session import Session

sock = <create and connect socket>

session = Session()
session.handshake(sock)
print(session.userauth_list())

Output will vary depending on SSH server configuration. For example:

['publickey', 'password', 'keyboard-interactive']

Agent Authentication

session.agent_auth(user)

Command Execution

channel = session.open_session()
channel.execute('echo Hello')

Reading Output

size, data = channel.read()
while(size > 0):
    print(data)
    size, data = channel.read()
Hello

Exit Code

print("Exit status: %s" % (channel.get_exit_status()))
Exit status: 0

Public Key Authentication

session.userauth_publickey_fromfile(
    username, 'private_key_file')

Passphrase can be provided with the passphrase keyword param - see API documentation.

Password Authentication

session.userauth_password(
    username, '<my password>')

SFTP Read

from ssh2.sftp import LIBSSH2_FXF_READ, LIBSSH2_SFTP_S_IRUSR

sftp = session.sftp_init()
with sftp.open(<remote file to read>,
               LIBSSH2_FXF_READ, LIBSSH2_SFTP_S_IRUSR) as remote_fh, \
        open(<local file to write>, 'wb') as local_fh:
    for size, data in remote_fh:
        local_fh.write(data)

Complete Example

A simple usage example looks very similar to libssh2 usage examples.

See examples directory for more complete example scripts.

As mentioned, redlibssh2 is intentionally a thin wrapper over libssh2 and directly maps most of its API.

Clients using this library can be much simpler to use than interfacing with the libssh2 API directly.

from __future__ import print_function

import os
import socket

from ssh2.session import Session

host = 'localhost'
user = os.getlogin()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))

session = Session()
session.handshake(sock)
session.agent_auth(user)

channel = session.open_session()
channel.execute('echo me; exit 2')
size, data = channel.read()
while size > 0:
    print(data)
    size, data = channel.read()
channel.close()
print("Exit status: %s" % channel.get_exit_status())
Output:

me

Exit status: 2

SSH Functionality currently implemented

  • SSH channel operations (exec,shell,subsystem) and methods

  • SSH agent functionality

  • Public key authentication and management

  • SFTP operations

  • SFTP file handles and attributes

  • SSH port forwarding and tunnelling

  • Non-blocking mode

  • SCP send and receive

  • Listener for port forwarding

  • Subsystem support

  • Host key checking and manipulation

And more, as per libssh2 functionality.

Comparison with other Python SSH libraries

Performance of above example, compared with Paramiko.

time python examples/example_echo.py
time python examples/paramiko_comparison.py
Output:

redlibssh2:

real       0m0.141s
user       0m0.037s
sys        0m0.008s

paramiko:

real       0m0.592s
user       0m0.351s
sys        0m0.021s

Why did you drop manylinux1 wheels?

Because frankly the manylinux1 docker containers won’t run on my build hosts because I run up to date software and kernels. The manylinux1 docker images are also full of extremely old package versions that will not receive updates or security fixes. The way that ParallelSSH handled this was to bundle their own versions of libssh2, OpenSSL and zlib in the repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redlibssh2-2.1.5.post1.tar.gz (1.2 MB view hashes)

Uploaded Source

Built Distributions

redlibssh2-2.1.5.post1-pp39-pypy39_pp73-manylinux_2_24_x86_64.whl (2.6 MB view hashes)

Uploaded PyPy manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-pp39-pypy39_pp73-manylinux_2_24_aarch64.whl (2.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-pp39-pypy39_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (3.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-pp38-pypy38_pp73-manylinux_2_24_x86_64.whl (2.6 MB view hashes)

Uploaded PyPy manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-pp38-pypy38_pp73-manylinux_2_24_aarch64.whl (2.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-pp38-pypy38_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (3.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-pp38-pypy38_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-pp38-pypy38_pp73-manylinux_2_12_i686.manylinux2010_i686.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ i686

redlibssh2-2.1.5.post1-pp37-pypy37_pp73-manylinux_2_24_x86_64.whl (2.6 MB view hashes)

Uploaded PyPy manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-pp37-pypy37_pp73-manylinux_2_24_aarch64.whl (2.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-pp37-pypy37_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (3.2 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ i686

redlibssh2-2.1.5.post1-pp36-pypy36_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-pp36-pypy3_72-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-pp27-pypy_73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-pp27-pypy_41-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-cp311-cp311-manylinux_2_24_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-cp311-cp311-manylinux_2_24_aarch64.whl (3.5 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (4.6 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

redlibssh2-2.1.5.post1-cp310-cp310-manylinux_2_24_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-cp310-cp310-manylinux_2_24_aarch64.whl (3.5 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (4.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

redlibssh2-2.1.5.post1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl (4.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ i686

redlibssh2-2.1.5.post1-cp310-cp310-macosx_12_0_universal2.whl (5.3 MB view hashes)

Uploaded CPython 3.10 macOS 12.0+ universal2 (ARM64, x86-64)

redlibssh2-2.1.5.post1-cp310-cp310-macosx_11_0_universal2.whl (5.4 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ universal2 (ARM64, x86-64)

redlibssh2-2.1.5.post1-cp310-cp310-macosx_10_10_universal2.whl (4.9 MB view hashes)

Uploaded CPython 3.10 macOS 10.10+ universal2 (ARM64, x86-64)

redlibssh2-2.1.5.post1-cp39-cp39-manylinux_2_24_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-cp39-cp39-manylinux_2_24_aarch64.whl (3.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (4.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

redlibssh2-2.1.5.post1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.5 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (4.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

redlibssh2-2.1.5.post1-cp39-cp39-macosx_12_0_universal2.whl (5.3 MB view hashes)

Uploaded CPython 3.9 macOS 12.0+ universal2 (ARM64, x86-64)

redlibssh2-2.1.5.post1-cp39-cp39-macosx_11_0_universal2.whl (5.4 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ universal2 (ARM64, x86-64)

redlibssh2-2.1.5.post1-cp39-cp39-macosx_10_10_universal2.whl (4.9 MB view hashes)

Uploaded CPython 3.9 macOS 10.10+ universal2 (ARM64, x86-64)

redlibssh2-2.1.5.post1-cp38-cp38-manylinux_2_24_x86_64.whl (4.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-cp38-cp38-manylinux_2_24_aarch64.whl (3.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (4.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

redlibssh2-2.1.5.post1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

redlibssh2-2.1.5.post1-cp38-cp38-macosx_12_0_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.8 macOS 12.0+ x86-64

redlibssh2-2.1.5.post1-cp38-cp38-macosx_11_0_x86_64.whl (5.4 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ x86-64

redlibssh2-2.1.5.post1-cp38-cp38-macosx_10_10_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.8 macOS 10.10+ x86-64

redlibssh2-2.1.5.post1-cp37-cp37m-manylinux_2_24_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-cp37-cp37m-manylinux_2_24_aarch64.whl (3.5 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.6 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

redlibssh2-2.1.5.post1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl (4.2 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

redlibssh2-2.1.5.post1-cp37-cp37m-macosx_12_0_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.7m macOS 12.0+ x86-64

redlibssh2-2.1.5.post1-cp37-cp37m-macosx_11_0_x86_64.whl (5.4 MB view hashes)

Uploaded CPython 3.7m macOS 11.0+ x86-64

redlibssh2-2.1.5.post1-cp37-cp37m-macosx_10_10_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.7m macOS 10.10+ x86-64

redlibssh2-2.1.5.post1-cp36-cp36m-manylinux_2_24_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.24+ x86-64

redlibssh2-2.1.5.post1-cp36-cp36m-manylinux_2_24_aarch64.whl (3.5 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.24+ ARM64

redlibssh2-2.1.5.post1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

redlibssh2-2.1.5.post1-cp36-cp36m-manylinux_2_17_i686.manylinux2014_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686

redlibssh2-2.1.5.post1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.6 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ARM64

redlibssh2-2.1.5.post1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

redlibssh2-2.1.5.post1-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl (4.2 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

redlibssh2-2.1.5.post1-cp36-cp36m-macosx_12_0_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.6m macOS 12.0+ x86-64

redlibssh2-2.1.5.post1-cp36-cp36m-macosx_10_10_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.6m macOS 10.10+ x86-64

redlibssh2-2.1.5.post1-cp35-cp35m-macosx_12_0_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.5m macOS 12.0+ x86-64

redlibssh2-2.1.5.post1-cp35-cp35m-macosx_10_10_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.5m macOS 10.10+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page