Skip to content

Commit

Permalink
Updated readme, updated setup. Added exceptions cython file and agent…
Browse files Browse the repository at this point in the history
… source code file.
  • Loading branch information
Pan committed Jul 29, 2017
1 parent b9dc8ab commit 224b7a2
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 525 deletions.
124 changes: 124 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
ssh2-python
============

Super fast SSH2 protocol library. ``ssh2-python`` provides Python bindings for `libssh2`_.

.. image:: https://img.shields.io/badge/License-LGPL%20v2-blue.svg
:target: https://pypi.python.org/pypi/ssh2-python
:alt: License
.. image:: https://img.shields.io/pypi/v/ssh2-python.svg
:target: https://pypi.python.org/pypi/ssh2-python
:alt: Latest Version

Features
---------

Majority of the `libssh2`_ API has been implemented as Python native code extensions. ``ssh2-python`` is a thin wrapper of ``libssh2`` - ``libssh2`` code examples can be ported straight over to Python with only minimal changes.

*Library is usable for testing purposes and at the moment available as source code only. API, module names and documentation not yet finalised. Contributions welcome.*

SSH Functionality provided
++++++++++++++++++++++++++++

* SSH channel operations (exec,shell,subsystem)
* SSH agent
* Public key authentication and management
* SFTP
* SCP
* 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


Example
--------

A simple usage example looks very similar to ``libssh2`` `usage examples <https://www.libssh2.org/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.

.. code-block:: python
from __future__ import print_function
import os
import socket
from ssh2 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


Comparison with other Python SSH2 libraries
---------------------------------------------

Performance of above example, compared with Paramiko.

.. code-block:: shell
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 < link >`_ for more complete example scripts.

.. _libssh2: https://www.libssh2.org
.. _Cython: https://www.cython.org
57 changes: 57 additions & 0 deletions examples/example_echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from __future__ import print_function
import os
import socket

from ssh2 import Session
from ssh2.utils import version

# Connection settings
host = 'localhost'
user = os.getlogin()

# Make socket, connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))

# Initialise
session = Session()
session.handshake(sock)

# List available authentication methods
print(session.userauth_list(user))

# Convenience function for agent based authentication
session.agent_auth(user)

# Agent capabilities
# agent = session.agent_init()
# agent.connect()
# identities = agent.get_identities(user)
# print(identities)
# print(identities[0].magic)

# Public key blob available as identities[0].blob

# Channel initialise, exec and wait for end
channel = session.open_session()
channel.execute('echo me')
channel.wait_eof()
channel.close()
channel.wait_closed()

# Get exit status
print("Exit status: %s" % channel.get_exit_status())

# Print output
size, data = channel.read()
while size > 0:
print(data)
size, data = channel.read()

# SFTP capabilities, uncomment and enter a filename

# sftp = session.sftp_init()
# fh = sftp.open('<my file>', 0, 0)
# for data in fh:
# pass
# del session
16 changes: 16 additions & 0 deletions examples/paramiko_echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import print_function

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(
paramiko.MissingHostKeyPolicy())
client.connect('localhost')
transport = client.get_transport()
channel = transport.open_session()
stdout = channel.makefile('rb')
channel.exec_command('echo me')
for line in stdout:
print(line)
channel.close()
print("Exit status: %s" % channel.recv_exit_status())
4 changes: 0 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import platform
import os
import sys
import glob

# import versioneer
from setuptools import setup, find_packages, Extension
Expand All @@ -19,9 +18,6 @@

ext = 'pyx' if USING_CYTHON else 'c'

sources = glob.glob("ssh2/ext/*.%s" % (ext,))
extensions = sources

extensions = [
Extension('ssh2/*',
sources=['ssh2/*.pyx'],
Expand Down
Loading

0 comments on commit 224b7a2

Please sign in to comment.