Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First release #1

Merged
merged 4 commits into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Conn

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/conn`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem
Super simple ssh(or something) commander

## Installation

Expand All @@ -22,7 +20,25 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
```ruby
require 'conn'

Conn.ssh "target.exapmle.jp" do |input|
input << "uptime"
sleep 0.5
input << "uname -a"
end

# or

include Conn::DSL

ssh "target.exapmle.jp" do |input|
#...
end
```

NOTE: conn gem respects your `~/.ssh/config` automatically.

## Development

Expand Down
3 changes: 3 additions & 0 deletions conn.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "net-ssh"
spec.add_dependency "colored"

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
end
3 changes: 2 additions & 1 deletion lib/conn.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "conn/version"

module Conn
# Your code goes here...
end

require "conn/dsl"
83 changes: 83 additions & 0 deletions lib/conn/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'thread'
require 'etc'
require 'net/ssh'
require 'colored'

require 'conn/pty_ssh'

module Conn
module DSL
def ssh(hostname, &blk)
config = Net::SSH::Config.for(hostname)
user = config[:user] || Etc.getlogin
queue = Queue.new
cmd_loop = Thread.new do
blk.call(queue)
queue.respond_to?(:close) ? queue.close : (queue << false)
end
Net::SSH.start(hostname, user, config) do |ssh|
ssh.loop do
msg = queue.pop
if msg
input(msg)
ssh.exec!(msg) do |chan, stream, data|
if stream == :stdout
stdout(data)
else
stderr(data)
end
end
true
else
false
end
end
end
cmd_loop.join if cmd_loop.alive?
end

def input(str)
puts "%s %s" % ["SSH<<".green.bold, str.yellow]
end

def stdout(str)
puts "%s %s" % ["SSH>>".magenta.bold, str.cyan]
end

def stderr(str)
puts "%s %s" % ["SSH!>".red.bold, str.red]
end

using Conn::PtySSH
def ssh_pty(hostname, &blk)
config = Net::SSH::Config.for(hostname)
user = config[:user] || Etc.getlogin
queue = Queue.new
cmd_loop = Thread.new do
blk.call(queue)
queue.respond_to?(:close) ? queue.close : (queue << false)
end
Net::SSH.start(hostname, user, config) do |ssh|
ssh.loop do
msg = queue.pop
if msg
input(msg)
ssh.exec!(msg) do |chan, stream, data|
if stream == :stdout
stdout(data)
else
stderr(data)
end
end
true
else
false
end
end
end
cmd_loop.join if cmd_loop.alive?
end
end

extend DSL
end
57 changes: 57 additions & 0 deletions lib/conn/pty_ssh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
if RUBY_VERSION < '2.1'
require 'refinements'
end
require 'net/ssh/connection/session'

module Conn
module PtySSH
refine Net::SSH::Connection::Session do
alias open_channel_orig open_channel
def open_channel(type="session", *extra, &on_confirm)
on_confirm_with_tty = Proc.new do |ch|
ch.request_pty
on_confirm.call(ch)
end
open_channel_orig(type, *extra, &on_confirm_with_tty)
end

def exec(command, &block)
open_channel do |channel|
channel.exec(command) do |ch, success|
raise "could not execute command: #{command.inspect}" unless success

channel.on_data do |ch2, data|
if block
block.call(ch2, :stdout, data)
else
$stdout.print(data)
end
end

channel.on_extended_data do |ch2, type, data|
if block
block.call(ch2, :stderr, data)
else
$stderr.print(data)
end
end
end
end
end

def exec!(command, &block)
block_or_concat = block || Proc.new do |ch, type, data|
ch[:result] ||= ""
ch[:result] << data
end

channel = exec(command, &block_or_concat)
channel.wait

channel[:result] ||= "" unless block

return channel[:result]
end
end
end
end
2 changes: 1 addition & 1 deletion lib/conn/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Conn
VERSION = "0.0.1.pre"
VERSION = "0.0.1"
end