Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spider-gazelle/ssh2.cr
libssh2 binding for Crystal language
https://github.com/spider-gazelle/ssh2.cr
Last synced: about 2 months ago
JSON representation
libssh2 binding for Crystal language
- Host: GitHub
- URL: https://github.com/spider-gazelle/ssh2.cr
- Owner: spider-gazelle
- License: mit
- Created: 2019-02-02T21:20:55.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-05T20:48:13.000Z (8 months ago)
- Last Synced: 2024-06-21T18:11:25.863Z (3 months ago)
- Language: Crystal
- Size: 96.7 KB
- Stars: 43
- Watchers: 4
- Forks: 7
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - ssh2.cr - Bindings for libssh2 library (C bindings)
- awesome-crystal - ssh2.cr - Bindings for libssh2 library (C bindings)
README
# ssh2.cr
[![CI](https://github.com/spider-gazelle/ssh2.cr/actions/workflows/ci.yml/badge.svg)](https://github.com/spider-gazelle/ssh2.cr/actions/workflows/ci.yml)
This library provides binding for libssh2 library.
# Requirements
- Crystal language version 0.7.0 and higher.
- libssh2 version 1.5.0 or higherYou 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:
```crystal
require "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:
```crystal
require "ssh2"session = SSH2::Session.open("localhost", 2222)
session.login("root", "somepassword")
channel = session.open_session# request the terminal has echo mode off
channel.request_pty("vt100", [{SSH2::TerminalMode::ECHO, 0u32}])
channel.shell# Send commands
spawn {
list = ["ls\n", "ps aux\n", "uptime\n"]
loop do
channel.write(list.sample(1)[0].to_slice)
sleep 3
end
}# Receive responses
raw_data = Bytes.new(2048)
loop do
bytes_read = channel.read(raw_data)
puts String.new(raw_data[0, bytes_read])
end
```An example of using SFTP API:
```crystal
require "ssh2"SSH2::Session.open("localhost", 2222) do |session|
# NOTE:: ED25519 based keys only work on latest versions of libssh2
session.login_with_pubkey("root", "./spec/keys/id_rsa", "./spec/keys/id_rsa.pub")
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.
Many thanks to the original author [Kent Sibilev](https://github.com/datanoise/ssh2.cr)