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

Installation

System packages are available on the latest releases 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.

Binary wheel packages are also provided for Linux, OSX and Windows, all Python versions, with libssh2 and its required libraries included.

Wheel packages have no dependencies.

pip install ssh2-python

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.

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.

Native Code Extension 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 that it uses native code prodigiously

  • Object oriented - memory freed automatically and safely as objects expire

  • Use Python semantics where applicable, such as iterator support for reading from SFTP file handles

  • Expose errors as Python exceptions where possible

  • Provide access to libssh2 error code definitions

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.5.3.tar.gz (393.8 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.5.3-cp36-cp36m-win_amd64.whl (523.9 kB view details)

Uploaded CPython 3.6mWindows x86-64

ssh2_python-0.5.3-cp36-cp36m-win32.whl (451.6 kB view details)

Uploaded CPython 3.6mWindows x86

ssh2_python-0.5.3-cp36-cp36m-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.6m

ssh2_python-0.5.3-cp35-cp35m-win_amd64.whl (523.6 kB view details)

Uploaded CPython 3.5mWindows x86-64

ssh2_python-0.5.3-cp35-cp35m-win32.whl (451.2 kB view details)

Uploaded CPython 3.5mWindows x86

ssh2_python-0.5.3-cp35-cp35m-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.5m

ssh2_python-0.5.3-cp34-cp34m-win_amd64.whl (501.6 kB view details)

Uploaded CPython 3.4mWindows x86-64

ssh2_python-0.5.3-cp34-cp34m-win32.whl (449.8 kB view details)

Uploaded CPython 3.4mWindows x86

ssh2_python-0.5.3-cp34-cp34m-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.4m

ssh2_python-0.5.3-cp33-cp33m-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.3m

ssh2_python-0.5.3-cp27-cp27mu-manylinux1_x86_64.whl (2.6 MB view details)

Uploaded CPython 2.7mu

ssh2_python-0.5.3-cp27-cp27m-win_amd64.whl (505.2 kB view details)

Uploaded CPython 2.7mWindows x86-64

ssh2_python-0.5.3-cp27-cp27m-win32.whl (446.1 kB view details)

Uploaded CPython 2.7mWindows x86

ssh2_python-0.5.3-cp27-cp27m-manylinux1_x86_64.whl (2.6 MB view details)

Uploaded CPython 2.7m

ssh2_python-0.5.3-cp27-cp27m-macosx_10_11_x86_64.whl (1.8 MB view details)

Uploaded CPython 2.7mmacOS 10.11+ x86-64

ssh2_python-0.5.3-cp26-cp26mu-manylinux1_x86_64.whl (2.6 MB view details)

Uploaded CPython 2.6mu

ssh2_python-0.5.3-cp26-cp26m-manylinux1_x86_64.whl (2.6 MB view details)

Uploaded CPython 2.6m

File details

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

File metadata

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

File hashes

Hashes for ssh2-python-0.5.3.tar.gz
Algorithm Hash digest
SHA256 2842da1447d48b11cb2c9b9dec7740549e7b76adf724314c701fada9ad0a0158
MD5 64f550e71eca277829edab88bb1fab0b
BLAKE2b-256 7643db642c9789e057a813afcaf4ade7af09085c567dd26d36f773f9b904823f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 ecd43193418abcc1a363c477bf871f8dce6fed04a1908670a6d216de4e23271e
MD5 20eddb3c25b6d3a75cb2d574d753b3c6
BLAKE2b-256 3de4b4c7b7eca12bd175a834dfe4d77c8fe35b19183c19051745bc6807dfbd23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 ddd3e4410d265aa2dfa9cf657702afc099bf20e5211a7664ec798b1bde13ea89
MD5 3c876ab93adb6a444c8fb99380750bb4
BLAKE2b-256 2d58ad515c3ab491be668a30cc1cbc71b99336ca94226ca577950067a0930549

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4c389a6614d17f892f208fb6fd3a1b6b9b1e5db97d7e2e6f781c5fe15dec53e8
MD5 86a01fc5d3c916f5f9e751fd9860aa9b
BLAKE2b-256 3e964a72d21354e554b27aa52b5e25a6cd4b7a7f8ae46cb096ac0efa4249e671

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 1cdace66e1f8a9ff48536c806c9098b2688eb2bf0d5885abe9cc46eb3f04e7a2
MD5 f2f32cf6f0f3ecbf7c50257faefe3622
BLAKE2b-256 97c122262c688d3e198d0a769e32a9b381f566b3e579c84372d4641f77fc7396

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 8303414a2721ce2a7f141aaa765026b339e7c8211f40030e79f9ee10ded3b5e8
MD5 f13a1183334c70f0df5d646cd659f5f0
BLAKE2b-256 67a46703835571aecc795d98058d0ff17642e1bf10f26b9b7dd16e705f0ff898

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dbb32f1a64bc539057d4b7a752db1b63aa49e2cad477d038da290c1b87842bda
MD5 6d8151cfd8ce78bb365cae8241d53c62
BLAKE2b-256 36eb67d2280e78793dc410ab7b58a36f904af5d349f3863b30b6da48e925f94b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 07ff3422714317a52ccc2ff74db34e7e272d322be4af1b81c6001c94583b8a91
MD5 618e0637c07237ffa33d4fddd0e67c0e
BLAKE2b-256 d483bcd370117aeae1a5599335adabbc521f734c27dd51ca4cf963cf8c21112c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 6dc67f5919d89c3b5de67d44233cc25725884217c1b8e66d1549fd645de59942
MD5 866d4f7655e4f0d07418237b46016b33
BLAKE2b-256 440df81bfd3ff0ef25d06699cb69822ff9f80ecc8523c8341596a61eb778f0c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e70c980e5030abc0755c85db81f4126602b1694d6860faee2d3f0c25f5454509
MD5 81ce30a8202f42e169bd07f3ea55e937
BLAKE2b-256 c0ac212a9b516fb055cca7e80f9f1bd42c6fb9dece3bf63fe4126ce49056c380

See more details on using hashes here.

File details

Details for the file ssh2_python-0.5.3-cp33-cp33m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f7d65add9a128febc8a41e39ee681a524542da0f38091be49699e8c0fc38ca44
MD5 065ff1a6da8327f53eae2ab935058b09
BLAKE2b-256 b0c38fcc638593f23956358e71cff4b793a146d4618507039976d7e15cb7e64e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8f9d9406941344e19d198c095f3608145ae0eabb4ca251946aa381f5f75ffb82
MD5 3ab670ada5fb33d338dd09cd58023f52
BLAKE2b-256 e30479856077a963ceedfce1b8c69154960a770f94d269ff348d85dad4ffc20c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 f620bb165503261d870840ec411a43babc2dd5bb432e87a9c3bdf1e0859a0d4d
MD5 1b32c379a18da12453d04ab99300bb50
BLAKE2b-256 d3d102f126edc0017463eea3181a97c66830da0aff8545cc5022456847e1c15b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 b95f2c24c24d09e756bba8dbca087c36f01c98d814532c33af2d10cc6c29e373
MD5 7fe178957a036021bced8ce3ae231e36
BLAKE2b-256 0e42bee5d1d4eb8d883f7ac8155151a374c08f849acbdc1eded79f30406ad25a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8bb14316b66e9561852ac909bc5951562be01da505b40beafcac4b1b33122afd
MD5 50d03fe343921732add4f4227c62143e
BLAKE2b-256 c5e19475de2aaaf8d9ab31f1eb8a829fa87f4710cfc2a4751b34132612b21485

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 6ed18cd2ce2a26d63baf28e6172aee8f624bc247696ef91d16fa1f0060fe62d6
MD5 1d531c83ad2baed46067766a691fe6aa
BLAKE2b-256 937f1d118d9d089a3700f85042e8c3b0ff5ff33777e12eb3caa52d03b1c1589c

See more details on using hashes here.

File details

Details for the file ssh2_python-0.5.3-cp26-cp26mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp26-cp26mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7eb6db083a5de2db1dbfc849b8d53c1ca2aa57c63976910985a91347180c9022
MD5 25b41cbfb0f48c679ad52212c470d8cf
BLAKE2b-256 49f7fdb99d186fd6d16964cd7cfc3a7a82ee816db1696eb9044ac60b9c804d92

See more details on using hashes here.

File details

Details for the file ssh2_python-0.5.3-cp26-cp26m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python-0.5.3-cp26-cp26m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c07eb450976f69cfe07e93f4e29de9a1d2c607ca598aff640e51b17c6952208d
MD5 0251596c27dddd6767b3abe79bd3827c
BLAKE2b-256 446efd1291fc9fcb39d9e11d45cb2b1ff23c24fa7ab5ff283029f0e0968b619b

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