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

Currently the vast majority of the libssh2 API has been implemented with only few exceptions.

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

sftp = session.sftp_init()
with sftp.open(<remote file to read>, 0, 0) as remote_fh, \
        open(<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

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.7.0.post4.tar.gz (439.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.7.0.post4-cp36-cp36m-win_amd64.whl (538.2 kB view details)

Uploaded CPython 3.6mWindows x86-64

ssh2_python-0.7.0.post4-cp36-cp36m-win32.whl (461.5 kB view details)

Uploaded CPython 3.6mWindows x86

ssh2_python-0.7.0.post4-cp36-cp36m-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.6m

ssh2_python-0.7.0.post4-cp35-cp35m-win_amd64.whl (536.7 kB view details)

Uploaded CPython 3.5mWindows x86-64

ssh2_python-0.7.0.post4-cp35-cp35m-win32.whl (460.0 kB view details)

Uploaded CPython 3.5mWindows x86

ssh2_python-0.7.0.post4-cp35-cp35m-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.5m

ssh2_python-0.7.0.post4-cp34-cp34m-win_amd64.whl (515.8 kB view details)

Uploaded CPython 3.4mWindows x86-64

ssh2_python-0.7.0.post4-cp34-cp34m-win32.whl (460.1 kB view details)

Uploaded CPython 3.4mWindows x86

ssh2_python-0.7.0.post4-cp34-cp34m-manylinux1_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.4m

ssh2_python-0.7.0.post4-cp33-cp33m-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.3m

ssh2_python-0.7.0.post4-cp27-cp27mu-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 2.7mu

ssh2_python-0.7.0.post4-cp27-cp27m-win_amd64.whl (521.8 kB view details)

Uploaded CPython 2.7mWindows x86-64

ssh2_python-0.7.0.post4-cp27-cp27m-win32.whl (455.3 kB view details)

Uploaded CPython 2.7mWindows x86

ssh2_python-0.7.0.post4-cp27-cp27m-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 2.7m

ssh2_python-0.7.0.post4-cp27-cp27m-macosx_10_12_x86_64.whl (1.8 MB view details)

Uploaded CPython 2.7mmacOS 10.12+ x86-64

File details

Details for the file ssh2-python-0.7.0.post4.tar.gz.

File metadata

File hashes

Hashes for ssh2-python-0.7.0.post4.tar.gz
Algorithm Hash digest
SHA256 1e496b2e84bb3a7abe746649fa93f8a41e8614fd5c1c54292edcf09003d779b2
MD5 f7a2b1c1da25bcdee8d77064853bf619
BLAKE2b-256 9ab9ced04b0e2d01a5b2c3675a9aeb1d761eccdd5b9942e09cf03ef89428a76e

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 1076790b9827c6710f4089e0bf84151bfc7844b6c6ac168f886f463a6e2f63c6
MD5 6ea3e24ee0b441809bff3f07f1b60b77
BLAKE2b-256 a491d391d6ff4655c0333d0d2d3bc3b2b6a7ac78da05e648fdadf371a17d292d

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 bc8daf2a1a7247a589132fdd50047c2f3b971db61239b8639de26513ebec93b5
MD5 00ef610c59933665fbe550bcb035383c
BLAKE2b-256 3e2b77470a8d4e61b024455611328573dc538713c650c60ce0ce7e174d2782da

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4afae9778919d175cb21fb52432ab13223726efceaf708a025619aabeceb853c
MD5 99f8a1393489796ef7803bd4cb6efc41
BLAKE2b-256 1cce7574b2c79fbfaa438b260bcf0765876af2d12a98f9470fead29e6b938bc6

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 c0e3bfb7aa949144bfbf3f2931b9a948f802dee780265d11515ce64c5285e846
MD5 5be435b376f134e75412edbe3638e81a
BLAKE2b-256 9b8d09b90a0a809cfd87da521ec41e7438c5e317eec9759b1ef9fa062eb07323

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 ac38c681d2d89a7d6550d8e753ee0f11869f0feb9300231d97eed4501ac20bdc
MD5 7176dcdb689c06c839b5ce34c9491360
BLAKE2b-256 e4215e39912ee468c46b26f62f02423fc563b0c427f9279b99e14f1456115150

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 61e0c8de2dc4a7479fe35e60291c37debabeede9f6d3b5385c13401ed4eb4445
MD5 3193c68998231830380f881f2d4d22e4
BLAKE2b-256 8c66e2a4c6d5368c430d757a74ce7083e2b7a4a3002185d7da0da0f8aca90842

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 0fac6701a96c950e359c42d2a3288e20c95a7892b0b1e67d81b5605bb033585f
MD5 a3ca3ae20ebe9cd332ba80716cd7703c
BLAKE2b-256 359b96c262bd1164127cfc72ac6b02b7cff0b41d3c165643365adba126021ee4

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 409b05e70047ba70446b87cd6bda3ff2f65cbaea78e9c0bb2dec1229fc33ae4e
MD5 13aa541e828e9bdf2c90b29e2e7e7b49
BLAKE2b-256 35e5a9f61c167b97a7cb43965af012a28896b4ceace2cae66a2034c849fa9856

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b297532685d6e7b0ab5c5c6848ae6d1b8f3d02154037a4456df15f235405452c
MD5 4e31be2fa636052d6d7ab16bcc9a8f59
BLAKE2b-256 7506b12e09d39600a14abf0345d51cbc89829c8987b1a06597aafe3c63a53cc8

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp33-cp33m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6d008b774821c81d2a2875f7f48ac2f6208df233753407cdbd1e2778838f9bbf
MD5 c73a8070e4e9e2f85ed7e91d88e674cf
BLAKE2b-256 4b7f7ed57363ab8637d11136171ad0d67f1aa4a49148763c9fcabc60bbaeff53

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bdd1ad318dab1613badad508cb1b3303c2422d7452ab49cb39dfa15acbc29773
MD5 823b8b79bcb91220e2750cf3f5faab53
BLAKE2b-256 f08e3aa48911d9b3987b9b2dbc88cbe3b088af94519b0e2ee6939f88db6bfe17

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 b7365a0f1e331ba1db33bb0d1bc95a4d200d24cc5946849d88898f1f8d8168c5
MD5 2714b07241a3b5241786f301c4a2dc20
BLAKE2b-256 ae4592d42dc849c969de527d4b443106d8b2dcbc727813c57f44879fc5fe5c80

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 94ade22827506f7c34546622bfbb7e6a8c44bece9fb32dd8ab386a6ef7da60b3
MD5 2b52d63efa38b080d91f2811785c2bea
BLAKE2b-256 22cbf716ba6cd7d6138bb115d01072063da90a6c3086f438e69889e04056f413

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a35516a8587f14a2beb95dfcaef0378c6c00919dd6d87512ff89812f976fb740
MD5 a38da77ba8732bdf50db0e6462b24a7a
BLAKE2b-256 9410445d97b0042cdc2876b39890c8ff5fcb8a6fc7fcb73f713a8e727d6ac8d6

See more details on using hashes here.

File details

Details for the file ssh2_python-0.7.0.post4-cp27-cp27m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post4-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 79e36db189152ee20057fd18dfc63e350763f3b2e18d058a00f337d095e753dd
MD5 9660964d6bad0c8d0653dc76ae2fe8cd
BLAKE2b-256 c70a14b860f43e3b18d5ee79f53d8357fe906acf6ac6537366f09274a9422a75

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