GithubHelp home page GithubHelp logo

ssh2.cr's Introduction

ssh2.cr

This library provides binding for libssh2 library.

Status

Alpha

Requirements

  • Crystal language version 0.17 and higher.
  • libssh2 version 1.5.0 or higher

You can use homebrew to install the latest libssh2:

$ brew install libssh2

Goal

The goal is to utilize libssh2 API by providing services like ability to run shell commands via ssh as well as scp and sftp services.

Usage

An example of running a shell command via SSH on the remote server:

require "./src/ssh2"

SSH2::Session.open("my_server") do |session|
  session.login("username", "password")
  session.open_session do |channel|
    channel.command("uptime")
    IO.copy(channel, STDOUT)
  end
end

An example of running shell:

require "./src/ssh2"

SSH2::Session.open("localhost", 2222) do |session|
  session.login_with_pubkey("root", "./spec/keys/id_rsa")
  session.open_session do |ch|
    ch.request_pty("vt100")
    ch.shell
    session.blocking = false

    buf_space = uninitialized UInt8[1024]
    buf = buf_space.to_slice
    loop do
      io = IO.select([STDIN, ch.socket]).first
      if io == STDIN
        command = gets
        if command
          ch.write(command.to_slice)
        end
      elsif io == ch.socket
        len = ch.read(buf).to_i32
        print String.new buf[0, len]
        break if ch.eof?
      end
    end
  end
end

An example of using SFTP API:

require "./src/ssh2"

SSH2::Session.open("localhost", 2222) do |session|
  session.login_with_pubkey("root", "./spec/keys/id_rsa")
  session.sftp_session do |sftp|
    sftp.open_dir(".").ll do |fn|
      puts fn
    end
    file = sftp.open(".bashrc")
    puts file.gets_to_end
  end
end

Testing

In order to run test suite you need to pull and run the following docker container:

$ docker pull tutum/ubuntu:trusty
$ docker run -d -p 2222:22 -e AUTHORIZED_KEYS="`cat ./spec/keys/id_rsa.pub`" tutum/ubuntu:trusty

License

MIT clause - see LICENSE for more details.

ssh2.cr's People

Contributors

benaghaeipour avatar colstrom avatar datanoise avatar fusion avatar vonkingsley 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.