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://circleci.com/gh/ParallelSSH/ssh2-python/tree/master.svg?style=svg 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.

Who Should Use This

Developers of bespoke SSH clients.

Who Should Not Use This

Developers looking for ready made SSH clients.

This library is not an SSH client.

Developers looking for high level easy to use clients based on this library should use parallel-ssh. It provides both single and parallel clients.

This library provides bindings to libssh2 and its API closely matches libssh2.

If the examples seem long, this is not the right library. Use parallel-ssh.

API Feature Set

At this time all of the libssh2 API has been implemented up to the libssh2 version in the repository. Please report any missing implementation.

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

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

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

Uploaded CPython 3.8Windows x86-64

ssh2_python-0.25.0-cp38-cp38-manylinux2010_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

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

Uploaded CPython 3.7mWindows x86-64

ssh2_python-0.25.0-cp37-cp37m-manylinux2010_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.25.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.25.0-cp36-cp36m-win_amd64.whl (9.5 MB view details)

Uploaded CPython 3.6mWindows x86-64

ssh2_python-0.25.0-cp36-cp36m-manylinux2010_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.25.0-cp35-cp35m-manylinux2010_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.5mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.25.0-cp27-cp27mu-manylinux2010_x86_64.whl (3.9 MB view details)

Uploaded CPython 2.7mumanylinux: glibc 2.12+ x86-64

ssh2_python-0.25.0-cp27-cp27m-manylinux2010_x86_64.whl (3.9 MB view details)

Uploaded CPython 2.7mmanylinux: glibc 2.12+ x86-64

File details

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

File metadata

  • Download URL: ssh2-python-0.25.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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2-python-0.25.0.tar.gz
Algorithm Hash digest
SHA256 23c333ee4b8135ff03e405ceb46abbe8aa4ce5b6cc63380b43fa243f7552cbb6
MD5 8381ea27046c56c189a4f7dd34da3ce3
BLAKE2b-256 d764ed407bd835f5fc9d5fb43b3ae47d93f88bc1193b28184a304515b0de0daa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.25.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 23c7d03575203721ea2c060d80242ddef948a3a2b4426aee1f1e691200e1c118
MD5 dbf32ef3198c164c4880ebef192e7982
BLAKE2b-256 3ae5ade0c28d80c67f20f729f5272ff17d5d42eca4c3ab0bc8146b8592307f1e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.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.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.25.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 800082308c6f2eaa188f5d8e92c8865c74bc2a4da6f7b098cd1c042ea50f044a
MD5 1c10ec57dc3b0b2283897ae1d0000c34
BLAKE2b-256 89221eb9043c11b90362faa6d47a168f0713cf2bb1d21ab939a83485a7071085

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.3 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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.25.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 bb956257f87fdc8bf2e6e30470c3a4c8505b2e7f5584e1e24a182d43cf9de7bd
MD5 b3fd0b36cb14240e1f38c87c7307adff
BLAKE2b-256 751113c81b4a48fefdf0a9af0ee81975b8efc9f041161bfe8c6e2c4c32e97f50

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.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.1 requests/2.25.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.4

File hashes

Hashes for ssh2_python-0.25.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d36afa0b6bed97fb9a0e37752bb80b2e36237e8ad897d868e6a3dbd1ca961a87
MD5 8446416034cea9940f8e294cf50a126a
BLAKE2b-256 4cdfdcbf9165a218709d356687b2dc4fc079557f3e2cf7260178a8988c335565

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.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.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.25.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 2c946de1f8937fc51c8def1fafef10ea76a324799ae1a5e046db567e39d7066e
MD5 11fa1a7ea8eeaa2f229f941d9d05772c
BLAKE2b-256 d8df9e5f15af7ba41dddf058059a73e1d0a53fa78b7a8d8ac985fb4922bdd9ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.1 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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.25.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 435edcb9cc56bcd0ff5aaf791228d190d235a46bfeea51f1b33ed80d6d99e704
MD5 1f114da781d63a3df0b5111ee54c4d19
BLAKE2b-256 66ef768b38d38b003b17363f59a974f1bea88771029821ddf00071af72731324

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.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.1 requests/2.25.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.4

File hashes

Hashes for ssh2_python-0.25.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 451b7358accfa285679977a5d8d439519a6f790937019379d38fd6bce9c19bad
MD5 730201ac9e12c98019293005375f0535
BLAKE2b-256 80b49dfa815d87ca03c2ae02c237216c37c02e258e655315c0211f41d62ebdbd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.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.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.25.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 22d06510bfa42e913826d06bd63d0b486e46c2187e1558ab4f51229a2a66c881
MD5 d1dbc4344d99246d9adcf53bc0e67e11
BLAKE2b-256 08bf7d2a94ac04fd212ee95dbc618837566aa64daa3414a20242b5d17080532f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.0-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.1 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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.25.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 e48dd075f5bd184ebc6f143cc92546eba06edd79b7017ca1af0c5de38ca1d45d
MD5 134a8299e84973fa1258bf16152e9bdd
BLAKE2b-256 8a9ad0101792e62b28a8106cccd0b1893b34cd507d9af1e4a4df70bf7c6691f4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.0-cp35-cp35m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.1 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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.25.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 a17774eb63986b502edbeb247788750dae85541b21cd1146d0b0d3bcba5fb8e0
MD5 f48e686639e26a522e9b45b9aa62b599
BLAKE2b-256 d70867305d43725016edad31dd57d618c6fb66af83b9a6401207ecefe50824f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.0-cp27-cp27mu-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.9 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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.25.0-cp27-cp27mu-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8bf0ac3a6bac051f461573e387e7bf937a34277769744c7e3e1fe3887dc2d21a
MD5 64e8e7c2c8271742030b871daa50a1f2
BLAKE2b-256 efe0dc778ce9eabb1f6f8e72f681d4f208adf74b0574ddcfb7c7729d08005975

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.25.0-cp27-cp27m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.9 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.1 requests/2.25.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.25.0-cp27-cp27m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 439f4aeae666650e024202bdc6795e28d89fd39cb1dc7b2571aa4400246f5f33
MD5 2b6abb55f62b3d9904cb30ce45b5fde9
BLAKE2b-256 9c2ca26643c13822bdac6077f7dcbc5053cb29e670f0085ee660d51d5e9700f9

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