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

Project details


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.14.0.tar.gz (559.0 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.14.0-cp36-cp36m-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.6mWindows x86-64

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

Uploaded CPython 3.6mWindows x86

ssh2_python-0.14.0-cp36-cp36m-manylinux1_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.6m

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

Uploaded CPython 3.5mWindows x86-64

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

Uploaded CPython 3.5mWindows x86

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.4mWindows x86-64

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

Uploaded CPython 3.4mWindows x86

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

Uploaded CPython 3.4m

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

Uploaded CPython 2.7mu

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

Uploaded CPython 2.7mWindows x86-64

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

Uploaded CPython 2.7mWindows x86

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

Uploaded CPython 2.7m

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

Uploaded CPython 2.7mmacOS 10.13+ x86-64

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

Uploaded CPython 2.7mmacOS 10.12+ x86-64

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

Uploaded CPython 2.7mmacOS 10.11+ x86-64

ssh2_python-0.14.0-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.14.0.tar.gz.

File metadata

  • Download URL: ssh2-python-0.14.0.tar.gz
  • Upload date:
  • Size: 559.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ssh2-python-0.14.0.tar.gz
Algorithm Hash digest
SHA256 11aa6ad22899165da1ccc75f96c5a6e3f5de58451bdd0994e745448af8d40300
MD5 ee79d1582c385b094cd555c9cfef337c
BLAKE2b-256 30b70b0d980651512786f521b934906781a23854aeebcec11ff7b48e15312ec2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 6344bfd1b90c5b857551b9f580c7152e4eed1124b47f4c707177159e98579fdd
MD5 dbd710fc617edc76e752c731ffc3febc
BLAKE2b-256 ffd2633929563ebc15405edc204569150f82362a76dbc80ddc01a9c9865af4a1

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 1a434e3b4cd5cedc64435eeadba3927754a28ac08f6ca94bc557082b83077f8a
MD5 2bdef377eacf43f13f0658d00d187348
BLAKE2b-256 9768c55b39c599e75b80d6e71ee55df3b687b7e661e4706810cc79df4850195b

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0872eb65e0a60e598218ecf46ef1b3775ea985426bb8c22fa73489bc5109d1b3
MD5 998bc7450b4254dcc3a9cd120660b5c0
BLAKE2b-256 54034698ed3bdbee285ef3bdf6c283c6f2451da1c47fb214a1d1cdad18fa5bee

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 5c8a68b95fc10f0c16c310aca57698587ee01893d154a54f3cb1bf3042eef05d
MD5 a1ad1270222e0401d6322c88d2dcf896
BLAKE2b-256 ceaecf2d24aab063344604dae89ea8cab64df5b56ad84e51b382f7f58abca123

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 0d5bb6f093b5ffd7ae5095010b04c0f682cc50cf7ebaf0ede5985a11e2ffbda2
MD5 4746a98fb4e17ba4dd0de86693459600
BLAKE2b-256 1f3e3473cf8ea0ec5b0716e666205a70bc76410ba699d144b7d7dbb94e231501

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2e42df856ece7aa133c0a2804278c44a2c069fedcf3cf6fb750a50d01453a0b3
MD5 34165af39a4ef54369074bd6e2b8db8f
BLAKE2b-256 12af369487805fd835b6161204a677c3e18fef05cbfde58d05c1a601717054a4

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 5deb549a640870e4fc232763ca758d5540fc104e0f989e5e04125769c37dcc64
MD5 42726c5f176eeaaf5a168629b64a5921
BLAKE2b-256 a319aeec0fc3609195a6178cad88881b67dbbbbd12eb42ff0ffb5fe37735dff7

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 53a787025c05ced92ca5afcb9bf36716c1b8a75bd301531561680a37ad91d8dc
MD5 a909ecb9434789e50c0725dc1841191a
BLAKE2b-256 077f81149cb104567257ea699f4c871613a7fbbea7437f4698b0c87b3ddb91e2

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e3ec5bec287de653162338dec3211c3e10202d18e3210459cba38ccf783f5dee
MD5 7406d3e4abbb67ede8c1837ba1357a63
BLAKE2b-256 b64ecaf06858ef10b137290155787b3a886f0309f1b498cf6ddfb7e709e21405

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 326cba407b23f7eddf03b360892ff7d4350b61aade9179b3d2f8562a0dc06332
MD5 49d8c6208a11a262abfffed48580c776
BLAKE2b-256 a76117a7f0a54b7d80976fbe8e702c51d1696d174ffb459520b43bae88a074cf

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 23e6e0799d5ce8b042fcbd082713caf572c35eba2a1ebc016c57af7bed88c0d1
MD5 e54ee39b1748d2c9ceb1bedc55e7ee25
BLAKE2b-256 e3b5794b52b1da310a22cf44f65169e3f2702c9d176846defc5ecb1af8ee3d28

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 c53ad193abac65c80a6f6a340ac7fc303aaadc56d230a3fbd1fb463cc2a8c8ba
MD5 7d37dd72f4f2531ea4c127c006dd6878
BLAKE2b-256 bdc2360ad9ee0e54d1c9bd2765ffbfa64a160c155c1be9372aeb09f2f6ae8356

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 991191dbb19f58a4eff60e3b454c18eb3199f01409c15c595c4654f7f148a063
MD5 537ed10293f02072f073af4b2a69c167
BLAKE2b-256 335cfdcd2c323f1f037da07c26fbe74bf17ed7095ed88efe7687d2617820d69c

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27m-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5f783f2bc8af5ea693d74437e5d34943f1c58443e65024dc3650a314574f657e
MD5 53ba23aa972dd2d670cc16968106e5e2
BLAKE2b-256 8439bd443455c7952fe9b8c9e9683a12cef32a727b98409d2adf2ce5771c54f3

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3f1f0b772fa927ebe617a42ce3a2fe7df8588710e5a2e56df2cfd3212b5c935d
MD5 21725d2a479f90adf14194df3308065e
BLAKE2b-256 12dea72fb9d017b6afef1bcf80df4ca5e595b5c5398af57a435718c28ba6b8a8

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 1978896f760a64eda460003366371a3a69025aeef73889aefb79d52bd6023df7
MD5 18d9f850a30634826e9fadf33ae523b5
BLAKE2b-256 17827f6344f13c83f9f9f6ea4b3beace772a99a8405cb3c35dca85a85ade468d

See more details on using hashes here.

File details

Details for the file ssh2_python-0.14.0-cp27-cp27m-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for ssh2_python-0.14.0-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 3cc816e33a5146a7bd52f418247617f3dc16f50f4f060c5265d857356a6d809f
MD5 b1a2c5918bf68547cf5b3d0c91ca1d32
BLAKE2b-256 2c482b4cc773bc4ed148aca5a1c94ca19baf0415660327e2898963123759284c

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