Skip to main content

Super fast SSH library - bindings for libssh2

Project description

Super fast SSH2 protocol library. redlibssh2 provides Python bindings for libssh2.

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 on 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.

API Feature Set

At this time all of the libssh2 API has been implemented up to version 1.9.0.

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

  • 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 or the mail group for discussion and questions.

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

redlibssh2-2.0.0-cp39-cp39-manylinux2010_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

redlibssh2-2.0.0-cp38-cp38-manylinux2010_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

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

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

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

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

redlibssh2-2.0.0-cp35-cp35m-manylinux2010_x86_64.whl (4.0 MB view details)

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

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

Uploaded CPython 2.7mu manylinux: glibc 2.12+ x86-64

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

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

File details

Details for the file redlibssh2-2.0.0-cp39-cp39-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.0-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.2 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.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 629896da37b65ef2798fd762b1ab94089c91fba3c410787ba6fd2d54472de693
MD5 ca8ea0a636a36d75c4e7d43b416f98f8
BLAKE2b-256 52cda3c2cf5ab928611581a31310e65bbb7ed70e47e8f8d9c7d78a9ac83cc3a2

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.0-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.3 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.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c39f020866592fc46fa7138b2563eaea8eeb78441cb3a7d99da497cf931f2ec6
MD5 65c8ba57baf17b5549b640591d9761ce
BLAKE2b-256 1f5f4f57e66d9e82bfa62a6882f20acce29bdee41003f41e6105d3fe565ab9f8

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.0-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.0-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.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 fc654d87fc68e5bcbe8dd3c6e0f19c9590a93e20596fac7ee24482546be4850f
MD5 fe4ab932d68c2f31ca37d9ffd1e39c17
BLAKE2b-256 07c5ff291a0980662580d6e971408431c02c2f1bf36edc4fef20cb8a55c6beac

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.0-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.0-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.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 95ebc981c0640cdbbcb537fa3019878975a3c91d0ada48ee1ff3daa8df71c54c
MD5 1d730e644c72092d57a3c8e98b8139d6
BLAKE2b-256 69dbb1585c4201ec062b89f0ba9eebdc06d0a452407d6df4184adc76bba3e9b3

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.0-cp35-cp35m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.0-cp35-cp35m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.0 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.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 1c04184ab173fe1fdc41959e7e6a337b0dfcc72c4249f223b5b85e5d52527669
MD5 7615666c4bd1f0ebea848bcd088d9f68
BLAKE2b-256 01fac3e6a82e7164331da981f3850db44fc78194bde276dfbd646c85a3fb9522

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.0-cp27-cp27mu-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.0-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.0-cp27-cp27mu-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5eb7a8fb345d2c45c82b7e2d0643d39a477bcdb873137977898bc3cad0ce8615
MD5 d4362dbb77c24035fb82c2f0f466fd00
BLAKE2b-256 7c701e26eea284633fe5901f9368dc07c926880f655eaafd61a5775dcc3c19a1

See more details on using hashes here.

File details

Details for the file redlibssh2-2.0.0-cp27-cp27m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: redlibssh2-2.0.0-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.0-cp27-cp27m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f0a6e328efc1e71b010bc44402065dc5f07c169a41581ab3b9a12a2225e2a2c7
MD5 4be974f20bff9e40bbe8880a652c4acf
BLAKE2b-256 d058c65a9af657b3f8f6660cd76318aac536fa9dc466d98ae9e429b5037f70fb

See more details on using hashes here.

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