net-ssh 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/doc/LICENSE-BSD +27 -0
  2. data/doc/LICENSE-GPL +280 -0
  3. data/doc/LICENSE-RUBY +56 -0
  4. data/doc/README +13 -0
  5. data/doc/manual-html/chapter-1.html +333 -0
  6. data/doc/manual-html/chapter-2.html +455 -0
  7. data/doc/manual-html/chapter-3.html +413 -0
  8. data/doc/manual-html/chapter-4.html +353 -0
  9. data/doc/manual-html/chapter-5.html +393 -0
  10. data/doc/manual-html/chapter-6.html +296 -0
  11. data/doc/manual-html/index.html +217 -0
  12. data/doc/manual-html/manual.css +192 -0
  13. data/doc/manual/chapter.erb +18 -0
  14. data/doc/manual/example.erb +18 -0
  15. data/doc/manual/index.erb +29 -0
  16. data/doc/manual/manual.css +192 -0
  17. data/doc/manual/manual.rb +240 -0
  18. data/doc/manual/manual.yml +67 -0
  19. data/doc/manual/page.erb +87 -0
  20. data/doc/manual/parts/channels_callbacks.txt +32 -0
  21. data/doc/manual/parts/channels_loop.txt +14 -0
  22. data/doc/manual/parts/channels_open.txt +20 -0
  23. data/doc/manual/parts/channels_operations.txt +15 -0
  24. data/doc/manual/parts/channels_types.txt +3 -0
  25. data/doc/manual/parts/channels_what_are.txt +7 -0
  26. data/doc/manual/parts/exec_channels.txt +28 -0
  27. data/doc/manual/parts/exec_open.txt +51 -0
  28. data/doc/manual/parts/exec_popen3.txt +35 -0
  29. data/doc/manual/parts/forward_direct.txt +37 -0
  30. data/doc/manual/parts/forward_handlers.txt +16 -0
  31. data/doc/manual/parts/forward_intro.txt +18 -0
  32. data/doc/manual/parts/forward_local.txt +18 -0
  33. data/doc/manual/parts/forward_remote.txt +14 -0
  34. data/doc/manual/parts/intro_author.txt +1 -0
  35. data/doc/manual/parts/intro_getting.txt +39 -0
  36. data/doc/manual/parts/intro_license.txt +6 -0
  37. data/doc/manual/parts/intro_support.txt +7 -0
  38. data/doc/manual/parts/intro_what_is.txt +7 -0
  39. data/doc/manual/parts/intro_what_is_not.txt +3 -0
  40. data/doc/manual/parts/proxy_http.txt +52 -0
  41. data/doc/manual/parts/proxy_intro.txt +1 -0
  42. data/doc/manual/parts/proxy_socks.txt +23 -0
  43. data/doc/manual/parts/session_key.txt +66 -0
  44. data/doc/manual/parts/session_options.txt +42 -0
  45. data/doc/manual/parts/session_session.txt +14 -0
  46. data/doc/manual/parts/session_start.txt +49 -0
  47. data/doc/manual/tutorial.erb +30 -0
  48. data/examples/channel-demo.rb +81 -0
  49. data/examples/port-forward.rb +51 -0
  50. data/examples/process-demo.rb +91 -0
  51. data/examples/remote-net-port-forward.rb +45 -0
  52. data/examples/remote-port-forward.rb +80 -0
  53. data/examples/tail-demo.rb +49 -0
  54. data/lib/net/ssh.rb +52 -0
  55. data/lib/net/ssh/connection/channel.rb +411 -0
  56. data/lib/net/ssh/connection/constants.rb +47 -0
  57. data/lib/net/ssh/connection/driver.rb +343 -0
  58. data/lib/net/ssh/connection/services.rb +72 -0
  59. data/lib/net/ssh/connection/term.rb +90 -0
  60. data/lib/net/ssh/errors.rb +27 -0
  61. data/lib/net/ssh/proxy/errors.rb +34 -0
  62. data/lib/net/ssh/proxy/http.rb +126 -0
  63. data/lib/net/ssh/proxy/socks4.rb +83 -0
  64. data/lib/net/ssh/proxy/socks5.rb +160 -0
  65. data/lib/net/ssh/service/forward/driver.rb +319 -0
  66. data/lib/net/ssh/service/forward/local-network-handler.rb +74 -0
  67. data/lib/net/ssh/service/forward/remote-network-handler.rb +81 -0
  68. data/lib/net/ssh/service/forward/services.rb +76 -0
  69. data/lib/net/ssh/service/process/driver.rb +153 -0
  70. data/lib/net/ssh/service/process/open.rb +193 -0
  71. data/lib/net/ssh/service/process/popen3.rb +160 -0
  72. data/lib/net/ssh/service/process/services.rb +66 -0
  73. data/lib/net/ssh/service/services.rb +44 -0
  74. data/lib/net/ssh/session.rb +242 -0
  75. data/lib/net/ssh/transport/algorithm-negotiator.rb +267 -0
  76. data/lib/net/ssh/transport/compress/compressor.rb +53 -0
  77. data/lib/net/ssh/transport/compress/decompressor.rb +53 -0
  78. data/lib/net/ssh/transport/compress/none-compressor.rb +39 -0
  79. data/lib/net/ssh/transport/compress/none-decompressor.rb +39 -0
  80. data/lib/net/ssh/transport/compress/services.rb +68 -0
  81. data/lib/net/ssh/transport/compress/zlib-compressor.rb +60 -0
  82. data/lib/net/ssh/transport/compress/zlib-decompressor.rb +52 -0
  83. data/lib/net/ssh/transport/constants.rb +66 -0
  84. data/lib/net/ssh/transport/errors.rb +47 -0
  85. data/lib/net/ssh/transport/identity-cipher.rb +61 -0
  86. data/lib/net/ssh/transport/kex/dh-gex.rb +106 -0
  87. data/lib/net/ssh/transport/kex/dh.rb +231 -0
  88. data/lib/net/ssh/transport/kex/services.rb +60 -0
  89. data/lib/net/ssh/transport/ossl/buffer-factory.rb +52 -0
  90. data/lib/net/ssh/transport/ossl/buffer.rb +87 -0
  91. data/lib/net/ssh/transport/ossl/cipher-factory.rb +98 -0
  92. data/lib/net/ssh/transport/ossl/digest-factory.rb +51 -0
  93. data/lib/net/ssh/transport/ossl/hmac-factory.rb +71 -0
  94. data/lib/net/ssh/transport/ossl/hmac/hmac.rb +62 -0
  95. data/lib/net/ssh/transport/ossl/hmac/md5-96.rb +44 -0
  96. data/lib/net/ssh/transport/ossl/hmac/md5.rb +46 -0
  97. data/lib/net/ssh/transport/ossl/hmac/none.rb +46 -0
  98. data/lib/net/ssh/transport/ossl/hmac/services.rb +68 -0
  99. data/lib/net/ssh/transport/ossl/hmac/sha1-96.rb +44 -0
  100. data/lib/net/ssh/transport/ossl/hmac/sha1.rb +45 -0
  101. data/lib/net/ssh/transport/ossl/key-factory.rb +113 -0
  102. data/lib/net/ssh/transport/ossl/services.rb +149 -0
  103. data/lib/net/ssh/transport/packet-stream.rb +210 -0
  104. data/lib/net/ssh/transport/services.rb +146 -0
  105. data/lib/net/ssh/transport/session.rb +296 -0
  106. data/lib/net/ssh/transport/version-negotiator.rb +73 -0
  107. data/lib/net/ssh/userauth/agent.rb +218 -0
  108. data/lib/net/ssh/userauth/constants.rb +35 -0
  109. data/lib/net/ssh/userauth/driver.rb +176 -0
  110. data/lib/net/ssh/userauth/methods/hostbased.rb +119 -0
  111. data/lib/net/ssh/userauth/methods/password.rb +70 -0
  112. data/lib/net/ssh/userauth/methods/publickey.rb +137 -0
  113. data/lib/net/ssh/userauth/methods/services.rb +63 -0
  114. data/lib/net/ssh/userauth/services.rb +126 -0
  115. data/lib/net/ssh/userauth/userkeys.rb +258 -0
  116. data/lib/net/ssh/util/buffer.rb +274 -0
  117. data/lib/net/ssh/util/openssl.rb +146 -0
  118. data/lib/net/ssh/util/prompter.rb +73 -0
  119. data/lib/net/ssh/version.rb +29 -0
  120. data/test/ALL-TESTS.rb +21 -0
  121. data/test/connection/tc_channel.rb +136 -0
  122. data/test/connection/tc_driver.rb +287 -0
  123. data/test/connection/tc_integration.rb +85 -0
  124. data/test/proxy/tc_http.rb +209 -0
  125. data/test/proxy/tc_socks4.rb +148 -0
  126. data/test/proxy/tc_socks5.rb +214 -0
  127. data/test/service/forward/tc_driver.rb +289 -0
  128. data/test/service/forward/tc_local_network_handler.rb +123 -0
  129. data/test/service/forward/tc_remote_network_handler.rb +108 -0
  130. data/test/service/process/tc_driver.rb +79 -0
  131. data/test/service/process/tc_integration.rb +117 -0
  132. data/test/service/process/tc_open.rb +179 -0
  133. data/test/service/process/tc_popen3.rb +164 -0
  134. data/test/tc_integration.rb +79 -0
  135. data/test/transport/compress/tc_none_compress.rb +41 -0
  136. data/test/transport/compress/tc_none_decompress.rb +45 -0
  137. data/test/transport/compress/tc_zlib_compress.rb +61 -0
  138. data/test/transport/compress/tc_zlib_decompress.rb +48 -0
  139. data/test/transport/kex/tc_dh.rb +304 -0
  140. data/test/transport/kex/tc_dh_gex.rb +70 -0
  141. data/test/transport/ossl/fixtures/dsa-encrypted +15 -0
  142. data/test/transport/ossl/fixtures/dsa-encrypted-bad +15 -0
  143. data/test/transport/ossl/fixtures/dsa-unencrypted +12 -0
  144. data/test/transport/ossl/fixtures/dsa-unencrypted-bad +12 -0
  145. data/test/transport/ossl/fixtures/dsa-unencrypted.pub +1 -0
  146. data/test/transport/ossl/fixtures/not-a-private-key +4 -0
  147. data/test/transport/ossl/fixtures/not-supported +2 -0
  148. data/test/transport/ossl/fixtures/rsa-encrypted +18 -0
  149. data/test/transport/ossl/fixtures/rsa-encrypted-bad +18 -0
  150. data/test/transport/ossl/fixtures/rsa-unencrypted +15 -0
  151. data/test/transport/ossl/fixtures/rsa-unencrypted-bad +15 -0
  152. data/test/transport/ossl/fixtures/rsa-unencrypted.pub +1 -0
  153. data/test/transport/ossl/hmac/tc_hmac.rb +58 -0
  154. data/test/transport/ossl/hmac/tc_md5.rb +50 -0
  155. data/test/transport/ossl/hmac/tc_md5_96.rb +50 -0
  156. data/test/transport/ossl/hmac/tc_none.rb +50 -0
  157. data/test/transport/ossl/hmac/tc_sha1.rb +50 -0
  158. data/test/transport/ossl/hmac/tc_sha1_96.rb +50 -0
  159. data/test/transport/ossl/tc_buffer.rb +97 -0
  160. data/test/transport/ossl/tc_buffer_factory.rb +67 -0
  161. data/test/transport/ossl/tc_cipher_factory.rb +84 -0
  162. data/test/transport/ossl/tc_digest_factory.rb +39 -0
  163. data/test/transport/ossl/tc_hmac_factory.rb +72 -0
  164. data/test/transport/ossl/tc_key_factory.rb +199 -0
  165. data/test/transport/tc_algorithm_negotiator.rb +169 -0
  166. data/test/transport/tc_identity_cipher.rb +52 -0
  167. data/test/transport/tc_integration.rb +110 -0
  168. data/test/transport/tc_packet_stream.rb +183 -0
  169. data/test/transport/tc_session.rb +283 -0
  170. data/test/transport/tc_version_negotiator.rb +86 -0
  171. data/test/userauth/methods/tc_hostbased.rb +136 -0
  172. data/test/userauth/methods/tc_password.rb +89 -0
  173. data/test/userauth/methods/tc_publickey.rb +167 -0
  174. data/test/userauth/tc_agent.rb +223 -0
  175. data/test/userauth/tc_driver.rb +190 -0
  176. data/test/userauth/tc_integration.rb +81 -0
  177. data/test/userauth/tc_userkeys.rb +265 -0
  178. data/test/util/tc_buffer.rb +217 -0
  179. metadata +256 -0
@@ -0,0 +1,413 @@
1
+ <html>
2
+ <head>
3
+ <title>Net::SSH Manual :: Chapter 3: Channels</title>
4
+ <link type="text/css" rel="stylesheet" href="manual.css" />
5
+ </head>
6
+
7
+ <body>
8
+ <div id="banner">
9
+ <table border='0' cellpadding='0' cellspacing='0' width='100%'>
10
+ <tr><td valign='top' align='left'>
11
+ <div class="title">
12
+ <span class="product">Net::SSH&mdash;</span><br />
13
+ <span class="tagline">Secure Shell for Ruby</span>
14
+ </div>
15
+ </td><td valign='middle' align='right'>
16
+ <div class="info">
17
+ Net::SSH Version: <strong>0.5.0</strong><br />
18
+ Manual Last Updated: <strong>2004-11-23 20:18 GMT</strong>
19
+ </div>
20
+ </td></tr>
21
+ </table>
22
+ </div>
23
+
24
+ <table border='0' width='100%' cellpadding='0' cellspacing='0'>
25
+ <tr><td valign='top'>
26
+
27
+ <div id="navigation">
28
+ <h1>Net::SSH Manual</h1>
29
+
30
+ <h2>Chapters</h2>
31
+ <ol type="I">
32
+
33
+ <li>
34
+ <a href="chapter-1.html">
35
+ Introduction
36
+ </a>
37
+
38
+ <ol type="1">
39
+
40
+ <li><a href="chapter-1.html#s1">What is Net::SSH?</a></li>
41
+
42
+ <li><a href="chapter-1.html#s2">What isn&#8217;t Net::SSH?</a></li>
43
+
44
+ <li><a href="chapter-1.html#s3">Getting Net::SSH</a></li>
45
+
46
+ <li><a href="chapter-1.html#s4">License Information</a></li>
47
+
48
+ <li><a href="chapter-1.html#s5">Support</a></li>
49
+
50
+ <li><a href="chapter-1.html#s6">About the Author</a></li>
51
+
52
+ </ol>
53
+ </li>
54
+
55
+ <li>
56
+ <a href="chapter-2.html">
57
+ Starting a Session
58
+ </a>
59
+
60
+ <ol type="1">
61
+
62
+ <li><a href="chapter-2.html#s1">Using Net::SSH.start</a></li>
63
+
64
+ <li><a href="chapter-2.html#s2">Using a Public/Private Key</a></li>
65
+
66
+ <li><a href="chapter-2.html#s3">Options</a></li>
67
+
68
+ <li><a href="chapter-2.html#s4">Using Net::SSH::Session</a></li>
69
+
70
+ </ol>
71
+ </li>
72
+
73
+ <li><strong>
74
+ <a href="chapter-3.html">
75
+ Channels
76
+ </a>
77
+ </strong> <big>&larr;</big>
78
+ <ol type="1">
79
+
80
+ <li><a href="chapter-3.html#s1">What are Channels?</a></li>
81
+
82
+ <li><a href="chapter-3.html#s2">Session.loop</a></li>
83
+
84
+ <li><a href="chapter-3.html#s3">Channel Types</a></li>
85
+
86
+ <li><a href="chapter-3.html#s4">Opening a Channel</a></li>
87
+
88
+ <li><a href="chapter-3.html#s5">Callbacks</a></li>
89
+
90
+ <li><a href="chapter-3.html#s6">Channel Operations</a></li>
91
+
92
+ </ol>
93
+ </li>
94
+
95
+ <li>
96
+ <a href="chapter-4.html">
97
+ Executing Commands
98
+ </a>
99
+
100
+ <ol type="1">
101
+
102
+ <li><a href="chapter-4.html#s1">Using Channels</a></li>
103
+
104
+ <li><a href="chapter-4.html#s2">Using #process.open</a></li>
105
+
106
+ <li><a href="chapter-4.html#s3">Using #process.popen3</a></li>
107
+
108
+ </ol>
109
+ </li>
110
+
111
+ <li>
112
+ <a href="chapter-5.html">
113
+ Port Forwarding
114
+ </a>
115
+
116
+ <ol type="1">
117
+
118
+ <li><a href="chapter-5.html#s1">Introduction</a></li>
119
+
120
+ <li><a href="chapter-5.html#s2">Local-to-Remote</a></li>
121
+
122
+ <li><a href="chapter-5.html#s3">Remote-to-Local</a></li>
123
+
124
+ <li><a href="chapter-5.html#s4">Direct Channels</a></li>
125
+
126
+ <li><a href="chapter-5.html#s5">Remote-to-Local Handlers</a></li>
127
+
128
+ </ol>
129
+ </li>
130
+
131
+ <li>
132
+ <a href="chapter-6.html">
133
+ Using Proxies
134
+ </a>
135
+
136
+ <ol type="1">
137
+
138
+ <li><a href="chapter-6.html#s1">Introduction</a></li>
139
+
140
+ <li><a href="chapter-6.html#s2"><span class="caps">HTTP</span></a></li>
141
+
142
+ <li><a href="chapter-6.html#s3"><span class="caps">SOCKS</span></a></li>
143
+
144
+ </ol>
145
+ </li>
146
+
147
+ </ol>
148
+
149
+ <h2>Other Documentation</h2>
150
+
151
+ <ul>
152
+ <li><a href="http://net-ssh.rubyforge.org/api/index.html">Net::SSH API</a></li>
153
+ <li><a href="http://rubyforge.org/tracker/?atid=1842&group_id=274&func=browse">Net::SSH FAQ</a></li>
154
+ </ul>
155
+
156
+ <h2>Tutorials</h2>
157
+ <ol>
158
+
159
+ </ol>
160
+
161
+ <p align="center"><strong>More To Come...</strong></p>
162
+
163
+ <div class="license">
164
+ <a href="http://creativecommons.org/licenses/by-sa/2.0/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights" /></a><br />
165
+ This manual is licensed under a <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons License</a>.
166
+ </div>
167
+ </div>
168
+
169
+ </td><td valign='top' width="100%">
170
+
171
+ <div id="content">
172
+
173
+ <h1>3. Channels</h1>
174
+
175
+
176
+
177
+ <h2>
178
+ <a name="s1"></a>
179
+ 3.1. What are Channels?
180
+ </h2>
181
+
182
+
183
+
184
+ <div class="section">
185
+ <p>The <span class="caps">SSH</span> protocol requires that requests for services on a remote machine be made over <em>channels</em>. A single <span class="caps">SSH</span> connection may contain multiple channels, all run simultaneously over that connection.</p>
186
+
187
+ <p>Each channel, in turn, represents the processing of a single service. When you invoke a process on the remote host with Net::SSH, a channel is opened for that invocation, and all input and output relevant to that process is sent through that channel. The connection itself simply manages the packets of all of the channels that it has open.</p>
188
+
189
+ <p>This means that, for instance, over a single <span class="caps">SSH</span> connection you could execute a process, download a file via <span class="caps">SFTP</span>, and forward any number of ports, all (seemingly) at the same time!</p>
190
+
191
+ <p>Naturally, they do not occur simultaneously, but rather work in a &#8220;time-share&#8221; fashion by sharing the bandwidth of the connection. Nevertheless, the fact that these channels exist make working with the <span class="caps">SSH</span> protocol a bit more challenging than simpler protocols (like <span class="caps">FTP</span>, HTTP, or Telnet).</p>
192
+ </div>
193
+
194
+
195
+
196
+ <h2>
197
+ <a name="s2"></a>
198
+ 3.2. Session.loop
199
+ </h2>
200
+
201
+
202
+
203
+ <div class="section">
204
+ <p>Because a session may be composed of multiple simultaneously operating channels, the Net::SSH interface works by means of <em>callbacks</em>. You specify actions that need to occur in response to various events, and when those events occur, the framework invokes the corresonding callbacks.</p>
205
+
206
+ <p>In order to allow the events to be processed in a continuous manner, you need to be sure to call the <code>loop</code> method of your session handle, after setting up any callbacks that you want to be executed. If you do not call the <code>loop</code> method, your session will terminate as soon as the block is exited, which means none of your carefully laid callbacks will ever be called.</p>
207
+
208
+ <p>The <code>loop</code> method is easy to invoke:</p>
209
+
210
+
211
+ <pre>
212
+ Net::SSH.start( 'host' ) do |session|
213
+ ...
214
+ session.loop
215
+ end
216
+ </pre>
217
+ <p>Incidentally, the <code>loop</code> method accepts an optional block, which if specified should return a &#8220;false&#8221; value when the loop should terminate. In the absense of a block, the loop will continue until there are no more open channels. Sometimes, however, you only want the loop to continue until some action occurs, at which time you then do some processing and then start the loop again.</p>
218
+ </div>
219
+
220
+
221
+
222
+ <h2>
223
+ <a name="s3"></a>
224
+ 3.3. Channel Types
225
+ </h2>
226
+
227
+
228
+
229
+ <div class="section">
230
+ <p>Each channel has a <em>type</em>. Usually, you will use &#8220;session&#8221; channels, but there are also &#8220;x11&#8221; channels, &#8220;forwarded-tcpip&#8221; channels, and &#8220;direct-tcpip&#8221; channels. Net::SSH currently has no support for &#8220;x11&#8221; channels. The &#8220;forwarded-tcpip&#8221; and &#8220;direct-tcpip&#8221; channels are managed internally via the port-forwarding interfaces.</p>
231
+
232
+ <p>The &#8220;session&#8221; channel type allows for a broad range of actions, including (but not limited to) <span class="caps">SFTP</span> requests and remote process execution.</p>
233
+ </div>
234
+
235
+
236
+
237
+ <h2>
238
+ <a name="s4"></a>
239
+ 3.4. Opening a Channel
240
+ </h2>
241
+
242
+
243
+
244
+ <div class="section">
245
+ <p>The simplest way to open a channel is via the <code>open_channel</code> method of Net::SSH::Session. By default, the channel will be of type &#8220;session&#8221;, but you can optionally specify the channel type and any extra data to initialize the channel with. You also pass a block to the <code>open_channel</code> invocation. This block will be called after the server has confirmed that the channel is valid and has been opened successfully.</p>
246
+
247
+ <p>The <code>open_channel</code> method always returns immediately&#8212;all it does is inform the server that a channel needs to be opened and then registers the associated block as the callback to be invoked when the channel is confirmed.</p>
248
+
249
+ <p>This behavior is typical of most of the methods in the Net::SSH <span class="caps">API</span>; they simply send a request to the server and then (optionally) register a callback. Very few of them actually block (pause) until the server responds.</p>
250
+
251
+ <p>Here is an example of opening a channel:</p>
252
+
253
+
254
+ <pre>
255
+ Net::SSH.start( 'host' ) do |session|
256
+ session.open_channel do |channel|
257
+ puts "channel successfully opened... closing..."
258
+ channel.close
259
+ end
260
+
261
+ session.loop
262
+ end
263
+ </pre>
264
+ <p>Note the use of the <code>close</code> method for the channel. Just like most methods in the Net::SSH <span class="caps">API</span>, it does not immediately close the channel, but instead sends a close request to the server and returns. When the server responds that the channel has been closed, the framework will then call any final callbacks for the channel and then remove it.</p>
265
+ </div>
266
+
267
+
268
+
269
+ <h2>
270
+ <a name="s5"></a>
271
+ 3.5. Callbacks
272
+ </h2>
273
+
274
+
275
+
276
+ <div class="section">
277
+ <p>There are various callbacks that may be registered on a channel. Registering a callback is as simple as invoking the corresponding method on the channel and giving it a block that should be invoked when the named action occurs. The following table describes each callback and how it is used.</p>
278
+
279
+ <table class="list">
280
+ <tr>
281
+ <th>Name </th>
282
+ <th>Description </th>
283
+ </tr>
284
+ <tr>
285
+ <td style="vertical-align:top;text-align:center;"><code>on_close</code> </td>
286
+ <td> This callback should accept a single parameter: the channel being closed. It is called immediately after the channel is removed from the connection. The callback should not send any data through the channel, since at this point the channel is no longer connected to the remote host.</td>
287
+ </tr>
288
+ <tr>
289
+ <td style="vertical-align:top;text-align:center;"><code>on_confirm_failed</code> </td>
290
+ <td> This callback should accept four parameters: the channel instance, the reason code, a description of why the confirm failed, and the language code for the message. It is called immediately after the server has indicated that a channel could not be opened.</td>
291
+ </tr>
292
+ <tr>
293
+ <td style="vertical-align:top;text-align:center;"><code>on_confirm_open</code> </td>
294
+ <td> This callback should accept a single parameter: the channel being opened. It is called immediately after the server has confirmed that the channel has been opened. This callback is typically set by a block passed to an <code>#open_channel</code> call.</td>
295
+ </tr>
296
+ <tr>
297
+ <td style="vertical-align:top;text-align:center;"><code>on_data</code> </td>
298
+ <td> This callback is invoked when data is received over the channel from the remote server. This data typically corresponds to the remote process&#8217;s <code>stdout</code> stream. The channel should accept two parameters: the channel itself, and the data being received.</td>
299
+ </tr>
300
+ <tr>
301
+ <td style="vertical-align:top;text-align:center;"><code>on_eof</code> </td>
302
+ <td> This callback is called when the server indicates that no more data will be sent <em>from the server</em> over this channel. Your program is still welcome to send data to the server, but you are guaranteed at this point that your <code>on_data</code> and <code>on_extended_data</code> callbacks will no longer be called for this channel. The callback should accept a single parameter, the channel itself.</td>
303
+ </tr>
304
+ <tr>
305
+ <td style="vertical-align:top;text-align:center;"><code>on_extended_data</code> </td>
306
+ <td> This callback is called when <em>extended data</em> is received from the server. There are (potentially) many <em>types</em> of extended data. The callback should accept three parameters: the channel, an integer indicating the type of the data, and the data itself. Right now, you can pretty much count on the data type being a &#8220;1&#8221;, which corresponds to the remote process&#8217;s <code>stderr</code> stream. Other data types are not defined in the <span class="caps">SSH</span> specification, but that does not mean some <span class="caps">SSH</span> servers won&#8217;t try to invent their own.</td>
307
+ </tr>
308
+ <tr>
309
+ <td style="vertical-align:top;text-align:center;"><code>on_failure</code> </td>
310
+ <td> When a request is sent over a channel (via the <code>send_request</code> or <code>send_request_string</code> methods), it may either succeed or fail. If it fails, this callback will be invoked. It should take a single parameter: the channel itself.</td>
311
+ </tr>
312
+ <tr>
313
+ <td style="vertical-align:top;text-align:center;"><code>on_request</code> </td>
314
+ <td> When the server sends a &#8220;channel request&#8221; to the client, this callback will be invoked. Channel requests from the server typically indicate things like the exit status of a process. This callback should take three parameters: the channel, a boolean (indicating whether or not the server wants an explicit reply to this request), and the data from the request.</td>
315
+ </tr>
316
+ <tr>
317
+ <td style="vertical-align:top;text-align:center;"><code>on_success</code> </td>
318
+ <td> When a request is sent over a channel (via the <code>send_request</code> or <code>send_request_string</code> methods), it may either succeed or fail. If it succeeds, this callback will be invoked. It should take a single parameter: the channel itself.</td>
319
+ </tr>
320
+ <tr>
321
+ <td style="vertical-align:top;text-align:center;"><code>on_window_adjust</code> </td>
322
+ <td> When the server asks the client to adjust this channel&#8217;s window size, this callback will be invoked. It should accept two parameters: the channel, and the number of bytes to add the channel&#8217;s window size.</td>
323
+ </tr>
324
+ </table>
325
+
326
+
327
+
328
+ <p>In general, you will never need to register callbacks for <code>on_failure</code>, <code>on_request</code>, <code>on_success</code>, or <code>on_window_adjust</code>, unless you are needing to implement support for some subservice or piggy-backed protocol (like <span class="caps">SFTP</span>).</p>
329
+
330
+ <p>Following is an example of registering callbacks on a channel:</p>
331
+
332
+
333
+ <pre>
334
+ Net::SSH.start( 'host' ) do |session|
335
+ session.open_channel do |channel|
336
+ channel.on_close do |ch|
337
+ puts "channel closed successfully."
338
+ end
339
+ puts "closing channel..."
340
+ channel.close
341
+ end
342
+
343
+ session.loop
344
+ end
345
+ </pre>
346
+ </div>
347
+
348
+
349
+
350
+ <h2>
351
+ <a name="s6"></a>
352
+ 3.6. Channel Operations
353
+ </h2>
354
+
355
+
356
+
357
+ <div class="section">
358
+ <p>There are a variety of operations that may be performed on a channel. Some we&#8217;ve already mentioned, like registering callbacks, or closing the channel. Some of the other more common operations are listed (and described) in the following table.</p>
359
+
360
+ <table class="list">
361
+ <tr>
362
+ <th>Operation </th>
363
+ <th>Description </th>
364
+ </tr>
365
+ <tr>
366
+ <td> <code>#exec</code> </td>
367
+ <td> Executes a command asynchronously on this channel.</td>
368
+ </tr>
369
+ <tr>
370
+ <td> <code>#request_pty</code> </td>
371
+ <td> Requests that a pseudo-terminal (pty) be opened for this channel.</td>
372
+ </tr>
373
+ <tr>
374
+ <td> <code>#send_data</code> </td>
375
+ <td> Sends the given data string to the server via this channel. This is useful for sending data to a remote process, or sending an <span class="caps">SFTP</span> packet to the <span class="caps">SFTP</span> subsystem.</td>
376
+ </tr>
377
+ <tr>
378
+ <td> <code>#send_eof</code> </td>
379
+ <td> Tells the server that no further data will be sent from the client to the server. The client must honor this by not sending any more data (either normal or extended) to the server over this channel.</td>
380
+ </tr>
381
+ <tr>
382
+ <td> <code>#send_extended_data</code> </td>
383
+ <td> Sends a data string to the server, along with an integer describing its type. This is typically used to send <code>stderr</code> data.</td>
384
+ </tr>
385
+ <tr>
386
+ <td> <code>#send_request</code> </td>
387
+ <td> Sends a named request to the server for this channel. This is primarily used by implementations of protocols and subsystems that run on top of <span class="caps">SSH</span>.</td>
388
+ </tr>
389
+ <tr>
390
+ <td> <code>#send_signal</code> </td>
391
+ <td> Indicates that the server should send the given signal to the process on the other end of the channel.</td>
392
+ </tr>
393
+ <tr>
394
+ <td> <code>#subsystem</code> </td>
395
+ <td> Requests that the server start the given subsystem on this channel. This is how (for instance) the <span class="caps">SFTP</span> subsystem is invoked. </td>
396
+ </tr>
397
+ </table>
398
+
399
+
400
+
401
+ <p>See the <span class="caps">API</span> documentation for an exhaustive reference of all available channel
402
+ operations.</p>
403
+ </div>
404
+
405
+
406
+
407
+
408
+ </div>
409
+
410
+ </td></tr>
411
+ </table>
412
+ </body>
413
+ </html>
@@ -0,0 +1,353 @@
1
+ <html>
2
+ <head>
3
+ <title>Net::SSH Manual :: Chapter 4: Executing Commands</title>
4
+ <link type="text/css" rel="stylesheet" href="manual.css" />
5
+ </head>
6
+
7
+ <body>
8
+ <div id="banner">
9
+ <table border='0' cellpadding='0' cellspacing='0' width='100%'>
10
+ <tr><td valign='top' align='left'>
11
+ <div class="title">
12
+ <span class="product">Net::SSH&mdash;</span><br />
13
+ <span class="tagline">Secure Shell for Ruby</span>
14
+ </div>
15
+ </td><td valign='middle' align='right'>
16
+ <div class="info">
17
+ Net::SSH Version: <strong>0.5.0</strong><br />
18
+ Manual Last Updated: <strong>2004-11-23 20:18 GMT</strong>
19
+ </div>
20
+ </td></tr>
21
+ </table>
22
+ </div>
23
+
24
+ <table border='0' width='100%' cellpadding='0' cellspacing='0'>
25
+ <tr><td valign='top'>
26
+
27
+ <div id="navigation">
28
+ <h1>Net::SSH Manual</h1>
29
+
30
+ <h2>Chapters</h2>
31
+ <ol type="I">
32
+
33
+ <li>
34
+ <a href="chapter-1.html">
35
+ Introduction
36
+ </a>
37
+
38
+ <ol type="1">
39
+
40
+ <li><a href="chapter-1.html#s1">What is Net::SSH?</a></li>
41
+
42
+ <li><a href="chapter-1.html#s2">What isn&#8217;t Net::SSH?</a></li>
43
+
44
+ <li><a href="chapter-1.html#s3">Getting Net::SSH</a></li>
45
+
46
+ <li><a href="chapter-1.html#s4">License Information</a></li>
47
+
48
+ <li><a href="chapter-1.html#s5">Support</a></li>
49
+
50
+ <li><a href="chapter-1.html#s6">About the Author</a></li>
51
+
52
+ </ol>
53
+ </li>
54
+
55
+ <li>
56
+ <a href="chapter-2.html">
57
+ Starting a Session
58
+ </a>
59
+
60
+ <ol type="1">
61
+
62
+ <li><a href="chapter-2.html#s1">Using Net::SSH.start</a></li>
63
+
64
+ <li><a href="chapter-2.html#s2">Using a Public/Private Key</a></li>
65
+
66
+ <li><a href="chapter-2.html#s3">Options</a></li>
67
+
68
+ <li><a href="chapter-2.html#s4">Using Net::SSH::Session</a></li>
69
+
70
+ </ol>
71
+ </li>
72
+
73
+ <li>
74
+ <a href="chapter-3.html">
75
+ Channels
76
+ </a>
77
+
78
+ <ol type="1">
79
+
80
+ <li><a href="chapter-3.html#s1">What are Channels?</a></li>
81
+
82
+ <li><a href="chapter-3.html#s2">Session.loop</a></li>
83
+
84
+ <li><a href="chapter-3.html#s3">Channel Types</a></li>
85
+
86
+ <li><a href="chapter-3.html#s4">Opening a Channel</a></li>
87
+
88
+ <li><a href="chapter-3.html#s5">Callbacks</a></li>
89
+
90
+ <li><a href="chapter-3.html#s6">Channel Operations</a></li>
91
+
92
+ </ol>
93
+ </li>
94
+
95
+ <li><strong>
96
+ <a href="chapter-4.html">
97
+ Executing Commands
98
+ </a>
99
+ </strong> <big>&larr;</big>
100
+ <ol type="1">
101
+
102
+ <li><a href="chapter-4.html#s1">Using Channels</a></li>
103
+
104
+ <li><a href="chapter-4.html#s2">Using #process.open</a></li>
105
+
106
+ <li><a href="chapter-4.html#s3">Using #process.popen3</a></li>
107
+
108
+ </ol>
109
+ </li>
110
+
111
+ <li>
112
+ <a href="chapter-5.html">
113
+ Port Forwarding
114
+ </a>
115
+
116
+ <ol type="1">
117
+
118
+ <li><a href="chapter-5.html#s1">Introduction</a></li>
119
+
120
+ <li><a href="chapter-5.html#s2">Local-to-Remote</a></li>
121
+
122
+ <li><a href="chapter-5.html#s3">Remote-to-Local</a></li>
123
+
124
+ <li><a href="chapter-5.html#s4">Direct Channels</a></li>
125
+
126
+ <li><a href="chapter-5.html#s5">Remote-to-Local Handlers</a></li>
127
+
128
+ </ol>
129
+ </li>
130
+
131
+ <li>
132
+ <a href="chapter-6.html">
133
+ Using Proxies
134
+ </a>
135
+
136
+ <ol type="1">
137
+
138
+ <li><a href="chapter-6.html#s1">Introduction</a></li>
139
+
140
+ <li><a href="chapter-6.html#s2"><span class="caps">HTTP</span></a></li>
141
+
142
+ <li><a href="chapter-6.html#s3"><span class="caps">SOCKS</span></a></li>
143
+
144
+ </ol>
145
+ </li>
146
+
147
+ </ol>
148
+
149
+ <h2>Other Documentation</h2>
150
+
151
+ <ul>
152
+ <li><a href="http://net-ssh.rubyforge.org/api/index.html">Net::SSH API</a></li>
153
+ <li><a href="http://rubyforge.org/tracker/?atid=1842&group_id=274&func=browse">Net::SSH FAQ</a></li>
154
+ </ul>
155
+
156
+ <h2>Tutorials</h2>
157
+ <ol>
158
+
159
+ </ol>
160
+
161
+ <p align="center"><strong>More To Come...</strong></p>
162
+
163
+ <div class="license">
164
+ <a href="http://creativecommons.org/licenses/by-sa/2.0/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights" /></a><br />
165
+ This manual is licensed under a <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons License</a>.
166
+ </div>
167
+ </div>
168
+
169
+ </td><td valign='top' width="100%">
170
+
171
+ <div id="content">
172
+
173
+ <h1>4. Executing Commands</h1>
174
+
175
+
176
+
177
+ <h2>
178
+ <a name="s1"></a>
179
+ 4.1. Using Channels
180
+ </h2>
181
+
182
+
183
+
184
+ <div class="section">
185
+ <p>To run multiple processes in parallel, you can access the channel <span class="caps">API</span> directly, setting up multiple channels and callbacks in order to process the output from the channel.</p>
186
+
187
+ <p>Suppose, for example, that you wanted to run multiple &#8220;tail&#8221; commands on various logs on the remote machine, combining them all into the output on the client. Something like the following would suffice:</p>
188
+
189
+
190
+ <pre>
191
+ def do_tail( session, file )
192
+ session.open_channel do |channel|
193
+ channel.on_data do |ch, data|
194
+ puts "[#{file}] -&gt; #{data}"
195
+ end
196
+ channel.exec "tail -f #{file}"
197
+ end
198
+ end
199
+
200
+ Net::SSH.start( 'host' ) do |session|
201
+ do_tail session, "/var/log/messages"
202
+ do_tail session, "/var/log/XFree86.0.log"
203
+ do_tail session, "/var/log/tomcat/catalina.log"
204
+ do_tail session, "/var/log/mysql/mysql.err"
205
+ session.loop
206
+ end
207
+ </pre>
208
+ <p>As you can see, four different logs are tailed on four separate channels. Each channel registers an <code>on_data</code> callback (which simply displays the data it recieves, together with the name of the log file it came from). The <code>exec</code> method of the channel is then invoked, which simply sends the request to execute the process to the server, and then returns.</p>
209
+
210
+ <p>The <code>loop</code> method then blocks while packets and processed and callbacks are invoked, completing the program.</p>
211
+
212
+ <p>This approach works fine for processing data coming from the server, and with a little work and coordination can work well for sending data <em>to</em> the server as well, by calling the <code>send_data</code> method of the channel at the appropriate times. However, it requires a bit of forethought, since you have to come up with a simple state machine to manage most interactive sessions, and many times that&#8217;s more effort than it is worth.</p>
213
+ </div>
214
+
215
+
216
+
217
+ <h2>
218
+ <a name="s2"></a>
219
+ 4.2. Using #process.open
220
+ </h2>
221
+
222
+
223
+
224
+ <div class="section">
225
+ <p>The <code>#process.open</code> interface provides a simpler way to manage interactive sessions. It still works via callbacks, and it still requires a kind of state machine in order to process input, but it does simplify things a little bit.</p>
226
+
227
+ <p>Just open an <span class="caps">SSH</span> session. The <code>#process</code> service of the session manages access to convenience methods for handling and communicating with remote processes. In particular the <code>#open</code> method of the <code>#process</code> service allows you to constuct callbacks for dealing with remote processes, more conveniently than using channels directly.</p>
228
+
229
+ <p>Consider the &#8220;bc&#8221; command. It is a command-line calculator that accepts expressions on <code>stdin</code> and writes the results to <code>stdout</code>. When it encounters the word <code>quit</code> on the input, it exits. Sounds like a great way to demonstrate the <code>process</code> service&#8230;</p>
230
+
231
+
232
+ <pre>
233
+ Net::SSH.start( 'host' ) do |session|
234
+
235
+ session.process.open( "bc" ) do |bc|
236
+ dialog = [ "5+5", "7*12", "sqrt(2.000000)", "quit" ]
237
+
238
+ bc.on_success do |p|
239
+ puts "requesting result of #{dialog.first}"
240
+ p.puts dialog.shift
241
+ end
242
+
243
+ bc.on_stdout do |p,data|
244
+ puts "--&gt; #{data}"
245
+ unless dialog.empty?
246
+ puts "requesting result of #{dialog.first}"
247
+ p.puts dialog.shift
248
+ end
249
+ end
250
+
251
+ bc.on_exit do |p, status|
252
+ puts "process finished with exit status: #{status}"
253
+ end
254
+ end
255
+
256
+ end
257
+ </pre>
258
+ <p>Notice the progression. First, the session itself is started. Then, while the session is active, the process is invoked (via <code>#process.open</code>). After we have a handle to the process (which is yielded to the block, in this case), we set up the callbacks on the process. These are reminiscent of, but different from, the callbacks that we set up on the channel itself in the previous section.</p>
259
+
260
+ <p>The following callbacks are defined for a process handle:</p>
261
+
262
+ <table class="list">
263
+ <tr>
264
+ <th>Name </th>
265
+ <th>Description </th>
266
+ </tr>
267
+ <tr>
268
+ <td style="vertical-align:top;text-align:center;"><code>on_exit</code> </td>
269
+ <td> This callback is invoked when the process terminates normally. The block should accept two parameters: the process handle itself, and the exit status of the process.</td>
270
+ </tr>
271
+ <tr>
272
+ <td style="vertical-align:top;text-align:center;"><code>on_failure</code> </td>
273
+ <td> This callback is invoked when the process could not be invoked. It should take two parameters: the process handle itself, and a status string (which currently always <code>nil</code>).</td>
274
+ </tr>
275
+ <tr>
276
+ <td style="vertical-align:top;text-align:center;"><code>on_success</code> </td>
277
+ <td> This callback is invoked after the process has been successfully started. The callback should take a single parameter: the process handle itself.</td>
278
+ </tr>
279
+ <tr>
280
+ <td style="vertical-align:top;text-align:center;"><code>on_stderr</code> </td>
281
+ <td> This callback is invoked when data is received from the process&#8217;s <code>stderr</code> stream. The callback should have two parameters: the process handle, and the data.</td>
282
+ </tr>
283
+ <tr>
284
+ <td style="vertical-align:top;text-align:center;"><code>on_stdout</code> </td>
285
+ <td> This callback is invoked when data is received from the process&#8217;s <code>stdout</code> stream. The callback should have two parameters: the process handle, and the data.</td>
286
+ </tr>
287
+ </table>
288
+
289
+
290
+
291
+ <p>Sending data to the process is as simple as calling <code>puts</code> on the process handle. If you don&#8217;t want a newline appended to your data, use <code>write</code> instead.</p>
292
+
293
+ <p>Notice that, when sending a block to <code>#process.open</code>, you do not have to explicitly invoke <code>session.loop</code>. It is implicitly called at the end of the block. If you ever want to set up multiple processes to run in parallel, simply use <code>#process.open</code> without a block. The process handle will be returned, and you will be required to execute <code>session.loop</code> manually.</p>
294
+
295
+ <p>For more information on the <code>#process.open</code> service, see the <span class="caps">API</span> documentation for <code>Net::SSH::Service::Process::Open</code>.</p>
296
+ </div>
297
+
298
+
299
+
300
+ <h2>
301
+ <a name="s3"></a>
302
+ 4.3. Using #process.popen3
303
+ </h2>
304
+
305
+
306
+
307
+ <div class="section">
308
+ <p>The last approach to interacting with remote processes is the <code>#popen3</code> method of <code>#process</code> service. It is a <em>synchronous</em> approach, meaning that each method call <em>may</em> (potentially) block until data is received; you can&#8217;t be using other features of the Net::SSH package while using it, but you don&#8217;t have to mess with callbacks.</p>
309
+
310
+ <p>If you are familiar with the &#8220;popen3&#8221; Ruby module, this will seem familiar. It&#8217;s not a perfect clone of the &#8220;popen3&#8221; module&#8217;s functionality, but it&#8217;s close. What you do is you specify the process to invoke, and then you get three pseudo-IO objects back: the process&#8217;s input stream, it&#8217;s output stream, and it&#8217;s error stream. You can write to the input stream to send data to the process, or read from the output and error streams. Reading from the output or error streams will block until data is available, which makes it very convenient for interacting with a single remote process.</p>
311
+
312
+ <p>Here&#8217;s the previous &#8220;bc&#8221; example, rewritten to use <code>#popen3</code>:</p>
313
+
314
+
315
+ <pre>
316
+ Net::SSH.start( 'host' ) do |session|
317
+
318
+ input, output, error = session.process.popen3( "bc" )
319
+
320
+ [ "5+5", "7*12", "sqrt(2.000000)" ].each do |formula|
321
+ input.puts formula
322
+ puts "#{formula}=#{output.read}"
323
+ end
324
+
325
+ input.puts "quit"
326
+
327
+ end
328
+ </pre>
329
+ <p>Much more concise, isn&#8217;t it? One caveat, though: there is no way to kill the process (unless the process can terminate itself, such as through the use of issuing bc&#8217;s &#8220;quit&#8221; command as used above) without closing the session. To remedy this, there is also a block version of popen3 that provides an explicit scope for the three data streams:</p>
330
+
331
+
332
+ <pre>
333
+ Net::SSH.start( 'host' ) do |session|
334
+ session.process.popen3( "bc" ) do |input, output, error|
335
+ [ "5+5", "7*12", "sqrt(2.000000)" ].each do |formula|
336
+ input.puts formula
337
+ puts "#{formula}=#{output.read}"
338
+ end
339
+ end
340
+ end
341
+ </pre>
342
+ <p>The three streams will be closed and process explicitly terminated when the block ends.</p>
343
+ </div>
344
+
345
+
346
+
347
+
348
+ </div>
349
+
350
+ </td></tr>
351
+ </table>
352
+ </body>
353
+ </html>