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.post3.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.post3-cp36-cp36m-win_amd64.whl (538.2 kB view details)

Uploaded CPython 3.6mWindows x86-64

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

Uploaded CPython 3.6mWindows x86

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

Uploaded CPython 3.5mWindows x86-64

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

Uploaded CPython 3.5mWindows x86

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

Uploaded CPython 3.4mWindows x86-64

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

Uploaded CPython 3.4mWindows x86

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

Uploaded CPython 2.7mWindows x86-64

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

Uploaded CPython 2.7mWindows x86

File details

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

File metadata

File hashes

Hashes for ssh2-python-0.7.0.post3.tar.gz
Algorithm Hash digest
SHA256 259645c2f4564d08b1d01f789303197b40d5311fede95ec53f25d40a4fd69dcb
MD5 70fd4eb8ed309d91e5c818651c90a4e3
BLAKE2b-256 a9e465460076410c87047dedc6d07844193182fe6ae569828489d1fe38ecbbd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 2c30f7c437944952fd004b6746388468afaa21777bb6fc8f23b5b45cbf4c84a2
MD5 191e70320285667e9e95af2929074403
BLAKE2b-256 3da45948090868c22b083346abab3bd6c76f359c6ec3152d5348c9b973ef092e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 3b8d0bc5395d17b80c5c37c6d2d8d43b2fd7135a3fb819147d1b720145261ec9
MD5 40f7288d7b45f7febcf181da200fc3be
BLAKE2b-256 091f4c8c283c32f44477b3f9826c8b63319321430d6a5b8600dea501f25c22b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 796d5f59fa968cf049493caecf59b6695480635788ee0d8c7fb568b284ecddcd
MD5 b6361733a47ab87dd7ef7ea6987ac2cf
BLAKE2b-256 acc6d7a955623d2a63c7efcca1356c764cb4a5a0c018438e703fc759aac8233f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 08b4c88294abe531e3d1776660f55f1710fd67b4d4699cc6a885b04235410275
MD5 5b3e1f22abe5c10fe3d66f98436995f5
BLAKE2b-256 d64b531c4bcb08b0a9175a04e1d71508986e222fa19219b25a8e611ade67b671

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 33ae0d96c9c30301f30a6b2fda9f2b073104322867014845d98d9c9addccdf00
MD5 40d3c40eb4bbc7fd0c5cc5b634df6b41
BLAKE2b-256 413a8168bcf0668f0e6b4e49c2f7aaab836b99775120be51d411243474fbecd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 7704d570fb1cd2189a8fe6fb5aecac73cd0d316b5430e1bc08e876077d97ddee
MD5 6a276fba255d4e284ab868fff50fb160
BLAKE2b-256 79c1b988b156bfd62562534232873cc033def7ce10d5def62094c47828822790

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 e1a166717ea1f4f0408e1bb5ece2fa985ec0b6cd4942f9bf8251c4f314278805
MD5 f428388058c544a9c7f3ea233828d977
BLAKE2b-256 7aeed4c5078d6be79532876bdedbd82ee578c5e9ce4b2a7be40963d7ad67e2e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.7.0.post3-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 8c443302d77075b0c871fcb8696f52c06ef05b77b05c856f87a474f745b58b73
MD5 2714132097c840b8d0e7d814969c1993
BLAKE2b-256 6b88eee6f98ee427ea047fa50015c2e33e718e0d16dd7296c7e47c30a05e45b5

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