em-ssh 0.3.0 → 0.3.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 +91 -82
- data/lib/em-ssh/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -3,56 +3,59 @@ Em-ssh is a net-ssh adapter for EventMachine. For the most part you can take any
|
|
3
3
|
|
4
4
|
Em-ssh is not associated with the Jamis Buck's [net-ssh](http://net-ssh.github.com/) library. Please report any bugs with em-ssh to [https://github.com/simulacre/em-ssh/issues](https://github.com/simulacre/em-ssh/issues)
|
5
5
|
##Installation
|
6
|
-
|
6
|
+
gem install em-ssh
|
7
7
|
|
8
8
|
##Synopsis
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
EM.run do
|
12
|
+
EM::Ssh.start(host, user, :password => password) do |connection|
|
13
|
+
connection.errback do |err|
|
14
|
+
$stderr.puts "#{err} (#{err.class})"
|
15
|
+
end
|
16
|
+
connection.callback do |ssh|
|
17
|
+
# capture all stderr and stdout output from a remote process
|
18
|
+
ssh.exec!('uname -a').tap {|r| puts "\nuname: #{r}"}
|
19
|
+
|
20
|
+
# capture only stdout matching a particular pattern
|
21
|
+
stdout = ""
|
22
|
+
ssh.exec!("ls -l /home") do |channel, stream, data|
|
23
|
+
stdout << data if stream == :stdout
|
13
24
|
end
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
ch.
|
33
|
-
|
34
|
-
|
35
|
-
# "on_data" is called when the process writes something to stdout
|
36
|
-
ch.on_data do |c, data|
|
37
|
-
$stdout.print data
|
38
|
-
end
|
39
|
-
|
40
|
-
# "on_extended_data" is called when the process writes something to stderr
|
41
|
-
ch.on_extended_data do |c, type, data|
|
42
|
-
$stderr.print data
|
43
|
-
end
|
44
|
-
|
45
|
-
ch.on_close { puts "done!" }
|
25
|
+
puts "\n#{stdout}"
|
26
|
+
|
27
|
+
# run multiple processes in parallel to completion
|
28
|
+
ssh.exec('ping -c 1 www.google.com')
|
29
|
+
ssh.exec('ping -c 1 www.yahoo.com')
|
30
|
+
ssh.exec('ping -c 1 www.rakuten.co.jp')
|
31
|
+
|
32
|
+
#open a new channel and configure a minimal set of callbacks, then wait for the channel to finishes (closees).
|
33
|
+
channel = ssh.open_channel do |ch|
|
34
|
+
ch.exec "/usr/local/bin/ruby /path/to/file.rb" do |ch, success|
|
35
|
+
raise "could not execute command" unless success
|
36
|
+
|
37
|
+
# "on_data" is called when the process writes something to stdout
|
38
|
+
ch.on_data do |c, data|
|
39
|
+
$stdout.print data
|
40
|
+
end
|
41
|
+
|
42
|
+
# "on_extended_data" is called when the process writes something to stderr
|
43
|
+
ch.on_extended_data do |c, type, data|
|
44
|
+
$stderr.print data
|
46
45
|
end
|
46
|
+
|
47
|
+
ch.on_close { puts "done!" }
|
47
48
|
end
|
48
|
-
|
49
|
-
channel.wait
|
50
|
-
|
51
|
-
ssh.close
|
52
|
-
EM.stop
|
53
49
|
end
|
54
|
-
|
55
|
-
|
50
|
+
|
51
|
+
channel.wait
|
52
|
+
|
53
|
+
ssh.close
|
54
|
+
EM.stop
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
56
59
|
|
57
60
|
See [http://net-ssh.github.com/ssh/v2/api/index.html](http://net-ssh.github.com/ssh/v2/api/index.html)
|
58
61
|
|
@@ -61,53 +64,59 @@ See [http://net-ssh.github.com/ssh/v2/api/index.html](http://net-ssh.github.com/
|
|
61
64
|
Em-ssh provides an expect-like shell abstraction layer on top of net-ssh in EM::Ssh::Shell
|
62
65
|
|
63
66
|
### Example
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'em-ssh/shell'
|
70
|
+
EM.run do
|
71
|
+
EM::Ssh::Shell.new(host, ENV['USER'], "") do |shell|
|
72
|
+
shell.callback do
|
73
|
+
shell.expect('~]$ ')
|
74
|
+
shell.expect('~]$ ','uname -a')
|
75
|
+
shell.expect('~]$ ')
|
76
|
+
shell.expect('~]$ ', '/sbin/ifconfig -a')
|
77
|
+
EM.stop
|
78
|
+
end
|
79
|
+
shell.errback do
|
80
|
+
puts "error: #{err} (#{err.class})"
|
81
|
+
EM.stop
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
80
86
|
|
81
87
|
### Run Multiple Commands in Parallel
|
82
|
-
require 'em-ssh/shell'
|
83
|
-
EM.run do
|
84
|
-
EM::Ssh::Shell.new(host, ENV['USER'], '') do |shell|
|
85
|
-
shell.errback do |err|
|
86
|
-
puts "error: #{err} (#{err.class})"
|
87
|
-
EM.stop
|
88
|
-
end
|
89
88
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
89
|
+
```ruby
|
90
|
+
require 'em-ssh/shell'
|
91
|
+
EM.run do
|
92
|
+
EM::Ssh::Shell.new(host, ENV['USER'], '') do |shell|
|
93
|
+
shell.errback do |err|
|
94
|
+
puts "error: #{err} (#{err.class})"
|
95
|
+
EM.stop
|
96
|
+
end
|
97
|
+
|
98
|
+
shell.callback do
|
99
|
+
commands.clone.each do |command|
|
100
|
+
mys = shell.split # provides a second session over the same connection
|
101
|
+
mys.on(:closed) do
|
102
|
+
commands.delete(command)
|
103
|
+
EM.stop if commands.empty?
|
104
|
+
end
|
97
105
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
106
|
+
puts("waiting for: #{waitstr.inspect}")
|
107
|
+
# When given a block, Shell#expect does not 'block'
|
108
|
+
mys.expect(waitstr) do
|
109
|
+
puts "sending #{command.inspect} and waiting for #{waitstr.inspect}"
|
110
|
+
mys.expect(waitstr, command) do |result|
|
111
|
+
puts "#{mys} result: '#{result}'"
|
112
|
+
mys.close
|
106
113
|
end
|
107
114
|
end
|
108
115
|
end
|
109
116
|
end
|
110
117
|
end
|
118
|
+
end
|
119
|
+
```
|
111
120
|
|
112
121
|
## Other Examples
|
113
122
|
See bin/em-ssh for an example of a basic replacement for system ssh.
|
data/lib/em-ssh/version.rb
CHANGED