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, with libssh2 and its required libraries included.

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

System packages are also available on the latest release page built on Centos/RedHat 6/7, Ubuntu 14.04/16.04, Debian 7/8 and Fedora 22/23/24.

System packages have no dependencies other than the libssh2 system library.

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

API Feature Set

At this time all of the libssh2 API has been implemented up to version 1.8.0.

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

  • Expose errors as Python exceptions where possible

  • 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. Channel output is always byte strings.

See Complete Example for a complete 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 intentially 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.11.0.post1.tar.gz (557.9 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.11.0.post1-cp36-cp36m-win_amd64.whl (616.4 kB view details)

Uploaded CPython 3.6mWindows x86-64

ssh2_python-0.11.0.post1-cp36-cp36m-win32.whl (528.3 kB view details)

Uploaded CPython 3.6mWindows x86

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

Uploaded CPython 3.6m

ssh2_python-0.11.0.post1-cp35-cp35m-win_amd64.whl (613.4 kB view details)

Uploaded CPython 3.5mWindows x86-64

ssh2_python-0.11.0.post1-cp35-cp35m-win32.whl (526.0 kB view details)

Uploaded CPython 3.5mWindows x86

ssh2_python-0.11.0.post1-cp35-cp35m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.5m

ssh2_python-0.11.0.post1-cp34-cp34m-win_amd64.whl (592.0 kB view details)

Uploaded CPython 3.4mWindows x86-64

ssh2_python-0.11.0.post1-cp34-cp34m-win32.whl (528.2 kB view details)

Uploaded CPython 3.4mWindows x86

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

Uploaded CPython 3.4m

ssh2_python-0.11.0.post1-cp33-cp33m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.3m

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

Uploaded CPython 2.7mu

ssh2_python-0.11.0.post1-cp27-cp27m-win_amd64.whl (598.3 kB view details)

Uploaded CPython 2.7mWindows x86-64

ssh2_python-0.11.0.post1-cp27-cp27m-win32.whl (521.9 kB view details)

Uploaded CPython 2.7mWindows x86

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

Uploaded CPython 2.7m

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

Uploaded CPython 2.7mmacOS 10.12+ x86-64

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

Uploaded CPython 2.7mmacOS 10.11+ x86-64

ssh2_python-0.11.0.post1-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.11.0.post1.tar.gz.

File metadata

File hashes

Hashes for ssh2-python-0.11.0.post1.tar.gz
Algorithm Hash digest
SHA256 b624cf0f2a00bedcd2ce9e5348aa299ad946f6c56f3ea7d56d804e848e2e8ce9
MD5 15ed946ab78330db9f83bdd6f0dea006
BLAKE2b-256 66d98c77a8864f5e13f11d76100fa7f5827e34f6461e2a9646dcc257b145a505

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 9be2d82a32383eb5d4a83bf7e17b7f0f9cf7d168bc71fa4fb6b6aec9049b90d5
MD5 ef2c79980247d35a67d21064e18e2e3e
BLAKE2b-256 5d359cfba0eab638a46b28d40d77c5b52aaf2ff811c4ab36f2c76bfe6a982718

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 ec511e255fac05186ccf4c4721f98a763e4327798578f5eb2a1df3e5fe1c86d8
MD5 678480f9ebac6a92a02d41aa9d91b868
BLAKE2b-256 70eb6b65a0105a0f0bf813694326554e994f70b52f7660e9091767dc911daf22

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f2eeb34b8032ae3d41d8aa70af539fa49f35c2d99ffb6711dca21830f8d2cf6e
MD5 92335fdd4467fbe61e145d66dae4fb9d
BLAKE2b-256 35b54516d61b691c6105904465d029322f8eb19061401998c2637611a8647138

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 09e9e10fe1082298568b403cc2d780e85e6ebde1f6763248c1529c3015f56779
MD5 cc54c76c003a6d72a4990fdd3ffddd14
BLAKE2b-256 bad78777bc36e587af16ccb235d17252f24ef28761fbb718ed949af9745066fa

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 fbc51586d2a07b90ab3971db116e8c4ea2e880f20d780a5f503cec9acd67d6e6
MD5 8ff115437df148d1404ba23e2a6738af
BLAKE2b-256 9db32c95c00ee5902ce4f09a46ae190e005baf712b7fd886dfc802a0dff88404

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 618c9037151a19ec52c283cdd0756b619016e079c364faa7e07a007e84ca05bc
MD5 91514277ff4a0455fe67dc7e09e01b49
BLAKE2b-256 6566e3f8a458c08e42a700d78b5c48c657a67eb8adc9f64911f7e4a17d047261

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 412c0433bf0be02458ede9f92ce8184533f56f2f7be1a1e7049b7858798e72c6
MD5 5dbb2e9396c68df5c25b196bda3ead8f
BLAKE2b-256 d6531b14c6c92e7e849a0554f9f2fbf2198c864cdf723e3eaf05da41a1a9685f

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 c0b9ca358906aca477581e8250634e9f0909a16274a5a736013744fce01f0e75
MD5 506ca43cb167ac1906d33224888c6ec5
BLAKE2b-256 b10ca3ea605211971bad2a8b884290e7b08c0093bbd4b73f8ca28f606fa90aab

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3867c97ff77a77ea79f2aee7cd9fa2a1a347d4f27bb18c98fea8f6bf8b11287b
MD5 15e305ac30c1e9e058ab2ca996175c2d
BLAKE2b-256 f8610a1482855b36c8e96243ddd748704ff036add760d25954a5b251c2dc4ec2

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp33-cp33m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fd783a0e8bb3aee77a561c39b4dc0e7cc79033cc8037a25d393396078e4c6d95
MD5 282219314f4a1d02742062520e609846
BLAKE2b-256 a722f454d23db26dfb31d10bb78b07e9cf3accdde2604b4fb5c95e882e268d11

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 976138fcb7fdda8d6044324cd9ab45b43af2328ce2be76f19852402892b5b6f7
MD5 2bd52c08fa87a2232a425db6494b502b
BLAKE2b-256 1d852ebe01e6e680f3894ceb953deb16f190f91f1c8ba2947f76f5d90f630c6a

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 f02c4f99289ce757dd34307f626ec84e9f61b7c32d23cff884f63b5406b317f0
MD5 1e2e708e1b12b7a5e041fcfd06749f16
BLAKE2b-256 1473b5fe8ba0183711e7ea485b272304301fba10329d941f09775b2b6e414a96

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 2bd971614d96857a2da352f3d5be9a3b841edaca5a262e04aeb094ac0f698023
MD5 f731f997f2fdec6603bee8c8a7c05343
BLAKE2b-256 ec9cbdba7d101bf1058d2be9352e1e0eb9a4909673f0ff2b6f7900e7531d04a3

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ebc9b39435f31eed39a2d33a454e3e8a1aa06970626522619a825371ee4c3fc9
MD5 cc27a22aab1f6731377ec241f7f66415
BLAKE2b-256 ec50e0fd9dcab17e03e1ccacde3972383eba8ed3cf291a1875a360ed1dbb363d

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp27-cp27m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1b70a23bd72a63472444eb0b7f4fb794b5a6545aa1d8949a195bacb1e8f84692
MD5 7c4ff5a82bbcbcbc21fe259598dad845
BLAKE2b-256 be766cba5af4432d9d2f99588c12e3497ee425ff9fbd7741e4923a37b9bb2d6a

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp27-cp27m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 7ae94c23ff6082f1d5ac4921071824d2a38eefb77de1881e3da83ffac2725bb0
MD5 a33a58841e194b95d491905d7eb80307
BLAKE2b-256 03cb88dc16896a4a6ec962c2e3c61c251bf17c2845e24591107f6a29d25667a7

See more details on using hashes here.

File details

Details for the file ssh2_python-0.11.0.post1-cp27-cp27m-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for ssh2_python-0.11.0.post1-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 7e9f652fb548bdb819d79ca9d0011f8c924780a5b8445500c020956e3b76bc68
MD5 d0c1651e80c1d6e3b044234cbe4c07e0
BLAKE2b-256 dc8de231ce2dc73686b3995d5b7b8b0ccc784dc23b892c3f6ca75d536e5d4359

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