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.23.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.23.0-cp39-cp39-manylinux2010_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

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

Uploaded CPython 3.8Windows x86-64

ssh2_python-0.23.0-cp38-cp38-manylinux2010_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

ssh2_python-0.23.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.23.0-cp37-cp37m-win_amd64.whl (9.5 MB view details)

Uploaded CPython 3.7mWindows x86-64

ssh2_python-0.23.0-cp37-cp37m-manylinux2010_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.23.0-cp37-cp37m-macosx_10_14_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

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

Uploaded CPython 3.6mWindows x86-64

ssh2_python-0.23.0-cp36-cp36m-manylinux2010_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.23.0-cp35-cp35m-manylinux2010_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.5mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.23.0-cp27-cp27mu-manylinux2010_x86_64.whl (3.8 MB view details)

Uploaded CPython 2.7mumanylinux: glibc 2.12+ x86-64

ssh2_python-0.23.0-cp27-cp27m-manylinux2010_x86_64.whl (3.8 MB view details)

Uploaded CPython 2.7mmanylinux: glibc 2.12+ x86-64

File details

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

File metadata

  • Download URL: ssh2-python-0.23.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2-python-0.23.0.tar.gz
Algorithm Hash digest
SHA256 2efa716e16924b025ea0ea3f1811fe77926b2224a9b235175e7dbaf24dd756d3
MD5 eba9facef3d11fc624ff020c1c5e9761
BLAKE2b-256 aa92f6acb9430894d0d0ff64741d8364ae88c64abb9f4a3c3a83d1fc1c0f315f

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp39-cp39-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.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/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.23.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 21d20426c6ef81ea1dc336c3503d6ea1bb22816704539e99a5c138a85a405b32
MD5 6e18af070d597b1db691e5a4939768d6
BLAKE2b-256 7904a9edd0a03ccfa1c6dcf8dc7c8a2542407c2d78e33d19c022cdd08b32a7ba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.23.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.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.23.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d96ad5c2d5c368ed567440bbb8376c8273265144d65d4e8f79d969027ce22523
MD5 505ad8cd4f25f86f28542adf8775e13a
BLAKE2b-256 fb7bc1511648c2f7cb7174b9dfa51548276543cfb251950ec881b344e1528ee1

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.23.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 2832770a56ca3a46c34319fd13a3913e123363027a0f0ed186c583f1cc6d9f53
MD5 dc173f575d70a227647109fcaa586171
BLAKE2b-256 6e8bb8bff5848cf53e9dafef618076a149f6e3115523591697daa8f7255e95a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.23.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.6.0 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.4

File hashes

Hashes for ssh2_python-0.23.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 5b9c7c2925c662f0962fc89315b14f9ec9866e2c9689c8f241c91d0b39569ab2
MD5 2231ca7c9ee1008197810d004496debb
BLAKE2b-256 d880fe90274839e80800af3280da8a66ff9cbea0b5b088ebcb0bd445e87d304c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.23.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.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.23.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 ffc84ad644d9e40d8fa2b5b21fa3246c905399cae842ab08d6e7ac25aa6171d1
MD5 d7dd0cf836c465479c6aee57846bd36e
BLAKE2b-256 486b579bb3082fd81a87d68f9a2d3c7419f8af2306d253e82bbe2707685a8c18

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.0 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.23.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 2ba8f42e30766168b762a3db5a66e540e048f074745a91803c54578d4e203905
MD5 c7e77b80287b9d444cc28b79e1f72991
BLAKE2b-256 611f046a7cd8357cbdb5e73c3a537fe19a99bbf101916b9fda45c423aaaff488

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.0-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.4

File hashes

Hashes for ssh2_python-0.23.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 a218668b2cc3f83d61642def04a04e4bf5b0d2f088b00b57938906e9818b9d46
MD5 9779d669b8c12e768f339a5b9ab1fd2d
BLAKE2b-256 708c007f6b6040859fdc5c22be904d138e641a80ab99fe3864a752f4acc25fa9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.23.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.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.23.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 b662cfc277009310454b70c4f12b22f63c4360909753c1d44d50b88995c388ab
MD5 d1dcb3abf9768fca78abbff537281b76
BLAKE2b-256 c4b4d5afa2be6e47876bd41e163b270897b6f6121deaa867375e4fdfd1d7f42a

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.0-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.0 MB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.23.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b1286df01868c631e68fd3e534d934043683fd2883a8d82bf7e2d0e7491b97b5
MD5 2cf2c8fb1991b9c69e379d3604812162
BLAKE2b-256 4d33a6681e228272bf86a8d2335e88cc7fb796a5d7f750d123322833a1288cbb

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp35-cp35m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.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/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.23.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8e8284db674ed21ee9451bde26263e63cc97f01c637f29c982b9937ba954ea24
MD5 85b3f67a50e5272a1bebfc92906dcd5c
BLAKE2b-256 f8bdd7f965d941ff4d11b0769b9f765bcc893702158f37f6dba2f7708b125e1d

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp27-cp27mu-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.0-cp27-cp27mu-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 2.7mu, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.23.0-cp27-cp27mu-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 3a6098365d7277c58c7051e2859103faee2e20e98ecf96b9c390f6bf103e4126
MD5 5428811b4a3a3a6c0a3c3669d7f0c823
BLAKE2b-256 e7733291ed161a527bcc83ac965a062b12d844ec1288ba4d34529f7add9ad23e

See more details on using hashes here.

File details

Details for the file ssh2_python-0.23.0-cp27-cp27m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: ssh2_python-0.23.0-cp27-cp27m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 2.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.23.0-cp27-cp27m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 65e7e0ad968a621f8886ca7a92ea3bb38102e9b408f980a1401346ae14087d6e
MD5 ddf85a84a9664499401bd3c7290fc1ef
BLAKE2b-256 3074873c04aac3ecc0c6033b51d642ae10b0a66192d4f6ad677f6d55e25ac310

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