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

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.

The library has no dependencies other than libssh2.

Installation from Source

Install libssh2 and Python header files.

Ubuntu

apt-get install libssh2-1-dev python-dev
pip install ssh2-python

RedHat

yum install libssh2-devel python-devel
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.3rc1.tar.gz (395.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.5.3rc1-cp36-cp36m-win_amd64.whl (524.3 kB view details)

Uploaded CPython 3.6mWindows x86-64

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

Uploaded CPython 3.6mWindows x86

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.5mWindows x86-64

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

Uploaded CPython 3.5mWindows x86

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.4mWindows x86-64

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

Uploaded CPython 3.4mWindows x86

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

Uploaded CPython 3.4m

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

Uploaded CPython 3.3m

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

Uploaded CPython 2.7mu

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

Uploaded CPython 2.7mWindows x86-64

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

Uploaded CPython 2.7mWindows x86

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

Uploaded CPython 2.7m

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

Uploaded CPython 2.6mu

ssh2_python-0.5.3rc1-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.3rc1.tar.gz.

File metadata

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

File hashes

Hashes for ssh2-python-0.5.3rc1.tar.gz
Algorithm Hash digest
SHA256 d7899254c8eb6c4511ea98d1b41b16c7ccd4c622b23df7637ff7b15e226872e3
MD5 820c5d47dbcc7445181d762325424c1e
BLAKE2b-256 f9b39b804bcf6a6a2f1cc483277c014ea4c33031b1c7ddcec2d25e12357d3cb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 82b15cc68d16b4adb18bf47166d1adc3bda3dd76f392632193bb7a4e6a68a62b
MD5 7a65a897ad8359dffda9736a15aa33b4
BLAKE2b-256 5456bacfe4669e1f3134badcabd393b04917269c7a5983067713937d3cdce078

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 1baf51c1ab3c2a987c53aa3ca8f40bea5b0d2bbb902bee838fef626199b5b82a
MD5 539e9646cd2331ad6acc7eba82a7190d
BLAKE2b-256 05bcd3d8cf4729381aec47b756991fa82498f3e073c923bb2f1ef2dfd0270b5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 badd140d6bf0eb1bc5b4b93c0b6de25318d4bd2135b7d16634cb283c827f0ad2
MD5 892defcc0f5652a39cfccd19a0101cb7
BLAKE2b-256 8595de774670d9dc3bf01633c89b4ea235645a908fd65c7621da38723071e8a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 314af88fc745576e86b5bbd3a4785dfad615174de2f94a90ce1d10fbbe3169e1
MD5 22a4e9d05256824da139de55341bef9a
BLAKE2b-256 f5505805bd9c9b8900cc34ce720efcd7db6b936619ee083a956dfbd3be382a02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 eaccc4f0912758bfbeeb3775cf0b35d13ec06061c06d0dcceaca01692765599a
MD5 be1656115ca7403b34c7aed7e972a3a5
BLAKE2b-256 56a49dd889820a7a75791de68478ce2f1381a97f6aef5404d377230d75743729

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fb1b8e15b6d3e110df967d88550a547f9fbcc52a780a82b43ba932e6c8fff97f
MD5 65692cb5d66e20d514e01bd075d888e7
BLAKE2b-256 50888079d44925d7746047121b369a06dd18ad31732d866def0b36a000973e03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 9c7435d80cf787e9e0804292f8742ff800d85ec54a0e64807f84d90a2bb5acf2
MD5 842e76ee6982da66f696abacbfa45c9b
BLAKE2b-256 bd4a56e0f0d11dd74ec7d2ad1a6bb72309f6eb78d431d799182aa2dbd4bd03a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 bff1d34952d69f5fa1172aaa21095d3d145d17389e651bfca52d1d675efe6403
MD5 2f0c2c99f33198805777b59d7c1c4c13
BLAKE2b-256 af671547359a01ef2439771f3f50148c4394a79e4cd6523dd82448ee76cbeb84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 386b6c9e45bcc5402f94a8d46c2da794950c75864b0020d140bb39fb76ea04fb
MD5 17b5935efe69d47667c6f61a16d4f87e
BLAKE2b-256 21dab968f016f6a1508429a543de4a924db5c12325bb132b8c535b74cd33dd5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 92d416ab99134fbba750b3cc6ae0d28f25bde7c079e597c35b3815bde28fc742
MD5 be42e0d4725b4f5b7aa7ffc18b204039
BLAKE2b-256 0e898dddf3d8b6d8f4d68ffa556ef8219488ebe4161d8739913e49fd7eb0a966

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 08c763f2c92df3f0381660461f99a34caa90d8d7432dfc0216d80688663136eb
MD5 4c403747f6db173377c1a05f331fad5e
BLAKE2b-256 8cd0890f2b2d200eb301bd621ab48458922312cf17713827f3f455f83939c647

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 78bbea5ad9966f0a44f6970f144602312c386ca9e7169526ca37e770a92a0061
MD5 7eff41868164783e9b1e1caa5bf8b0c5
BLAKE2b-256 87d26d0d074e9690eb85851820411176b28165b890c5b2639c151530e706ba70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 a1ec271969f860a19fb920d4e26e09d781ac9239c65486ce2b86ba491990dfcd
MD5 b4c2327b97670ddd408d0ea4fb365b79
BLAKE2b-256 18a50a6d5af8122da32cb75b9c8735aec280a5d0f6a3e3b1bc88150b55984d2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fcb3db769188e4db2237445f7de48d7a66d2b29c49553cff216928896253cdc3
MD5 f8ec13a296854254ad145bd04b20b7d5
BLAKE2b-256 76fa2b6eb11c25d7b86d5471e242588a0bd44a4ccb7c92f6095d49119279bf34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp26-cp26mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 24c58ca39f81425884798db812570d43b13d6f30fbe4ad7ac532c48a79a24ae5
MD5 ccd4c430a4d520f662e53eecb4950f74
BLAKE2b-256 6371945de918e00165d858cad1affe07fb11c6f1d8627c4f72e056332d8133bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssh2_python-0.5.3rc1-cp26-cp26m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a3bc1fbe96c772cb2a9d30a57fe2b4970bf2ba5b9b9b5b48251ca7dd91a452ea
MD5 1365dbccb2235ad05792cd5ad7318ce4
BLAKE2b-256 7a3da849307da2b04de7a734b632a14e5ea9507e4539be1c37fc290b104c46a3

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