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

Conda is another installation option - see documentation for more detailed instructions.

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

  • 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())
['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, 'my_pkey.pub', 'my_pkey', '')

Where '' can be a passphrase.

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.15.0.post2.tar.gz (561.5 kB 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.15.0.post2-cp37-cp37m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.7m

ssh2_python-0.15.0.post2-cp36-cp36m-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.6mWindows x86-64

ssh2_python-0.15.0.post2-cp36-cp36m-win32.whl (1.5 MB view details)

Uploaded CPython 3.6mWindows x86

ssh2_python-0.15.0.post2-cp36-cp36m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.6m

ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_13_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.6mmacOS 10.13+ x86-64

ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_12_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.6mmacOS 10.12+ x86-64

ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_11_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.6mmacOS 10.11+ x86-64

ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_10_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.6mmacOS 10.10+ x86-64

ssh2_python-0.15.0.post2-cp35-cp35m-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.5mWindows x86-64

ssh2_python-0.15.0.post2-cp35-cp35m-win32.whl (1.5 MB view details)

Uploaded CPython 3.5mWindows x86

ssh2_python-0.15.0.post2-cp35-cp35m-manylinux1_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.5m

ssh2_python-0.15.0.post2-cp34-cp34m-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.4mWindows x86-64

ssh2_python-0.15.0.post2-cp34-cp34m-win32.whl (1.5 MB view details)

Uploaded CPython 3.4mWindows x86

ssh2_python-0.15.0.post2-cp34-cp34m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.4m

ssh2_python-0.15.0.post2-cp27-cp27mu-manylinux1_x86_64.whl (3.1 MB view details)

Uploaded CPython 2.7mu

ssh2_python-0.15.0.post2-cp27-cp27m-win_amd64.whl (1.9 MB view details)

Uploaded CPython 2.7mWindows x86-64

ssh2_python-0.15.0.post2-cp27-cp27m-win32.whl (1.5 MB view details)

Uploaded CPython 2.7mWindows x86

ssh2_python-0.15.0.post2-cp27-cp27m-manylinux1_x86_64.whl (3.1 MB view details)

Uploaded CPython 2.7m

ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_13_x86_64.whl (2.0 MB view details)

Uploaded CPython 2.7mmacOS 10.13+ x86-64

ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_12_x86_64.whl (2.0 MB view details)

Uploaded CPython 2.7mmacOS 10.12+ x86-64

ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_11_x86_64.whl (2.0 MB view details)

Uploaded CPython 2.7mmacOS 10.11+ x86-64

ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_10_intel.whl (2.3 MB view details)

Uploaded CPython 2.7mmacOS 10.10+ Intel (x86-64, i386)

File details

Details for the file ssh2-python-0.15.0.post2.tar.gz.

File metadata

File hashes

Hashes for ssh2-python-0.15.0.post2.tar.gz
Algorithm Hash digest
SHA256 134a5cbf741aa6b36875a60501763ef1860684fae90da43933d3b429e17fb391
MD5 483798dc21d38691307a9b6d4ec23e11
BLAKE2b-256 9be45e24a1a692b245a6c27e126b47a3bf2bbdcb320a5d98371f907fd048c19d

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7e540e4e9b6c68297b8e53ee0997a477e21ebf9ca5cf0be0aa39c65b75ba76ac
MD5 02d0f20755d76315b54c60e10af246fb
BLAKE2b-256 8293c6e80e3eb1912e6123ba4e44a6401ceba427c9f76f2adf83823882405d3f

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 9c13a0196634e320961d9f2e972a12ea2291cda834485725c5a075f41092c4b8
MD5 56b09353234dad7fe4e929ff1cc7a335
BLAKE2b-256 dceba1263ff6a1fa0ccda225470bf7b3d3ba809817708eba0c46aa09d5b77a5d

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 3e48b738acfd261591582edfdffda9d609f5a16f5be9db672ee7293f70305ca0
MD5 8af2ce857c59121b31630ac2eb48b90b
BLAKE2b-256 d8a9ba1731cf7def8da59b61066a8e83a3fb940568d25828813f002f9a289073

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e63321158a5f9943a92a7292a3186e65d63efb257d73b342c44efd98b4517cbc
MD5 b2f03881ee3e4ff4867d7016c88a1ab8
BLAKE2b-256 29a06d9b1f7d9f3057e7028987d47776160cbad15392f0feac8d7220dd760f42

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 6bba83d78765ef6350c8878d0703547e56529461adc694937cc1cb40b9496362
MD5 7717d6914f9ad34df0a4ee4f69efa33d
BLAKE2b-256 756f8c3de053590bc30aeec9f7cc74f2c46b5706a0c223409154d7fc63ce662d

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f193f74cfa7169fc7c29f4e7f18e48a3433c129f140bde27b514d56be6119dbb
MD5 75c1b6eefc086cb01ab4482f811d2685
BLAKE2b-256 77902f8fc777f12a96b7800c6bba5a67827ff02753272314123904e77c199c85

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 37aafa10221e0f0b4992491101100a392b97f9e36a6d5cd58a61c74a908b1980
MD5 23ac137f99c8ac43f3573ee8af8e7c6c
BLAKE2b-256 59ecef6eebd39e04fecbf53b8161ce5a3179a9edd61389ce4039529d194158c8

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp36-cp36m-macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 1396f7ffb24532b9b52d129cad791159c4f7c4f43e6235cfdd4091dcd0c3f4d3
MD5 a5add7d42cd0a4ee238e7ae377efa595
BLAKE2b-256 35a62f64dc9d9e87e286d9efb716d92a6883095f17d6fde5feb215d6a4e11ed6

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 d990d6d62b2d5b9f87f5651d89016f82f9008aa3de846834a58360bb8fe6a063
MD5 5904aff5b1efd6b454bb7d83db1979c7
BLAKE2b-256 20e59ab6558b7ad9a36057e123af87c27bb3035e8363f93d147454ff1e2c035a

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 401f6148bae869081e70b1a6e47f3f87c00b1766c7eff5dba1b1e42cd2720c24
MD5 33ba5db775d6b86a7b0ef15d1aec6454
BLAKE2b-256 bf8a98d286ebd60350c0547c3d12b9fa4ceea5dc14a44fdce655fc7d41cf2d62

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6bfd53fd3f6521b7ef24ea38d1f9d32f0931c3926df11c056c13dae76bc5a8d2
MD5 5747f8d141828fc69b95b3586ef4cfcb
BLAKE2b-256 7f1128a9611cac123ac73c524322bed696953444eb48361054fce8aa46dc7e97

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 054f6684514bd20d06bd02b677ddd3d885344a4a75caa3999bb7fd44930509a6
MD5 f2a68973d614b8c2ea03232338653d25
BLAKE2b-256 7e1978cfbda329f16b43435c688f791a9850ba470ac2b40018506daff364f8e7

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 0ed384a6b12e7f679ce158d1711d6e209a092f351b98fa3a2bfd7d6cd071166a
MD5 2e93b61e77c1c2b121ad08805674a62c
BLAKE2b-256 89256f21bdfa87090f019fcfbf738d43b7bd24053b7f7590006d1ecd5ef6b971

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0c101eb79c33b08d833d29ac1962de98944d2199acf47612e413ffdc7e5e06ee
MD5 7e53dd0ba745598374391537abf4557e
BLAKE2b-256 f43b71ec8bcb6901a24da27bce4061e9b86687505eb926f0c2c61bb6999a732f

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 610e74999d7557164b51070c7138140ca7edafd6589f76b59896c4b36465c517
MD5 3fdec64f40cf781b65ab4481a9fb4aa4
BLAKE2b-256 269b4ecf698e52e6b9462c353e4909779a0013099f4c4d992ae5274d714e6105

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 3a3b77b1c573705d0996e40c6bd0065a2461ae74c4c69f9b201856d4515b5e2f
MD5 ee9ffb043d7ea2aa2bdacd9ebb14f79a
BLAKE2b-256 d31fe3f3fb097cb9db2cb7a51486843e0851930e62b453e753fb9d8f05f52478

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 69493ac9b2fa0d7a49699a7e6fe691265ed413549ec56ee059cb84a400616e11
MD5 200a3894e6e26ca6900f329233c95c51
BLAKE2b-256 4a0929e119e1acec63b374406f34c2d70fdef6dbd3b49f5e84cae39421ff68ff

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b177b073a6b04954dfe70aff989212f4c687f6b961cb0697baf82f3bce8cc46d
MD5 ee5f80db8e49e097b6083660ee09a7bc
BLAKE2b-256 0b2f55494c7df41fef2d866ae75918a3ffbd4256996e629abcb4694934b77ffc

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5888b7a4b0dc3a3b363c9751058523a37e67fd0669e8f9dbd455ca49522545e0
MD5 374c1fdd80c66e98bdaa456bb3485604
BLAKE2b-256 c214a587088d021e360df6224b28f703d7a78b7e71dbb178680f840c6ab5e5f7

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5167b3fbbcf4f531cc4bfa0ea733bfa33d6d11c9fef653faf864680c1223d200
MD5 5f3ba136fa3543cb6bfdb14c69007b25
BLAKE2b-256 7afec396ae0739bb639aa5e0c3cf4ef651e629a39cf64e8ebbb5b4e8df5c0647

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 11d17b49095553ef5ab857a699d780077ed760efab728f28ae0db50c8afda4f1
MD5 5a03719e606b0ce19f6c246919314465
BLAKE2b-256 0bc066aa01f537ddce24b8722d065867eb5dfc1e18933376300d2b03177ad08b

See more details on using hashes here.

File details

Details for the file ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for ssh2_python-0.15.0.post2-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 28ef2d6d76b39f6c7175f680f44d9c86b89a135f1b77bb3bc63f1c996f7b8dd7
MD5 1bec70253a9bdfbb634fbe7b22b8336c
BLAKE2b-256 3c1afcb7488557d695a2776adf8caea4bb0f9611f341fb02b3ea3a57884865e2

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