Skip to main content

Super fast SSH library - bindings for libssh2

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://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.

Why did you drop Windows and OSX wheels?

I don’t have build infrastructure for them and I don’t use these platforms anywhere. If someone would like these wheels to be built you can open an issue and it’ll be reviewed based on what can be provided to get such builds running.

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

redlibssh2-2.0.1.post1-cp39-cp39-manylinux2010_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

redlibssh2-2.0.1.post1-cp38-cp38-manylinux2010_x86_64.whl (4.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

redlibssh2-2.0.1.post1-cp37-cp37m-manylinux2010_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

redlibssh2-2.0.1.post1-cp36-cp36m-manylinux2010_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

redlibssh2-2.0.1.post1-cp35-cp35m-manylinux2010_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.5mmanylinux: glibc 2.12+ x86-64

redlibssh2-2.0.1.post1-cp27-cp27mu-manylinux2010_x86_64.whl (3.9 MB view details)

Uploaded CPython 2.7mumanylinux: glibc 2.12+ x86-64

redlibssh2-2.0.1.post1-cp27-cp27m-manylinux2010_x86_64.whl (3.9 MB view details)

Uploaded CPython 2.7mmanylinux: glibc 2.12+ x86-64

File details

Details for the file redlibssh2-2.0.1.post1-cp39-cp39-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.1.post1-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: CPython 3.9, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for redlibssh2-2.0.1.post1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 bc40122d94d9ebdd88c59868b225784f4b2d730d6aa859dca63b45339fc92444
MD5 0fcabe158cc08543d6371aa446e10ef0
BLAKE2b-256 bed0dab2a8fbc4e710728ceedd970865174b1c8629814c40b3292131ea0add81

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.1.post1-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.1.post1-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.4 MB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for redlibssh2-2.0.1.post1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 e958c839b7ad4b27279d17a3fc9af1ebae57bbacf1ee881c4bad73d8ea79b1a6
MD5 02cd073a3852297e65297331bfd89acc
BLAKE2b-256 2d996f6a03aa8e63f90b2f0edcea96d84746120e93a85f43b328de097d805a20

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.1.post1-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.1.post1-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.1 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for redlibssh2-2.0.1.post1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d31ea806e9d4d21b28243061622be4aa95a1b15606251ebd8d553ba37e2ab1d9
MD5 3e28d9a723c4722d2ff2ae789236ceb1
BLAKE2b-256 fb9720697e39c4dc813f8dc58290b08b3cec0ffaee7ee71537ea9088a675cbcf

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.1.post1-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.1.post1-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.1 MB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for redlibssh2-2.0.1.post1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 adb688c50dc93eb991a84a7d464da90eac7b34093e2bbc789889e22adc49a5e0
MD5 e781d312012dd6e021dc31fd210437bd
BLAKE2b-256 5f56b4256fb39c31c7977e72d85fe72900ae55baa5e6c6e33664583d47057c6f

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.1.post1-cp35-cp35m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.1.post1-cp35-cp35m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.1 MB
  • Tags: CPython 3.5m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for redlibssh2-2.0.1.post1-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 9bbdde370843deb5768b0945758319b90bc5128f427609c0dfdddc240b5aad82
MD5 9710054769a2c7e911bf77743de10f64
BLAKE2b-256 1ce7138290d7c166bea1b947cfb15089e17da841c061bade1415b594beb9ff4a

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.1.post1-cp27-cp27mu-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.1.post1-cp27-cp27mu-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: CPython 2.7mu, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for redlibssh2-2.0.1.post1-cp27-cp27mu-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 e6e861b283026600b0cb8a144c145b70bfcedbeef16b90e4443ab8fd37c442a5
MD5 0cf4a834e8a0cf74f8846fabbcd4d75f
BLAKE2b-256 55c8c1b0547a22611b1c53c0bb6d319a787a1657938d78b287a3750534a09420

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.1.post1-cp27-cp27m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.1.post1-cp27-cp27m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: CPython 2.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for redlibssh2-2.0.1.post1-cp27-cp27m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d45dc1c3fac10a7d191452370fb8358b514d785e93f7146aeca411d436b1e59e
MD5 58311f2bac48cd881ff1cc2d48275222
BLAKE2b-256 b6ff23da87342dcfa39ec5e246b3865b015e9774c9cd68eb5e768d6c2ff35ee8

See more details on using hashes here.

Supported by

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