-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds executable tests for README examples; removes CWI hanging pry
- Loading branch information
Showing
9 changed files
with
198 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
readme.*.rb | ||
tags | ||
|
||
*.gem | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env ruby | ||
require File.expand_path("../arg-parser", __FILE__) | ||
|
||
host, user = require_host_user | ||
|
||
require "em-ssh" | ||
EM.run do | ||
EM::Ssh.start(host, user) do |connection| | ||
connection.errback do |err| | ||
$stderr.puts "#{err} (#{err.class})" | ||
EM.stop | ||
end | ||
connection.callback do |ssh| | ||
# capture all stderr and stdout output from a remote process | ||
ssh.exec!('uname -a').tap {|r| puts "\nuname: #{r}"} | ||
|
||
# capture only stdout matching a particular pattern | ||
stdout = "" | ||
ssh.exec!("ls -l /home") do |channel, stream, data| | ||
stdout << data if stream == :stdout | ||
end | ||
puts "\n#{stdout}" | ||
|
||
# run multiple processes in parallel to completion | ||
ssh.exec('ping -c 1 www.google.com') | ||
ssh.exec('ping -c 1 www.yahoo.com') | ||
ssh.exec('ping -c 1 www.rakuten.co.jp') | ||
|
||
#open a new channel and configure a minimal set of callbacks, then wait for the channel to finishes (closees). | ||
channel = ssh.open_channel do |ch| | ||
ch.exec "/usr/local/bin/ruby /path/to/file.rb" do |ch, success| | ||
raise "could not execute command" unless success | ||
|
||
# "on_data" is called when the process writes something to stdout | ||
ch.on_data do |c, data| | ||
$stdout.print data | ||
end | ||
|
||
# "on_extended_data" is called when the process writes something to stderr | ||
ch.on_extended_data do |c, type, data| | ||
$stderr.print data | ||
end | ||
|
||
ch.on_close { puts "done!" } | ||
end | ||
end | ||
|
||
channel.wait | ||
|
||
ssh.close | ||
EM.stop | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env ruby | ||
require File.expand_path("../arg-parser", __FILE__) | ||
|
||
host, user = require_host_user | ||
|
||
require 'em-ssh/shell' | ||
EM.run do | ||
EM::Ssh::Shell.new(host, user, "") do |shell| | ||
shell.callback do | ||
shell.expect(Regexp.escape('~]$ ')) | ||
$stderr.puts shell.expect(Regexp.escape('~]$ '),'uname -a') | ||
$stderr.puts shell.expect(Regexp.escape('~]$ '), '/sbin/ifconfig -a') | ||
EM.stop | ||
end | ||
shell.errback do |err| | ||
$stderr.puts "error: #{err} (#{err.class})" | ||
EM.stop | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env ruby | ||
require File.expand_path("../arg-parser", __FILE__) | ||
|
||
host, user = require_host_user | ||
|
||
require 'em-ssh/shell' | ||
|
||
waitstr = Regexp.escape('~]$ ') | ||
commands = ["uname -a", "uptime", "ifconfig"] | ||
EM.run do | ||
EM::Ssh::Shell.new(host, user, "") do |shell| | ||
shell.errback do |err| | ||
$stderr.puts "error: #{err} (#{err.class})" | ||
EM.stop | ||
end | ||
|
||
shell.callback do | ||
commands.clone.each do |command| | ||
mys = shell.split # provides a second session over the same connection | ||
|
||
mys.on(:closed) do | ||
commands.delete(command) | ||
EM.stop if commands.empty? | ||
end | ||
|
||
mys.callback do | ||
$stderr.puts("waiting for: #{waitstr.inspect}") | ||
# When given a block, Shell#expect does not 'block' | ||
mys.expect(waitstr) do | ||
$stderr.puts "sending #{command.inspect} and waiting for #{waitstr.inspect}" | ||
mys.expect(waitstr, command) do |result| | ||
$stderr.puts "#{mys} result: '#{result}'" | ||
mys.close | ||
end | ||
end | ||
end | ||
|
||
mys.errback do |err| | ||
$stderr.puts "subshell error: #{err} (#{err.class})" | ||
mys.close | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env ruby | ||
require "em-ssh" | ||
require File.expand_path("../arg-parser", __FILE__) | ||
|
||
host, user = require_host_user | ||
|
||
waitstr = Regexp.escape('~]$ ') | ||
commands = ["uname -a", "uptime", "ifconfig"] | ||
|
||
require 'em-ssh/shell' | ||
EM.run do | ||
EM::Ssh::Shell.new(host, user, "") do |shell| | ||
shell.errback do |err| | ||
$stderr.puts "error: #{err} (#{err.class})" | ||
$stderr.puts err.backtrace | ||
EM.stop | ||
end | ||
|
||
shell.callback do | ||
commands.clone.each do |command| | ||
Fiber.new { | ||
# When given a block Shell#split will close the Shell after | ||
# the block returns. If a block is given it must be called | ||
# within a Fiber. | ||
sresult = shell.split do |mys| | ||
mys.on(:closed) do | ||
commands.delete(command) | ||
EM.stop if commands.empty? | ||
end | ||
mys.errback do |err| | ||
$stderr.puts "subshell error: #{err} (#{err.class})" | ||
mys.close | ||
end | ||
|
||
mys.expect(waitstr) | ||
result = mys.expect(waitstr, command) | ||
$stderr.puts "#{mys} result: '#{result.inspect}'" | ||
result | ||
end | ||
$stderr.puts "split result: #{sresult.inspect} +++" | ||
}.resume | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "optparse" | ||
|
||
def require_host_user | ||
summary =<<-SUMM | ||
A simple utility corresponding to a usage example in the | ||
[em-ssh](https://github.com/simulacre/em-ssh/) README. It requries a | ||
single argument user@host. You use must have an authorized ssh public | ||
key on the target host. After logging in a few non-destructive commands | ||
will be executed. | ||
SUMM | ||
|
||
host = nil | ||
user = nil | ||
user,host = ARGV[0].split("@") if ARGV[0] | ||
abort "Usage: #{File.basename($0)} user@host\n\n#{summary}" if ARGV.include?("-h") || host.nil? || user.nil? | ||
[host, user] | ||
end |