Python bindings for libssh2 based on Cython
Project description
Super fast SSH2 protocol library. ssh2-python provides Python bindings for libssh2.
Installation
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-dev
pip install ssh2-python
Feature Set
Majority of the libssh2 API has been implemented. ssh2-python is a thin wrapper of libssh2 - its code examples can be ported straight over to Python with only minimal changes.
Some parts are yet to be implemented though majority of the API is complete.
Library is at the moment available as source code only. Binary releases to follow.
Examples
Both byte and unicode strings are accepted as arguments and encoded appropriately. To change default encoding change the value of ssh2.utils.ENCODING. Channel output is always byte strings.
See Complete Example for a complete example including socket connect.
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: {}".format(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()
fh = sftp.open(<file path>, 0, 0)
with open(<file to write>, 'wb') as local_fh:
for data in fh:
local_fh.write(data)
fh.close()
Complete Example
A simple usage example looks very similar to libssh2 usage examples.
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
Public key authentication and management
SFTP open, close, read
SSH port forwarding and tunnelling
Non-blocking mode
Listener for port forwarding
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 SFTP file handles
Expose errors as Python exceptions where possible
Provide access to libssh2 error code definitions
Comparison with other Python SSH2 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
See examples directory for more complete example scripts.
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.