em-ssh 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +23 -2
- data/bin/em-ssh-shell +21 -23
- data/lib/em-ssh/shell.rb +9 -7
- data/lib/em-ssh/version.rb +1 -1
- metadata +9 -9
data/README.md
CHANGED
@@ -56,8 +56,24 @@ See [http://net-ssh.github.com/ssh/v2/api/index.html](http://net-ssh.github.com/
|
|
56
56
|
Em-ssh provides an exepct-like shell abstraction layer on top of net-ssh in EM::Ssh::Shell
|
57
57
|
|
58
58
|
### Example
|
59
|
+
require 'em-ssh/shell'
|
59
60
|
EM.run {
|
60
|
-
|
61
|
+
EM::Ssh::Shell.new(host, 'caleb', "") do |shell|
|
62
|
+
shell.should be_a(EventMachine::Ssh::Shell)
|
63
|
+
shell.wait_for(']$')
|
64
|
+
shell.send_and_wait('uname -a', ']$')
|
65
|
+
shell.wait_for(']$')
|
66
|
+
shell.send_and_wait('/sbin/ifconfig -a', ']$')
|
67
|
+
timer.cancel
|
68
|
+
EM.stop
|
69
|
+
end
|
70
|
+
}
|
71
|
+
|
72
|
+
#### Synchrony Example
|
73
|
+
require 'em-ssh/shell'
|
74
|
+
EM.run {
|
75
|
+
Fiber.new {
|
76
|
+
shell = EM::Ssh::Shell.new(host, 'caleb', '')
|
61
77
|
shell.wait_for(']$')
|
62
78
|
shell.send_and_wait('sudo su -', 'password for caleb: ')
|
63
79
|
shell.send_and_wait('password', ']$')
|
@@ -65,7 +81,7 @@ Em-ssh provides an exepct-like shell abstraction layer on top of net-ssh in EM::
|
|
65
81
|
# ...
|
66
82
|
shell.send_and_wait('exit', ']$')
|
67
83
|
shell.send_data('exit')
|
68
|
-
|
84
|
+
}.resume
|
69
85
|
}
|
70
86
|
|
71
87
|
|
@@ -75,6 +91,11 @@ See bin/em-ssh for an example of a basic replacement for system ssh.
|
|
75
91
|
|
76
92
|
See bin/em-ssh-shell for a more complex example usage of Shell.
|
77
93
|
|
94
|
+
## Known Issues
|
95
|
+
|
96
|
+
Em-ssh relies on Fibers. MRI 1.9.2-p290 on OSX Lion has been known to segfault when using Fibers.
|
97
|
+
|
98
|
+
|
78
99
|
|
79
100
|
##Copyright
|
80
101
|
Copyright (c) 2011 Caleb Crane
|
data/bin/em-ssh-shell
CHANGED
@@ -47,27 +47,25 @@ abort("command is required") if commands.empty?
|
|
47
47
|
|
48
48
|
|
49
49
|
EM.run do
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end # :childless
|
72
|
-
}.resume
|
50
|
+
EM::Ssh::Shell.new(host, options[:user], options[:password]) do |shell|
|
51
|
+
commands.each do |command|
|
52
|
+
mys = shell.split
|
53
|
+
mys.on(:closed) { info("#{mys} has closed") }
|
54
|
+
|
55
|
+
EM.next_tick do
|
56
|
+
Fiber.new {
|
57
|
+
debug("#{mys} waited for: '#{mys.wait_for(waitstr)}'")
|
58
|
+
debug("#{mys} send: #{command.inspect}")
|
59
|
+
puts "#{mys} result: '#{mys.send_and_wait(command, waitstr)}'"
|
60
|
+
mys.close
|
61
|
+
}.resume
|
62
|
+
end
|
63
|
+
end # |command|
|
64
|
+
|
65
|
+
shell.on(:childless) do
|
66
|
+
info("#{shell}'s children all closed")
|
67
|
+
shell.close
|
68
|
+
EM.stop
|
69
|
+
end # :childless
|
70
|
+
end # |shell|
|
73
71
|
end # EM.run
|
data/lib/em-ssh/shell.rb
CHANGED
@@ -51,7 +51,7 @@ module EventMachine
|
|
51
51
|
attr_reader :parent
|
52
52
|
# @return [String] a string (\r\n) to append to every command
|
53
53
|
def line_terminator
|
54
|
-
@line_terminator ||= "\
|
54
|
+
@line_terminator ||= "\n"
|
55
55
|
end
|
56
56
|
# [String]
|
57
57
|
attr_writer :line_terminator
|
@@ -76,7 +76,7 @@ module EventMachine
|
|
76
76
|
@parent = opts[:parent]
|
77
77
|
@children = []
|
78
78
|
|
79
|
-
block_given? ? open(&blk) : open
|
79
|
+
block_given? ? Fiber.new { open(&blk) }.resume : open
|
80
80
|
end
|
81
81
|
|
82
82
|
# Close the connection to the server and all child shells.
|
@@ -125,7 +125,7 @@ module EventMachine
|
|
125
125
|
# @return [String] the contents of the buffer
|
126
126
|
def wait_for(strregex, opts = { })
|
127
127
|
raise ClosedChannel if closed?
|
128
|
-
debug("wait_for(#{strregex}, #{opts})")
|
128
|
+
debug("wait_for(#{strregex.inspect}, #{opts})")
|
129
129
|
opts = { :timeout => @timeout, :halt_on_timeout => @halt_on_timeout }.merge(opts)
|
130
130
|
buffer = ''
|
131
131
|
found = nil
|
@@ -146,9 +146,8 @@ module EventMachine
|
|
146
146
|
buffer = "#{buffer}#{data}"
|
147
147
|
if strregex.is_a?(Regexp) ? buffer.match(strregex) : buffer.include?(strregex)
|
148
148
|
timer.respond_to?(:cancel) && timer.cancel
|
149
|
-
result = buffer.clone
|
150
149
|
shell.on_data {|c,d| }
|
151
|
-
f.resume(
|
150
|
+
f.resume(buffer)
|
152
151
|
end
|
153
152
|
end # |ch,data|
|
154
153
|
|
@@ -159,7 +158,8 @@ module EventMachine
|
|
159
158
|
# Open a shell on the server.
|
160
159
|
# You generally don't need to call this.
|
161
160
|
# @return [self]
|
162
|
-
def open
|
161
|
+
def open(&blk)
|
162
|
+
debug("open(#{blk})")
|
163
163
|
f = Fiber.current
|
164
164
|
connect unless connected?
|
165
165
|
|
@@ -171,6 +171,7 @@ module EventMachine
|
|
171
171
|
raise ConnectionError, "Failed to create shell." unless success
|
172
172
|
debug "***** shell open: #{shell}"
|
173
173
|
@shell = shell
|
174
|
+
Fiber.new { yield(self) if block_given? }.resume
|
174
175
|
f.resume(self)
|
175
176
|
end # |shell,success|
|
176
177
|
end # |pty,suc|
|
@@ -201,6 +202,7 @@ module EventMachine
|
|
201
202
|
# Does not open the shell; use #open or #split
|
202
203
|
# You generally won't need to call this on your own.
|
203
204
|
def connect
|
205
|
+
return if connected?
|
204
206
|
f = Fiber.current
|
205
207
|
::EM::Ssh.start(host, user, connect_opts) do |connection|
|
206
208
|
@connection = connection
|
@@ -215,7 +217,7 @@ module EventMachine
|
|
215
217
|
# @see #send_and_wait
|
216
218
|
# @param [String] d the data to send encoded as a string
|
217
219
|
def send_data(d)
|
218
|
-
#debug("send_data: #{d.
|
220
|
+
#debug("send_data: #{d.dump}#{line_terminator.dump}")
|
219
221
|
shell.send_data("#{d}#{line_terminator}")
|
220
222
|
end
|
221
223
|
|
data/lib/em-ssh/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-ssh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-10-22 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|
16
|
-
requirement: &
|
16
|
+
requirement: &70157590491700 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70157590491700
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: net-ssh
|
27
|
-
requirement: &
|
27
|
+
requirement: &70157590491240 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70157590491240
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: ruby-termios
|
38
|
-
requirement: &
|
38
|
+
requirement: &70157590490760 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70157590490760
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: highline
|
49
|
-
requirement: &
|
49
|
+
requirement: &70157590490300 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70157590490300
|
58
58
|
description: ''
|
59
59
|
email:
|
60
60
|
- em-ssh@simulacre.org
|