GithubHelp home page GithubHelp logo

xuanyuan300 / sshex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rubencaro/sshex

0.0 2.0 0.0 35 KB

Simple SSH helpers for Elixir. SSH is useful, but we all love SSHEx !

License: MIT License

Elixir 100.00%

sshex's Introduction

SSHEx

Build Status Hex Version Hex Version

Simple SSH helpers for Elixir.

Library to unify helpers already used on several applications. It uses low level Erlang ssh library.

The only purpose of these helpers is to avoid repetitive patterns seen when working with SSH from Elixir. It doesn't mean to hide anything from the venerable code underneath. If there's an ugly crash from :ssh it will come back as {:error, reason}.

Use

Just add {:sshex, "2.1.0"} to your deps on mix.exs.

Then assuming :ssh application is already started (hence it is listed on deps), you should acquire an SSH connection using SSHEx.connect/1 like this:

{:ok, conn} = SSHEx.connect ip: '123.123.123.123', user: 'myuser'

Then you can use the acquired conn with the cmd!/4 helper like this:

SSHEx.cmd! conn, 'mkdir -p /path/to/newdir'
res = SSHEx.cmd! conn, 'ls /some/path'

This is meant to run commands which you don't care about the return code. cmd!/3 will return the output of the command only, and will raise any errors. If you want to check the status code, and control errors too, you can use run/3 like this:

{:ok, _, 0} = SSHEx.run conn, 'rm -fr /something/to/delete'
{:ok, res, 0} = SSHEx.run conn, 'ls /some/path'
{:error, reason} = SSHEx.run failing_conn, 'ls /some/path'

You can pass the option :separate_streams to get separated stdout and stderr. Like this:

{:ok, stdout, stderr, 2} = SSHEx.run conn, 'ls /nonexisting/path', separate_streams: true

You will be reusing the same SSH connection all over.

Streaming

You can use SSHEx to run some command and create a Stream, so you can lazily process an arbitrarily long output as it arrives. Internally Stream.resource/3 is used to create the Stream, and every response from :ssh is emitted so it can be easily matched with a simple case.

You just have to use stream/3 like this:

str = SSHEx.stream conn, 'somecommand'

Stream.each(str, fn(x)->
  case x do
    {:stdout,row}    -> process_stdout(row)
    {:stderr,row}    -> process_stderr(row)
    {:status,status} -> process_exit_status(status)
    {:error,reason}  -> process_error(row)
  end
end)

Alternative keys

To use alternative keys you should save them somewhere on disk and then set the :user_dir option for SSHEx.connect/4. See ssh library docs for more options.

TODOs

  • Add tunnelling helpers *

Changelog

2.1.0

  • Add connect/1 to improve testability by easier mocking

2.0.1

  • Avoid some Elixir 1.2.0 warnings
  • Adjust the SSH flow control window to handle long outputs (fixes #4).

2.0.0

Backwards incompatible changes:

  • Remove every raise, get clean controlled {:error, reason} responses
  • Put every optional parameter under a unique Keyword list

1.3.1

  • Fix Elixir version requested. Use >= 1.0 now.

1.3

  • Support streaming via stream/3
  • Stop using global mocks (i.e. :meck)

1.2

  • Uniform raise behaviour on :ssh errors.
  • Document and test :ssh error handling

1.1

  • Add support for separate stdout/stderr responses.

1.0

  • Initial release

sshex's People

Contributors

rubencaro avatar

Watchers

pierce avatar  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.