Git Product home page Git Product logo

hossein-mohseni / ssh2-python3 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pycopia/ssh2-python3

1.0 1.0 0.0 15.98 MB

Fast wrapper for libssh2.

License: GNU Lesser General Public License v2.1

Shell 1.85% C++ 0.17% Python 3.18% Perl 1.00% C 70.05% Emacs Lisp 0.07% PowerShell 0.04% Awk 0.03% Clean 0.87% Makefile 2.55% CMake 2.11% Batchfile 0.04% M4 2.59% Cython 6.90% Dockerfile 0.14% Roff 6.81% RPGLE 1.59%

ssh2-python3's Introduction

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.

ssh2-python3's People

Contributors

bagder avatar jas4711 avatar pkittenis avatar mback2k avatar willco007 avatar gknauf avatar alamaison avatar dfandrich avatar dottedmag avatar vszakats avatar kdart-brt avatar kdart avatar michaelbuckley avatar kdudka avatar salva avatar ueno avatar zenju avatar jakob avatar biot avatar tsengjun avatar wootpthomas avatar yodaldevoid avatar agebert avatar monnerat avatar gitpeut avatar craigberry avatar castaglia avatar pipo avatar doublex avatar sbredahl avatar

Stargazers

Hossein M avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.