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.2.tar.gz (632.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ssh2_python3-1.11.2-cp314-cp314-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64manylinux: glibc 2.35+ x86-64

ssh2_python3-1.11.2-cp313-cp313-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64manylinux: glibc 2.35+ x86-64

ssh2_python3-1.11.2-cp312-cp312-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64manylinux: glibc 2.35+ x86-64

ssh2_python3-1.11.2-cp311-cp311-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64manylinux: glibc 2.35+ x86-64

ssh2_python3-1.11.2-cp310-cp310-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64manylinux: glibc 2.35+ x86-64

File details

Details for the file ssh2_python3-1.11.2.tar.gz.

File metadata

  • Download URL: ssh2_python3-1.11.2.tar.gz
  • Upload date:
  • Size: 632.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ssh2_python3-1.11.2.tar.gz
Algorithm Hash digest
SHA256 825fd7d1623c0c3ae3338d69c7fcf5e2188a6fd9420cdb9d24a43eac4a1b962f
MD5 d51dee2168c3c80d4066d513d9e3b7da
BLAKE2b-256 cfbcbf4572d546870908365df8a45c5539c1badd1b6b90448f30fedfdc01a0da

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.2-cp314-cp314-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.2-cp314-cp314-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 7ce36503ef7b0c2f0ea625611f1045cde83e3f149fad18eff0e105ba64d2955a
MD5 b4e7433879fbf8231de5c083d1457231
BLAKE2b-256 5ff6b7dd57aba7a65c824dc1cc46f6b00b154bec24b800071df538bd5e08ab47

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.2-cp313-cp313-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.2-cp313-cp313-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 ff4b36e3a5ce4dbbcaa4a00f858f28d33cab9dc89b4d57b57fb569209cf95cbc
MD5 26284a216f366e40ffe3b591bfc6a866
BLAKE2b-256 3845236843644320f2e770e01beab93b8b47dac48670bb83e128749246f44193

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.2-cp312-cp312-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.2-cp312-cp312-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 8353bdad0696bb1996375f26b29c295641a7c19c05f9af263aebf7bd2196ac59
MD5 3c9746cdeaa6fc13d9118168d944b6a1
BLAKE2b-256 ad7d296076c35ee4cff1d51154216e4103c051c83462cb59177bc74bb52b6548

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.2-cp311-cp311-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.2-cp311-cp311-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 8bb8ca7bfed9e76b1f29e0e75ed521f106f599789f6d755da834e9f7e510e45d
MD5 f608a133bcf8840f1f8446a7d709a66a
BLAKE2b-256 7306999be679029f504c4e2f08d476cd45c7bb77c9f792b6783a431fc041111c

See more details on using hashes here.

File details

Details for the file ssh2_python3-1.11.2-cp310-cp310-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for ssh2_python3-1.11.2-cp310-cp310-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 eaa3a455dbcfab1bafd6a8c66eeacf30d9297fc69f1625bcd4160704b187d797
MD5 0e5dd263b37c7997323c7884fe999ac8
BLAKE2b-256 2ccd552ed486a9913030bb44a2c9acee1be6fa505015fd0ab2199178d6e77415

See more details on using hashes here.

Supported by

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