Super fast SSH library - bindings for libssh2
Project description
Super fast SSH2 protocol library. ssh2-python provides Python bindings for libssh2.
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
Built Distributions
Hashes for ssh2_python-0.7.0.post2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3faf190f0f20e19f5700fed1aca1b8bf1fa8b6099ea91ef061c43b79da220dda |
|
MD5 | a8e1dab8c60454e3a07f4189112335fb |
|
BLAKE2b-256 | 59432debf546cca804a54083c0fd1db6f7ef0c69e15abd13e568b69f5b69906d |
Hashes for ssh2_python-0.7.0.post2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a812ad0029fb329100fe948408d508b23a2198f4e2b5be1b18af60992d856805 |
|
MD5 | 29df1f12023ea00f6b6a572d4430725e |
|
BLAKE2b-256 | bd2a4636e95cb57dea37d10bf6f55cf57320e45f7ef7ef3611e2976daa840298 |
Hashes for ssh2_python-0.7.0.post2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 316d4d0e0bec0a2ceff59e0b6bb7983c624e68df322779e89edbf607a9be94f9 |
|
MD5 | 5552a3c2b241c27965e57f1e8f895d1d |
|
BLAKE2b-256 | c4bc2198490c155f40b5a1c245ce2cc7853f525920016202dd269003b101be29 |
Hashes for ssh2_python-0.7.0.post2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5285a00efd00bd3240a4b0c264604211a9a356c2b8e58df0aaf9fbe59dd573cd |
|
MD5 | e3756277100d921b9d1be188d78eea8e |
|
BLAKE2b-256 | 34c96051277f6e429a3bfe524a478fe8ced994290be144aa484015bc1d02d6a8 |
Hashes for ssh2_python-0.7.0.post2-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0109d1a1a4e5d34f9adace1a297ec4907fbd0ae033203d05dcf090ec247df84e |
|
MD5 | 892956dd4655208c5b84a69f80a79e0e |
|
BLAKE2b-256 | 9b0eee491b5a7c077ef073116b241446aa2cacf41f485e43029d2e5495b025c7 |
Hashes for ssh2_python-0.7.0.post2-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 260f842ecd173656a2b920a5112156b0e84651e2d49718510f6694f8e9f15b7c |
|
MD5 | 4d0d4a3b44bd9b912781e8158c7ce4d4 |
|
BLAKE2b-256 | 475aaf7048ce2dbe2b68882893f4715e9fe05e393fade4179bad51fb194ac5bb |
Hashes for ssh2_python-0.7.0.post2-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 239d820ea8536146d2acfa01eeed6e0adf7963eb57dc9dcbad79b065403adde1 |
|
MD5 | c98329f1167da91b20af17fb2f1d942c |
|
BLAKE2b-256 | f3694c7b0de6b461bf6c3b5488cb987c67d5d5c8360bc6e2620466de2456616a |
Hashes for ssh2_python-0.7.0.post2-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7e1571134fc8b1f37d81b83282da82e564fe820c17aa7ab8925be53dae08ab4 |
|
MD5 | 0c9b6b723aad604d82b15129fc19d8ca |
|
BLAKE2b-256 | 0fb0ceaa503d005be2bd8e63e97d9d48176d30a53d36e6d96338273293286124 |