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

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.

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. 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.22.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.22.0-cp39-cp39-manylinux2010_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

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

Uploaded CPython 3.8Windows x86-64

ssh2_python-0.22.0-cp38-cp38-manylinux2010_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

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

Uploaded CPython 3.7mWindows x86-64

ssh2_python-0.22.0-cp37-cp37m-manylinux2010_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

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

Uploaded CPython 3.6mWindows x86-64

ssh2_python-0.22.0-cp36-cp36m-manylinux2010_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.22.0-cp35-cp35m-manylinux2010_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.5mmanylinux: glibc 2.12+ x86-64

ssh2_python-0.22.0-cp27-cp27mu-manylinux2010_x86_64.whl (3.8 MB view details)

Uploaded CPython 2.7mumanylinux: glibc 2.12+ x86-64

ssh2_python-0.22.0-cp27-cp27m-manylinux2010_x86_64.whl (3.8 MB view details)

Uploaded CPython 2.7mmanylinux: glibc 2.12+ x86-64

File details

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

File metadata

  • Download URL: ssh2-python-0.22.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2-python-0.22.0.tar.gz
Algorithm Hash digest
SHA256 8f82d6e35a4ec9c57b89b69294113842dfa818f8cf1257c5e8bc87d12dcdfbe5
MD5 62ba1c3eeb425f1866279f29474f7e80
BLAKE2b-256 37f463b63af7609b6518f48f25abcbb82de8230b0abd171344e4e11748b2af67

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.0-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.1 MB
  • Tags: CPython 3.9, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.22.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b873074e5c7fa39d338576694f8bcdb586691bf9d61a3777f32fb0cc2201c646
MD5 fa461dd9cd4313e8ff1b110f424ce92a
BLAKE2b-256 bb985bec59ae367a3b54c65fe708c6eb7cfcfdabe1c574b99396ff7f3fd29118

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.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.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.22.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0a07e9f3cc9eac4664d65fce549acd6ec948abd0be667134991e1f8925a19448
MD5 d69edbef92bd555421c320dfbc9fb864
BLAKE2b-256 0da6699dcb8b9d0659737bf5c621e6f1954760eb11466102fee27de09948b2c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.22.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 fc8b5fbae7e2e08391faa4759c68f88a047f9f0f07c46b1c976ad026f805cc3f
MD5 2d276d6ab0bfacb89d410833da22d00c
BLAKE2b-256 525597d3387b47865819b21da59dff6d9d237a6b3c80e00e01eb92af8f98763e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.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.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.4

File hashes

Hashes for ssh2_python-0.22.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 03d84d9d2f255544d881b413bfb8aaf1efc8960827779bd603cf3261f9fd40bf
MD5 6dc47da6f131ea1a861c394da3461def
BLAKE2b-256 c8073baf34626501bd59f757eb5553e801ed40cb8ed40775aa2c875efb7bd885

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.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.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.22.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 8aede8a4bd4263e095016c4cdb7aa3b41dfdf7d3ad2fa00696fce5828234fc98
MD5 dd751ae2ec22062f14d6124476a7c5b5
BLAKE2b-256 b915db440cbcbd5bab4015fb2f298efba10175eeee638822e5b99d0d3d4e6af1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.0 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.22.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 13f8c0e344707e8972bbe591b0a0dca70fd3413c30de0a355e53c0e2498cd6d8
MD5 62274cdda8e0daa3805104649909bee5
BLAKE2b-256 1a453717dc9670a634008842ca96d1085e17fb2a488067097ae47deb6849e10c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.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.5.0.1 requests/2.24.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.4

File hashes

Hashes for ssh2_python-0.22.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 fc1f6bbe3e7f45099d767e28d92c596531764d084f163090d4d88738398747ea
MD5 748f0c9f4668cbf3a94411d0229612d5
BLAKE2b-256 d6c4ee4615efed619640faa9b10fb20d8edfaf31b456dd49ef03baa4bb847d3e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.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.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

File hashes

Hashes for ssh2_python-0.22.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 6e6c63fa492a76920239356b3f7580250a4316a72a0a2930ab69967bd8896c63
MD5 5515a707c7f34125284ac5716f77a92f
BLAKE2b-256 4f074e57c5d4f0450608b01bb2435ea3b475247e3e5ebc22484657a4a5fdbbbc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.0-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.0 MB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.22.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 600935ca29d7298599da63815de3f24f43df0cc9f49ff855870f0c218e9a47d6
MD5 1406b8f21a3e0cccba2d8979c02fff5d
BLAKE2b-256 cf425bbfc288c5539755b5644cf592aa573826ad9d3eb742c748a15860aa7d06

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.0-cp35-cp35m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 4.0 MB
  • Tags: CPython 3.5m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.22.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d54853824b38d64b4f602e51639fa47a204149f9d270c8a59032cd60d31c08b9
MD5 a44e0e4796b167619a4ba052f5dd3b61
BLAKE2b-256 0f15b6a7b189120e09719a52698ede7d5da8dbe18497339b1e6b4ba7e0ab04f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.0-cp27-cp27mu-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 2.7mu, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.22.0-cp27-cp27mu-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d4e5cfee5fd15075813031bea2f0e927d4d08219991f930efbe8e3e947724a43
MD5 9ca0f3431de8fa2770beaa529a5e15be
BLAKE2b-256 3cc6bdab633dd607303653790c757f644ab532c2c81155ab858b539d243c3b12

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh2_python-0.22.0-cp27-cp27m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 2.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/20.10.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/2.7.12

File hashes

Hashes for ssh2_python-0.22.0-cp27-cp27m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 cc660facce18ed2c3d7f2ea07909b75e24d5c66f2d5e8fadb3f8ea418e831f92
MD5 269fb5df19a72dfc71cf3b5ac6811cdb
BLAKE2b-256 278764b9419141d9bced3fc3c32ed3e3733c93180870d15a2dfc6745a7ce616f

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