Skip to main content

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.

Release files for ssh2-python3 1.11.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ssh2-python3 1.11.2
File Size Uploaded
ssh2_python3-1.11.2.tar.gz 632.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for ssh2-python3 1.11.2
File
ssh2_python3-1.11.2-cp314-cp314-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.34+ x86-64, Linux glibc 2.35+ x86-64 Details
ssh2_python3-1.11.2-cp313-cp313-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.34+ x86-64, Linux glibc 2.35+ x86-64 Details
ssh2_python3-1.11.2-cp312-cp312-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.35+ x86-64, Linux glibc 2.34+ x86-64 Details
ssh2_python3-1.11.2-cp311-cp311-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.35+ x86-64, Linux glibc 2.34+ x86-64 Details
ssh2_python3-1.11.2-cp310-cp310-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.35+ x86-64, Linux glibc 2.34+ x86-64 Details

Total release size: 23.5 MB

Release files / ssh2_python3-1.11.2.tar.gz

Download URL ssh2_python3-1.11.2.tar.gz
Size 632.8 kB
Tags Source
SHA-256 checksum
How to use checksums
825fd7d1623c0c3ae3338d69c7fcf5e2188a6fd9420cdb9d24a43eac4a1b962f
BLAKE2b-256 checksum
How to use checksums
cfbcbf4572d546870908365df8a45c5539c1badd1b6b90448f30fedfdc01a0da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / ssh2_python3-1.11.2-cp314-cp314-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl

Download URL ssh2_python3-1.11.2-cp314-cp314-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Size 4.6 MB
Tags CPython 3.14 Linux glibc 2.34+ x86-64 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
7ce36503ef7b0c2f0ea625611f1045cde83e3f149fad18eff0e105ba64d2955a
BLAKE2b-256 checksum
How to use checksums
5ff6b7dd57aba7a65c824dc1cc46f6b00b154bec24b800071df538bd5e08ab47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / ssh2_python3-1.11.2-cp313-cp313-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl

Download URL ssh2_python3-1.11.2-cp313-cp313-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Size 4.6 MB
Tags CPython 3.13 Linux glibc 2.34+ x86-64 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
ff4b36e3a5ce4dbbcaa4a00f858f28d33cab9dc89b4d57b57fb569209cf95cbc
BLAKE2b-256 checksum
How to use checksums
3845236843644320f2e770e01beab93b8b47dac48670bb83e128749246f44193
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / ssh2_python3-1.11.2-cp312-cp312-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl

Download URL ssh2_python3-1.11.2-cp312-cp312-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Size 4.7 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
8353bdad0696bb1996375f26b29c295641a7c19c05f9af263aebf7bd2196ac59
BLAKE2b-256 checksum
How to use checksums
ad7d296076c35ee4cff1d51154216e4103c051c83462cb59177bc74bb52b6548
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / ssh2_python3-1.11.2-cp311-cp311-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl

Download URL ssh2_python3-1.11.2-cp311-cp311-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Size 4.6 MB
Tags CPython 3.11 Linux glibc 2.34+ x86-64 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
8bb8ca7bfed9e76b1f29e0e75ed521f106f599789f6d755da834e9f7e510e45d
BLAKE2b-256 checksum
How to use checksums
7306999be679029f504c4e2f08d476cd45c7bb77c9f792b6783a431fc041111c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / ssh2_python3-1.11.2-cp310-cp310-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl

Download URL ssh2_python3-1.11.2-cp310-cp310-manylinux_2_34_x86_64.manylinux_2_35_x86_64.whl
Size 4.5 MB
Tags CPython 3.10 Linux glibc 2.34+ x86-64 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
eaa3a455dbcfab1bafd6a8c66eeacf30d9297fc69f1625bcd4160704b187d797
BLAKE2b-256 checksum
How to use checksums
2ccd552ed486a9913030bb44a2c9acee1be6fa505015fd0ab2199178d6e77415
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.11.2 This release

6 release files

1.11.1

6 release files

1.11.0

6 release files

1.10.2

5 release files

1.10.1

5 release files

0.22.6

5 release files

0.22.5

1 release file

0.22.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page