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. 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
Conda is another installation option - see documentation for more detailed instructions.
For from source installation instructions, including building against system provided libssh2, see documentation.
For creating native system packages for Centos/RedHat, Ubuntu, Debian and Fedora, see instructions in the documentation.
API Feature Set
At this time all of the libssh2 API has been implemented up to version 1.8.2.
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
Raise errors as Python exceptions
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. Output is always in byte strings.
See Complete Example for an 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())
Output will vary depending on SSH server configuration. For example:
['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, 'private_key_file')
Passphrase can be provided with the passphrase keyword param - see API documentation.
Password Authentication
session.userauth_password(
username, '<my password>')
SFTP Read
from ssh2.sftp import LIBSSH2_FXF_READ, LIBSSH2_SFTP_S_IRUSR
sftp = session.sftp_init()
with sftp.open(<remote file to read>,
LIBSSH2_FXF_READ, LIBSSH2_SFTP_S_IRUSR) as remote_fh, \
open(<local 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 intentionally 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
Host key checking and manipulation
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.17.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62934a0c01feb42fa67fdac1b4cebeaeee09c9b509778d4d0edc4f0bcc6980a4 |
|
MD5 | b287ff92dee3f7b3a2d7acb1dce47136 |
|
BLAKE2b-256 | 1f85818a3d040fa72c05512e69d3a17da1b249284b8314ab11126ccaec6a2dc1 |
Hashes for ssh2_python-0.17.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c724fb88d1ffb5a639949c7f16638d6914492843a4a8a66da38b6d32223bcf46 |
|
MD5 | eab4d3c9dcebcc183a571bc1ff4537a2 |
|
BLAKE2b-256 | 1aa0e55e0b77af79621efe3f5ebe85ee6400cbe1eef0c0b3abc56a6ca6bf6c1e |
Hashes for ssh2_python-0.17.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7266dc0a19fc8269e2b3930127a59bbdb68d39ff81559c4d7c36fea902fd81a3 |
|
MD5 | db73c63432a4ffbe5e54ddefe0f2174e |
|
BLAKE2b-256 | fdb27710a3ce254abf76194d1b6d820f95d9f33ad480af200fe4324ee324317d |
Hashes for ssh2_python-0.17.0-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b9fe88eceb4a1ace4c55079142b1e9f0dbad86c3c235986af96b809a8f9845f |
|
MD5 | 6bddf4cca1eaf20dae7a4a2f493270fd |
|
BLAKE2b-256 | 5e1ad3cea97e41e770cd6a57505a767684e0e6fb687831b07768070a8f077e2c |
Hashes for ssh2_python-0.17.0-cp37-cp37m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb88b6c6615d4369e7f701a1db8268275ecd915fa7ee7c8e02c4684a1f948eec |
|
MD5 | 01731243e4b6f4cdcc613faf0bba205f |
|
BLAKE2b-256 | f7279740585290edc7babe4718cfe6a786b9a93336ffbc085f13dc198695fa5b |
Hashes for ssh2_python-0.17.0-cp37-cp37m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae916e3822701e6ca78d1857507abe3d1fba1a2658628ed91ad90564eda33078 |
|
MD5 | 2045abd47e86882b2726f87472f8d442 |
|
BLAKE2b-256 | 6a572985b4cc2ea2ef45983f831cb3b24add822ec703ba6a69607b72b55e0a00 |
Hashes for ssh2_python-0.17.0-cp37-cp37m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90af6e0713a582173d7b3fd621c9bd59bb8d155bca12426401a98cab4fab01a2 |
|
MD5 | 34a6b182cf2e1968c0547aa3a8850ea4 |
|
BLAKE2b-256 | 9c5cda02e975731b0ab27dffde20b5f61728c37a3a2b2c94feaa6e9f8eb193f2 |
Hashes for ssh2_python-0.17.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c15ee53d0af149634f2d793a5b44293f3f6fe70722cc33a53ddacf5327cabdb |
|
MD5 | 6930e87506255679c8ab17c85d1fdc86 |
|
BLAKE2b-256 | de368b6f8ca5fe6e0f427f15cbab2c5804294fba1920b96d1b03fbb286344629 |
Hashes for ssh2_python-0.17.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 634a198fb50c4bfb1a9acda34149008661171cc96a7bf01faf692f15f848a90b |
|
MD5 | 323c7bb07dd807ad627562079dddd603 |
|
BLAKE2b-256 | b37d00277ca1b2d33173adc18197f610ed5af3e4db3826647ef13f79e182ae3c |
Hashes for ssh2_python-0.17.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ed50af9e73aa599fde4bb2357b9911275f02a58214fa3b560529e0b7ff46033 |
|
MD5 | d5755d5fbd5a6d6519270363739c2ca4 |
|
BLAKE2b-256 | d0ef0676e13db374733195a0ded79bcc60ff49741b2140146d0ed1a6b58a6a57 |
Hashes for ssh2_python-0.17.0-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 677e7ad1f1792e2cd06f5baf9c850e353fcc514d54195fb402e127062ea6eca2 |
|
MD5 | 067f7632ea237fd3cc61446cf18dd42b |
|
BLAKE2b-256 | 63838e383256ed0cad8ce4d6f78153551e2895b772e33cad6fd697942423799b |
Hashes for ssh2_python-0.17.0-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ec53c69e2b3eb6fbc01c58af4097f76e42f3637effd5daf3caea374d723d2d1 |
|
MD5 | 6f158e2c3450f8046f0ef45364f89381 |
|
BLAKE2b-256 | fe5fddd2210e5814b00f93194ebb49cda82ce756ea6f05bd67bd649c5fd70413 |
Hashes for ssh2_python-0.17.0-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a2d0bfe1b0990568a6ff2f3d057a4e627749b8a54f47af175dbdcb450bb3761 |
|
MD5 | 52a321d0cfadc89f91c1fc2231157a4c |
|
BLAKE2b-256 | 6c78ef510dcf84eeb412290135b6ccbd6ed6ce2b9bfa468c2f760e3d1ce8ffeb |
Hashes for ssh2_python-0.17.0-cp36-cp36m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2f46855073419bfbea53ba837f15dfe9c875e175987f09dd1fec99ae7eab47e |
|
MD5 | 4f14245cc3d41b56471c33fbb62456d2 |
|
BLAKE2b-256 | 8ff8ecc87e6cfc3f0e5faaaef9a8bc14d82eaf29b1b5ce9c9c0c0923ad02bc82 |
Hashes for ssh2_python-0.17.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8375952a103b7e47e5684f05a7656a27055fa9278b18587d9628823f0c9604c |
|
MD5 | 419e0e919172337d5ca0595b0f9fc15c |
|
BLAKE2b-256 | 47fe5dfaa0d5c2859d3b15d6c76207b409a1038d60ce9e844ea263a62ebec5b5 |
Hashes for ssh2_python-0.17.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b2d8cf5cf91fb97744e5d660f276de833aff4253178ffd65d5a9590d4e9ff04 |
|
MD5 | c416305b7c3914ef3b93d75852909873 |
|
BLAKE2b-256 | 94aeab764501ad6b4fe7bec062a576aac2646c476a9c864643943e65478875f4 |
Hashes for ssh2_python-0.17.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dec556e968109d7f7c7279516f96333e95ef9824e4bfa93fa00b2f3673b2615d |
|
MD5 | bf8651c9e9aa560e14c72e478d0b4e1c |
|
BLAKE2b-256 | 77a69a495d813e732336f8c2983192cc82684fb2298a1f371061dd2cdb3e9e17 |
Hashes for ssh2_python-0.17.0-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 286ae90146e1a500891976bfa2bd174df82a99b345583fa26052f2ae3271756a |
|
MD5 | eed08a7089cf2dce1c2b69bc5dbd11a6 |
|
BLAKE2b-256 | 64f1469336065c452859e4f9aa2b486c00f7f5223ada24013b1f417539a545ee |
Hashes for ssh2_python-0.17.0-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6167353e631a2f829931046b6267af08593d7ecc19e3dc7724236a5e4042679e |
|
MD5 | f223498b0d485cdbf5fb164c9c5f53ba |
|
BLAKE2b-256 | 7dcce1f67e9563bfaa65d8144a31f3e05e008ed5cb3662743acc2bb58e504e9f |
Hashes for ssh2_python-0.17.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59e6d1a589b69aef5efe03fe692b5600b84aa1f604a451b95c2fefb4ec1821c6 |
|
MD5 | 7327376e2f5373a21ec7f4ed9e3ff08d |
|
BLAKE2b-256 | ce93f4bd0a9d9018417c1aaccd15d60c8be4e7d3192cb1a5a6d2797de98718ae |
Hashes for ssh2_python-0.17.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e8c82493fc7583cda2714a23aca018562916cc45dd3c6e0ffc5f723f21384e7 |
|
MD5 | ce642a4ce8c2f49a2013f9d90046a61d |
|
BLAKE2b-256 | 7fbde088bca07d7a958b990d4d4b7c6858094596112afca6dd578a8fb2024ddd |
Hashes for ssh2_python-0.17.0-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bd1e6889a99472ef05459a502f041bfe53453372a235cacccffce74df553b4e |
|
MD5 | 4a47e96ed958e178480dcd54029b66f5 |
|
BLAKE2b-256 | 3885563974e8af81b3cb9ea8150ee9125e6155a645ad0532617db1dd8a0a79f3 |
Hashes for ssh2_python-0.17.0-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca6faa65b28e1d3349a5770390a2ea523915864f0b6590141564184153453210 |
|
MD5 | f56541b63b16ae01498f00488912661f |
|
BLAKE2b-256 | 021edce8f08213f659a0b1198bb254a1e2a8991784e0a3b993efdaf0d0e484de |
Hashes for ssh2_python-0.17.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7dd3760692e00c882397c940956358f45238c264d14e317aea95c638b6d01c0 |
|
MD5 | ab0b592f84caadee75419821ac93ecd7 |
|
BLAKE2b-256 | 5c21fb68b33c006e5c00f7f34271d16922aa7bd23d36b51e3b05e5f8ef97ca99 |
Hashes for ssh2_python-0.17.0-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2afd7824e094b90283d6bac0e5f2360de4860fe6feae6755f3ed8653eeb10be |
|
MD5 | e9e405c86d99dfc2cf33e7c254a75773 |
|
BLAKE2b-256 | df5cdfb79e6708f90a52f024536320485128edcd9532994ba49e0513a25e42ab |
Hashes for ssh2_python-0.17.0-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aec216637ca9a3e452ae6e43d9bac20702b2b9c4464823c5fbd92b5aca67f9ce |
|
MD5 | 608d615f29a36aadc332f7b58e117d12 |
|
BLAKE2b-256 | 417beb4e4ec1f29fda9c135802455fe2721fbfd92a4d496af2f78aec61bb67c6 |
Hashes for ssh2_python-0.17.0-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37217762a4b71b5a46cc45f06bf5c460a7547262020bf1c147ced30ab0e11e5c |
|
MD5 | 6766a474dcf46e20e7dc9d6d41cf2c73 |
|
BLAKE2b-256 | 54d4f533f83c602d1b60dd2bf1a843c85582796935942d209957877ebb76b82b |
Hashes for ssh2_python-0.17.0-cp27-cp27m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ca222576de2d311122479bf1a91ce9d8fa22742564da7da23b2951fde425bc1 |
|
MD5 | 852e3a7ca0382180e6c268f0de45fe18 |
|
BLAKE2b-256 | 29d5fdc1e53a15e02f99bdfb86036fe0861122601ffce1743941f7273d98ef3c |