Skip to main content

Super fast SSH library - bindings for libssh2

Project description

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

License Latest Version https://travis-ci.org/ParallelSSH/ssh2-python.svg?branch=master https://ci.appveyor.com/api/projects/status/github/parallelssh/ssh2-python?svg=true&branch=master https://img.shields.io/pypi/wheel/ssh2-python.svg https://img.shields.io/pypi/pyversions/ssh2-python.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 ssh2-python

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

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

In addition, as ssh2-python 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 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, ssh2-python 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:

ssh2-python:

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

paramiko:

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

Release history Release notifications | RSS feed

Download files

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

Source Distribution

ssh2-python-0.21.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

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

ssh2_python-0.21.0-cp38-cp38-win_amd64.whl (9.5 MB view details)

Uploaded CPython 3.8Windows x86-64

ssh2_python-0.21.0-cp38-cp38-macosx_10_15_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

ssh2_python-0.21.0-cp38-cp38-macosx_10_14_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

ssh2_python-0.21.0-cp37-cp37m-win_amd64.whl (9.5 MB view details)

Uploaded CPython 3.7mWindows x86-64

ssh2_python-0.21.0-cp36-cp36m-win_amd64.whl (9.5 MB view details)

Uploaded CPython 3.6mWindows x86-64

File details

Details for the file ssh2-python-0.21.0.tar.gz.

File metadata

  • Download URL: ssh2-python-0.21.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.8

File hashes

Hashes for ssh2-python-0.21.0.tar.gz
Algorithm Hash digest
SHA256 c55fc1356de84b11496959d7dd062ee48a3488d4759f53909e5de8f2ed8091e9
MD5 d1303e2f880b350f3ef7dce0560bdc56
BLAKE2b-256 ca63043148bb5f4e47fd7f49b8476df6718ef2e6e08295a4fd48989d0c060053

See more details on using hashes here.

File details

Details for the file ssh2_python-0.21.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: ssh2_python-0.21.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 9.5 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.21.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 bdbe5d10e78183a3d58c76aaf17daadf39a6319bc59f1bec1c6613a4dd942d64
MD5 eccfe3ced36fbbd48bf72559d267e5b0
BLAKE2b-256 663a4473febd42f9a3728af8dfb25deef1fa4f1b138078d6be07bf76eaa178ff

See more details on using hashes here.

File details

Details for the file ssh2_python-0.21.0-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.21.0-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for ssh2_python-0.21.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 5d7423ba2a59bf041e0c6979135c711d25ddb587168701d85d343b3780d6be54
MD5 06ced7a406ae44ae113a6bd8b2ec84be
BLAKE2b-256 e0af33885848f3f14f9939485b8612f13ffcde3482885251b6091c89b161238e

See more details on using hashes here.

File details

Details for the file ssh2_python-0.21.0-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.21.0-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for ssh2_python-0.21.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 fd41b60f611ab7d78005e139fe4145e46f6eda74e8f6b244cdf9200f5160e189
MD5 c367dd235c2fc38a00d56c6cf8fc66dc
BLAKE2b-256 db8444f7e9bae7c800f3479cb9ee5a875126c82d0a37c7aa76b3c19025e02613

See more details on using hashes here.

File details

Details for the file ssh2_python-0.21.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: ssh2_python-0.21.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 9.5 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.21.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 03f6e8c36476af2e7eb2b8652897c0b42d40157f4b24693e8f338da721aa88f5
MD5 54d98ad59ba414251814f2377255c0c3
BLAKE2b-256 0b4192af3eeb374a3e917a9dbdaa698fc2a75847254587e123cbe16fb6a425d2

See more details on using hashes here.

File details

Details for the file ssh2_python-0.21.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: ssh2_python-0.21.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 9.5 MB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.21.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 4fa1df04608b00ab74fcdcb20126762d1b7f675dcd9ce45ee7bff7ddf1804d84
MD5 68e7f834071b67f6b90d206b6faeadff
BLAKE2b-256 13134f324f08b8f77cf3061328e00a90e1507182fba00ca1273af554e3c9fec5

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