| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Super fast SSH2 protocol library. ssh2-python provides Python bindings for libssh2.
Binary wheel packages are provided for Linux, OSX and Windows, all Python versions. Wheel packages have no dependencies.
pip may need to be updated to be able to install binary wheel packages - pip install -U pip.
pip install ssh2-pythonFor from source installation instructions, including building against system provided libssh2, see documentation.
For creating native system packages for Centos/RedHat, Ubuntu, Debian and Fedora, see instructions in the documentation.
Developers of bespoke SSH clients.
Developers looking for ready made SSH clients.
This library is not an SSH client.
Developers looking for high level easy to use clients based on this library should use parallel-ssh. It provides both single and parallel clients.
This library provides bindings to libssh2 and its API closely matches libssh2.
If the examples seem long, this is not the right library. Use parallel-ssh.
At this time all of the libssh2 API has been implemented up to the libssh2 version in the repository. Please report any missing implementation.
Complete example scripts for various operations can be found in the examples directory.
In addition, as ssh2-python is a thin wrapper of libssh2 with Python semantics, its code examples can be ported straight over to Python with only minimal changes.
The library uses Cython based native code extensions as wrappers to libssh2.
Extension features:
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.
See Complete Example for an example including socket connect.
Please use either the issue tracker for reporting issues with code or the mail group for discussion and questions.
Contributions are most welcome!
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())Output will vary depending on SSH server configuration. For example:
['publickey', 'password', 'keyboard-interactive']session.agent_auth(user)channel = session.open_session()
channel.execute('echo Hello')size, data = channel.read()
while(size > 0):
print(data)
size, data = channel.read()Helloprint("Exit status: %s" % (channel.get_exit_status()))Exit status: 0session.userauth_publickey_fromfile(
username, 'private_key_file')Passphrase can be provided with the passphrase keyword param - see API documentation.
session.userauth_password(
username, '<my password>')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)A simple usage example looks very similar to libssh2 usage examples.
See examples directory for more complete example scripts.
As mentioned, ssh2-python 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.
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 |
|---|
And more, as per libssh2 functionality.
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 |
|---|
| Back | FazBrowse Home | New Git URL |