Code Monkey home page Code Monkey logo

py-libssh's Introduction

py-libssh

Python wrapper for libssh build on top of boost::python. With full streaming api.

Dependences:

  • boost >= 1.50
  • python >=3.2, 2.7
  • libssh >= 0.5
  • cmake >= 2.8 (build only)

And C++11 ready compiller. Tested with g++(4.7.1) and clang++(3.1).

How to build

For development, go to repo root directory and execute these commands:

mkdir build;
cd build
cmake ../src
make

# Copy a python module to local directory
cp ../src/pyssh.py .

For normal use, you can use a standard python distutils setup.py file:

python setup.py install

Api reference:

pyssh.connect(hostname="localhost", port=22, username=None, password=None)

Creates ssh session and connects to corresponding host and port. By default intent autenticate with local pubkey. If passwrod and username are provided, normal user and password authentication is executed instead of a pubkey.

This returns pyssh.Session instance.

pyssh.Session

Represents a ssh connection.

pyssh.Session.execute(command)
Executes some command on a remote machine. Returns pyssh.Result instance.

pyssh.Result

Represents a result of execution of command on ssh session. Result by default, does not download all the content, but you have to iterate over all output for command execution. (The content is obtained from the server in chucks of 1024 bytes)

pyssh.Result.__iter__()
Returns itself as iterator.
pyssh.Result.__next__()
Iterate over command output content in chunks of 1024 bytes.
pyssh.Result.return_code
Command execution return code. Only avaliable over all iteration.
pyssh.Result.as_str()
Return unicode string of all command execution output.
pyssh.Result.as_bytes()
Same as that, as_str() but returns a bytes.

pyssh.Sftp

Represents a sftp connection.

pyssh.Sftp.__init__(session)
Creates a sftp session on top of ssh session (pyssh.Session).
pyssh.Sftp.put(local_path, remote_path)
Transfer local file to remote file.
pyssh.Sftp.open(remote_path, mode="w+")
Open remote file (with random access support). Mode can be: w (Write + Truncate), r (Read), w+ (Write+Read+Truncate) and r+ (Read+Write). Returns pyssh.File instance.

pyssh.File

Represents a opened sftp remote file with random access support. This file only works with python3 bytes or python2 str types.

pyssh.File.write(data)
Write bytestring to the opened file.
pyssh.File.read(num=-1)
Read content from the opened file. if num is -1, reads all content from current position to the end of file.
pyssh.File.seek(pos)
Change position on the opened file.
pyssh.File.tell()
Get current position on the opened file.
pyssh.File.close()
Close the current file.

Examples

Sftp session example.

>>> import pyssh
>>> session = pyssh.connect("localhost")
>>> sftp = pyssh.Sftp(session)
>>> f = sftp.open("/tmp/some-file", "w+")
>>> f.tell()
0
>>> f.write(b'Hello World')
>>> f.tell()
11
>>> f.seek(0)
True
>>> f.read(5)
b'Hello'
>>> f.read()
b' World'

Command execution example.

>>> import pyssh
>>> s = pyssh.connect()
>>> r = s.execute("uname -a")
>>> r.as_bytes()
b'Linux vaio.niwi.be 3.5.3-1-ARCH #1 SMP PREEMPT Sun Aug 26 09:14:51 CEST 2012 x86_64 GNU/Linux\n'
>>> r.return_code
0

py-libssh's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

py-libssh's Issues

License for py-libssh ?

Hello,

I am the author of libssh. We are working with colleagues on a project that involves libssh and python, and would like to use your library as a start point for our bindings.
However, nowhere in the source code or documentation I could find the license terms of your lib. In the current status, we cannot (nobody can in fact) reuse your code to build onto.
Could you please clarify this ?

I must say that a license like LGPL, BSDv2 or MIT would be very appreciated.

Thanks a lot,

Aris Adamantiadis

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.