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
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. Note that libssh2 does not support sharing sessions across threads
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 Distributions
Built Distributions
Hashes for ssh2_python-0.20.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d53803cb9f0b78b4ace186019cb42145cfa395c8494f4eb69b3761cf287930b |
|
MD5 | 4648c7316b616ce74344004397ae08a6 |
|
BLAKE2b-256 | 95862bd01f9d2358cdcede8e6b068a373da3903e4c1b39b7b393f114cbcd0f97 |
Hashes for ssh2_python-0.20.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4eb47340ea6cb2e27b304ab71c0d6cc1d87139135a18a86c9eb7a5be99cdf36c |
|
MD5 | 4aeb2b5ce0067b409243f7da97a5f7c7 |
|
BLAKE2b-256 | fdd700d49f05a8b06ab4ed098dfe1b39cb9182ee919df678666011902cec0781 |
Hashes for ssh2_python-0.20.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d873ba9c210347794d33d52426dd63a71bd660be39ddb9ab4ce6db5c548d2199 |
|
MD5 | 6704565af411397f1856a118be86b5b7 |
|
BLAKE2b-256 | 766b9ae5a6c1140019cfdd7050a83039aef872e27a48c4f419bd5d2c6969e1d0 |
Hashes for ssh2_python-0.20.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 256618401440abe9dd7da4a436a40fe34cd2a63841e6ff7bcd56e12a23593a3b |
|
MD5 | 6ffb344c5251684196afedeae15fe0d5 |
|
BLAKE2b-256 | 9b6543a0c39fc66d0840b5beb02e5f3c88fbbd5a70bfc02c56a94d7c3cef4a65 |
Hashes for ssh2_python-0.20.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef3b8ec65e08fd9a125fdbf8452d22fd48888ec9ba0b70c8cec23d98a4330983 |
|
MD5 | 716e5a9d234682a8ce16117a448052fd |
|
BLAKE2b-256 | 73a732324b6cd935260328ecb9c3db8be202094b63798bf3e3672676f3d5dc47 |
Hashes for ssh2_python-0.20.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f67011212375e902e7102a44c0c3fa460c60607cd65356b64a014004a3465627 |
|
MD5 | 53975184b3b7d068d8996313966f77fb |
|
BLAKE2b-256 | 4ea652fbdf92347232cee84bd6f1cb03288133c9ccdfdcd00a72465daae54fea |
Hashes for ssh2_python-0.20.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d6a92f064b548d6640559b5d0063b34e9d436f6e7c8067615e8c159f5b8b2b1 |
|
MD5 | e523d4a0e25af5ed7dcd5fcfa63681a7 |
|
BLAKE2b-256 | 4b03b06f57d52f7dbd8f815236891673b71c3cdbd22bb33a2d77440aed671cb2 |
Hashes for ssh2_python-0.20.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3262b1907e0274949c2238a693a5143614fd28a7a9a6b89d1f7dec2b7e2959ec |
|
MD5 | 5a5ab864099190a24ac5a4fd133ebc15 |
|
BLAKE2b-256 | 1e37b77942773b6416b7022f2cda0b69044d3d6a251f4398ab4af148b16033e7 |
Hashes for ssh2_python-0.20.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86dd4070d1ad83d8a3adb49cc0d6787cb65eecb7b9d16178d73531524e746b6e |
|
MD5 | 369baff2d48b73817c153d789cd26841 |
|
BLAKE2b-256 | 08816bc61843595b7c22cf3f6c4259440cb3b84830ebadac4a7b18e310aa722d |
Hashes for ssh2_python-0.20.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ccd2ae54a695e4a36ef2c36e1a2b003614d4b2811cc35905b7387471a72e866 |
|
MD5 | 63d67cae2400eec2a7b783df9997e855 |
|
BLAKE2b-256 | 1ba61378d1b18ff4b66a240bff68280b9bf65ed797f4f7287e84327fa92f4576 |
Hashes for ssh2_python-0.20.0-cp27-cp27mu-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c100a73f14941095abe68b5f00650db9e9292ec7b32ace55c539198bbc902b1 |
|
MD5 | 35ec895e50b4f94bffd718c1ddfee139 |
|
BLAKE2b-256 | fea09a00ef3eb74bd201e8097a948ca0e2cf5671f673e64ffe2101fb1584908c |
Hashes for ssh2_python-0.20.0-cp27-cp27m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5fccb832e6f38e4fc11d69f938871dcd71768e26b69044239496c6b0024d5b4 |
|
MD5 | 93637d2138f640164dab97a4ff190302 |
|
BLAKE2b-256 | 1cbb50c9a12486115b3f4d9620107b9af73fe372d76664674693a13a6d5ea78f |