Skip to main content

Super fast SSH library - bindings for libssh2 and Python 3

Project description

ssh2-python3

Super fast SSH2 protocol library. This ssh2-python3 package provides Python bindings for libssh2.

License

This is a forked and modified version of the original, ssh2-python.

Notable changes:

  • Supports Python 3 only.
  • Uses exclusively the embedded libssh2 (also modified to support Unix tunnel targets).
  • Compiles libbsh2 to use Python's memory allocator.
  • Some new methods that support:
    • Unix domain socket tunnel target on server host.
    • The "signal" protocol message.
    • Generic message constructor.
    • Bug fixes. Notably, a segfault during garbage collection in certain situations.

Any new bugs are the result of myself and not the orignal author (Panos Kittenis). Many thanks for his fine work to get this started.

Installation

Binary wheel packages are provided for Linux, all recent Python versions. Wheel packages have no dependencies.

You may need to update pip to install recent binary wheel packages - pip install -U pip.

pip install ssh2-python3

API Feature Set

At this time all of the libssh2 API has been implemented up to version 1.9.1-embedded.

In addition, as ssh2-python3 is a thin wrapper of libssh2 with Python 3 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 for 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, and uses Python's memory allocator.
  • 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.

Contributions are most welcome!

Authentication Methods

Connect and get available authentication methods.

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.

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.

As mentioned, ssh2-python3 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.

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, for both TCP and Unix sockets.
  • Non-blocking mode
  • SCP send and receive
  • Listener for port forwarding
  • Subsystem support
  • Host key checking and manipulation
  • Signal remote process.

And more, as per libssh2 functionality.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ssh2-python3-1.11.1.tar.gz (613.6 kB view details)

Uploaded Source

Built Distributions

ssh2_python3-1.11.1-cp311-cp311-manylinux_2_27_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.27+ x86-64

ssh2_python3-1.11.1-cp310-cp310-manylinux_2_27_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.27+ x86-64

ssh2_python3-1.11.1-cp39-cp39-manylinux_2_27_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.27+ x86-64

ssh2_python3-1.11.1-cp38-cp38-manylinux_2_27_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.27+ x86-64

ssh2_python3-1.11.1-cp37-cp37m-manylinux_2_27_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.27+ x86-64

File details

Details for the file ssh2-python3-1.11.1.tar.gz.

File metadata

  • Download URL: ssh2-python3-1.11.1.tar.gz
  • Upload date:
  • Size: 613.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for ssh2-python3-1.11.1.tar.gz
Algorithm Hash digest
SHA256 f2a2c5d39f486e83ec85bfa7e2bcc378c9709035dbc6cb664ad9bbddddefd084
MD5 83a7a6ff5bc45096640d06624c2d8067
BLAKE2b-256 6368abe21160e2b8deea39a0e9e7d16a50775b4b9382c0c3561d96bc0a94940b

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.1-cp311-cp311-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.1-cp311-cp311-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 2844c8ca8502009aefc1921e545f7898432f4a8a29c308ee7f890e98a7e34fda
MD5 68a6e374a0fb1a59d26ae57528a86f2c
BLAKE2b-256 b84afb4c9cf6ad1fd77d188a2151b70e6389224b2ce58f1f36f0f032dce721ea

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.1-cp310-cp310-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.1-cp310-cp310-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 fd1cdcf74b0b7f3a1277173784bca7a7eccd4fa7cd4882bb2156430c6f791eb2
MD5 74cd861dc770bc86cadfdc4da9300af6
BLAKE2b-256 2730fcd1e700f5b5b55e5db8e09af43f1a938a550417e8acd15875cbe3ff8489

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.1-cp39-cp39-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.1-cp39-cp39-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 6e62c8eaf8af033cf1cab339d88ca3dca806efbc1e9ec3f4e856265ca3add067
MD5 d529c9a89a2a05389c1ce5b5d62f316a
BLAKE2b-256 b86814144f21f9abfb1a6c08618acd120e1dca8a487ed4f3b3c63f677b1f8028

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.1-cp38-cp38-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.1-cp38-cp38-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 f8e2e2701cfd10bb3455ae42bf4e205ab448529ef77c323d6fb8261b6902dfce
MD5 1bbc44367d46a1c35b8f7e215b88a075
BLAKE2b-256 ba15fb17f40c0cbaf191b5dfdaeaa3d4e696b127a5bd863e3241b82cd383d4df

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.1-cp37-cp37m-manylinux_2_27_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.1-cp37-cp37m-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 ed356a27a2df76179ac281f3ec9c77586e5d4d3c47cee7d04649edd566837e83
MD5 9e2dc92d3b54ce2833d9795f61acaf23
BLAKE2b-256 5af959eceb675489be5ccfeb06f8b96d9bed47c2a21917c8f863e65f06322285

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page