Super fast SSH library - bindings for libssh2
Project description
Super fast SSH2 protocol library. ssh2-python provides Python bindings for libssh2.
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
Built Distributions
Hashes for ssh2_python-0.5.4-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1271a025b612700b3cbfef2d313b253fe989407f6b8614a1fb7d475a406e6cc3 |
|
MD5 | 46e014fb462e70dac85c7c9d4cb0f9a8 |
|
BLAKE2b-256 | f7a7d4d463a5836dff747e7435853a4b77e6586854ebcded1f96939a9ab4230c |
Hashes for ssh2_python-0.5.4-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc757b0ff01eb291849e3715c30fe36cf8cabc4ceec98dbbc3d99a6ba6597bdf |
|
MD5 | d6c438b997a6106ec198f7a3317107c9 |
|
BLAKE2b-256 | a7e888538c2427a9377a2cbc53842bac988ff17f9f4e566ee03519868479dd52 |
Hashes for ssh2_python-0.5.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d71cc94ae960c7254b05a650df98dfa76d0291d878d51277f74d8b2b71c66cc |
|
MD5 | 344333412489076124b7206547aba238 |
|
BLAKE2b-256 | ee671ab2028d6ffd8e748dde8c8c30ad0b290a752a25f1d4b510289e4b919cdb |
Hashes for ssh2_python-0.5.4-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d9353af2ca5499deb5a48ba4b4d6d2e9a92368522a2654efe082c088309b0b3 |
|
MD5 | fa6440b033a19571f1d8c984958f4c25 |
|
BLAKE2b-256 | 202762b3275b4ea18b3b7480f2aa2da7384f9487359f9571ad0eaba092f5d522 |
Hashes for ssh2_python-0.5.4-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3159f17e69829c910a15a240d9afadb343109a48f5d920cfa3912184b86db805 |
|
MD5 | 7a6aaade2dc0247b19d3977bcaa31d6a |
|
BLAKE2b-256 | 7f56c6c4dc78a62a0c1e23c67e083ec9f1a241dee38d41d447ed54efdf485750 |
Hashes for ssh2_python-0.5.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12400de1030e4adb62f75cda9d77d4255c7c24c689391ce21199796e24777103 |
|
MD5 | c32272da00972fb523cd20fcbdbb53b6 |
|
BLAKE2b-256 | 92e2901c2cd617c10531bfce3747bea03c07d785cf8f73e8f61b07191cb0606a |
Hashes for ssh2_python-0.5.4-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa37b46b7f573ea590e9cead317a60b67caa2af61e8a19465419b0c93e00a3a8 |
|
MD5 | cab93f04158d63ac219534c984eb66cc |
|
BLAKE2b-256 | 9bb067eba97d5889d752e522e8e04b7567b3f0b4f8d2e0a3a0aee43f86a928d8 |
Hashes for ssh2_python-0.5.4-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 496b3c2dcbed570f3c032a6c54db78d5015cb198e7c16fdaa30517b210ed0b46 |
|
MD5 | 4fbf672b05458d01951744dc877b2af2 |
|
BLAKE2b-256 | c88a966b45ca9f1ac7e06b9b04fc17a497c9f1c608e822fd2e9df799fc0c769b |
Hashes for ssh2_python-0.5.4-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4ea213d87ba472dd92584cd07359282595f0333ceac76229c5e1c66a622bdd1 |
|
MD5 | 665c60513b617827924c64eddf34a318 |
|
BLAKE2b-256 | 2cbeaac75afd7094645bb25d9979b88396f271abaaeb4b83738a9e7720a83446 |
Hashes for ssh2_python-0.5.4-cp33-cp33m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f955d58f2a0983bc2032e3170b594b304466370f209670f74e9e94bd7fe59fc5 |
|
MD5 | 7aabd50a98a459d03d8df26ab50c9d05 |
|
BLAKE2b-256 | 1402e1cb98ac187941101e146f18e072e69c44566977fb28388a0bb3dff3b660 |
Hashes for ssh2_python-0.5.4-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e3d1c38c5bc60da9a977289f941b66756c7e490ad31be74458e2cc6420b7840 |
|
MD5 | 4db10c30d6aa69d33704abef30f56643 |
|
BLAKE2b-256 | f406d5c18df234263b9a3f8927f00b56c702a60202e3be29f8a08309f5d72764 |
Hashes for ssh2_python-0.5.4-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05f1b6a773b2f28371d010c6f56724ef207a7627e58959099e6a3da3532ff34a |
|
MD5 | 21e666d5509a6e61a8f4e0471b9ef074 |
|
BLAKE2b-256 | 753974af689aac4771de095887c11e2f98bfdbda9dd7ff210abe175b3211d689 |
Hashes for ssh2_python-0.5.4-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68f5e6c9b2d33300ec963d764162e4a58b314f72369de1a8b2b8923d4282e31f |
|
MD5 | 693b63fc21b08ffe5291a1197501d390 |
|
BLAKE2b-256 | 8158ed9024017eb6a18da72db79c72005b93b3f29953ef3fea2cda49cc1fb60e |
Hashes for ssh2_python-0.5.4-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a039c9d353b7343d3c26ab027eb9eb66299aab83496e818fa52babf5e568777 |
|
MD5 | d0ec0d31d7414cb5ad96d8f8064e5d12 |
|
BLAKE2b-256 | 4cac6b9d40b211701260d9942af8c3cd14f1f13d996c7db0fbd65d28658f5faa |
Hashes for ssh2_python-0.5.4-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | afe873cddc6e408e59982620ce292c83aa5239e0b5c28b97e0893717673533e0 |
|
MD5 | e704827c9fa216852ed462e92a4a1432 |
|
BLAKE2b-256 | 17f3f1b673b3f2b15e8b8eb1b250738b0667ccb06fbd54ed7b10ef88fbf6383b |
Hashes for ssh2_python-0.5.4-cp26-cp26mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f14e1fbfd46771bf1798ab828f2be1908ed659281722dab60e84a6ca853d8765 |
|
MD5 | bbcec664027cc23677103d9d0fcb6e9e |
|
BLAKE2b-256 | c6a079f5a9fed209eb21622ab563999cd2a5516a280747db3212e9d918b7f396 |
Hashes for ssh2_python-0.5.4-cp26-cp26m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dc9fe6c688f23d4950847b39e9ebb05d847e67ec66bda5c3ddb442961f6b177 |
|
MD5 | 4c0b82291907945f10dddf7772ac1283 |
|
BLAKE2b-256 | b72e96e6d83fa77ea06e327ba1e099aaa5e0593f613fb394f0912451b74a6e60 |