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

Uploaded CPython 3.7m

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

Uploaded CPython 3.6mWindows x86-64

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

Uploaded CPython 3.6mWindows x86

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.5mWindows x86-64

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

Uploaded CPython 3.5mWindows x86

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.4mWindows x86-64

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

Uploaded CPython 3.4mWindows x86

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

Uploaded CPython 3.4m

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

Uploaded CPython 2.7mu

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

Uploaded CPython 2.7mWindows x86-64

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

Uploaded CPython 2.7mWindows x86

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

Uploaded CPython 2.7m

ssh2_python-0.15.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.15.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.15.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.15.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.15.0.tar.gz.

File metadata

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

File hashes

Hashes for ssh2-python-0.15.0.tar.gz
Algorithm Hash digest
SHA256 df6760459611520e510984e13946a0fb84c399783a5470e712750dc2ded8e234
MD5 a562aa94e011afbf2bad8f4562610a4c
BLAKE2b-256 7a58302c82d127636ec538e4d1ab2ac9b209a4ddd4e4dcc2387f5b9368e66bfa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0bb22511f473e1f960356ebfa4f5d8cb66fbfe347bf1a5b90cc9f76d2840d170
MD5 28de2bf72aef305f3391cafdc7c0f429
BLAKE2b-256 b9c1b3adbe7e90978bbdb3e91d56868335e45b295829191bfa98ec1243949ba6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 7aef7295c6eb54bef1d9a93a2925aafc30377cb27a5eb3419c22793de81c61d9
MD5 d5984b3d7dbdfef1567d93a87bd99eab
BLAKE2b-256 ff582e6ac5e8307ea4983da63d8ebc1e4928e258a8dbfae826de3e3243526f27

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 b44d389f52360ee0aee2dd4257e6e8c4fa581e48b61b13a84b15fc33b3497e69
MD5 4e80bb7bec9adc0bed384f56a694084a
BLAKE2b-256 0f932233aecc93fec205567c72590092b2c3ae7d382e5b7371cf2acbc988b2d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 12819b8b1e8f993738649953825eca36163545c8c696edf8034ad6ed4793cfc6
MD5 93e0eb2bde8f3ae125df099f12ac7668
BLAKE2b-256 9864388039094d847d35d8d3245c3f31c9e7aa5764f586e1387e72741d6d0321

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 cc93a65061dff69b2f162576f1f764533f3cc8c9679136a24dc10f339127ca35
MD5 0555d34ccdc06341b9dde34462954988
BLAKE2b-256 528442cc97a7e4a5e21a25c5eebcd21abb58035381452d74ca6bc98ccbe7c36e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 68b99cf682a425719e205d2ae96c3afa603756575975e51a9f1bbffa32aa7abb
MD5 04e3201516840df360b434d547a33f0e
BLAKE2b-256 f1a6161e83e6337cd91066cd7c6631ea100e29c7666222ff5dd996862e9faa2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b4ec308548b27f8672f5507c23d36fe52210ddc704cf0fc4832aa834fdc6a3f6
MD5 34937a71f21028bd66a938c713b27252
BLAKE2b-256 3947bc6b805d33bbf8ee59eb01e774f92097ee345674e8161f214c3120cb15d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 8d3db95b56a29a80897e005e99afbbbaa5c86d280675b15f2d8e2ecabcd4612b
MD5 2dd7d7c4889a51c71e03ad2f19dc175b
BLAKE2b-256 0cf4e7ac0ac65bb59d86b169d24894601493a14a43314884beca397e75bec5fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 51520c46cbbcbdc030d9dafd10821da7f7350a93319ef92a80bf29ff5012fc90
MD5 cc1d8305ca1d0d4442f9ef8bedd06431
BLAKE2b-256 02e358ac9f574281de78cc06b61b6a9786b941ecece1059fe4514f51a6ecf5cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6109153bf210f7e9b0a92c64f1d025676d11ec72de49493d85027dfd7a8b11b1
MD5 14423111e646f43e741cc5e3d1e1de7e
BLAKE2b-256 21cf17796edfa7da8f7a411e52a25c3bbc4d49b0ef2130e3d8978335ec01efe4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e4495f3d50cbd9beccb1426922460a124b8b6d7ef797c734e573d43aa244bcda
MD5 d5f811701a2af1c0de5d893dade177c3
BLAKE2b-256 1de57fba261c6832c0b7ce344152a68df3d53c3c6e2204475486085648b44b61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 1188108b046b26fab6d3fd2f76bb421e1a0e48cab8e57f862b82942ba360b385
MD5 7c8e24d713e04e58e1862cf50e09fc9e
BLAKE2b-256 22fd35c9266e77bd273c2a7275f66b9d16325d4a62907064fa6864608b572ee7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 b58d99cd0a89dec8e22227228d86fcbbe4b43b1a3303f7b5d18f3801775a7f22
MD5 3a70f9d5198407dc69d89b43e8c96908
BLAKE2b-256 713371f7617a7bcf505366d06c5a54ffaae3cc761b3f0b0db5adf96185b6320b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9171973696849db4b031125268324a173ae8e6c3bf1f6512d0b7fbe039a88749
MD5 b7621f8b77f0e999b99d5a4af3e32340
BLAKE2b-256 b8aeae81cda9c637b6d8f880f74927085ed3fe89ebac20830390270f154aabec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e6c27aeae219d2ae2ca40d8e4a45aaeca17e62ad9d9b96480dfaa2e51fc6582f
MD5 6ef78a5753a5d07eda5b665371210da7
BLAKE2b-256 fc1347a5f6750f338ef4898efc07dbfde03d741b0b0fb9aa1f7ec63ba18ad27a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9575dec94ba48e18ba05899883cb8343da463f06fde17709d45588b39f8c32a5
MD5 7fb777e4e883e8c74ddf9438fe22906f
BLAKE2b-256 b2ce22d379c986deee2e7d6ad0c1077d8b6cd39fb2241293d6dd74585b007bb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 32d87181894322f139266b9f9e7d1c72620cface1502d764afe662952cdef34b
MD5 a51819750ce0f786b8b1d1c1f268ebae
BLAKE2b-256 1418dd120f0af56469401078b01273c7400e4ed031ba05bec8ce793f5589893b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.15.0-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 bf82e1c0b46234ad79e108c3a74974772c80c970ffe284bb7dc4b8ec948d1f67
MD5 42bd0b918e8be9bb727d10b6537579af
BLAKE2b-256 0ac566cb23a4a274d58aab7c9a826695ea62d644c7e0fc2f7927e34ddfec1274

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