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,393 @@
1
+ <html>
2
+ <head>
3
+ <title>Net::SSH Manual :: Chapter 5: Port Forwarding</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>
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><strong>
112
+ <a href="chapter-5.html">
113
+ Port Forwarding
114
+ </a>
115
+ </strong> <big>&larr;</big>
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>5. Port Forwarding</h1>
174
+
175
+
176
+
177
+ <h2>
178
+ <a name="s1"></a>
179
+ 5.1. Introduction
180
+ </h2>
181
+
182
+
183
+
184
+ <div class="section">
185
+ <p>Port forwarding is a feature of the <span class="caps">SSH</span> protocol that allows you to specify a port on one of the hosts, and have network connections on that port forwarded to a port on a different host, using the <span class="caps">SSH</span> connection as a proxy. There are basically two ways to use this forwarding:</p>
186
+ <ol>
187
+ <li>A port on the local host is forwarded via the remote host to another machine. Any connection to the specified port will cause all subsequent data to be sent over the connection to the remote host, where it will then be forwarded to the requested destination host.</li>
188
+ <li>A port on the remote host is forwarded over the connection to the local host, and from there to (potentially) some other remote destination. Any connection to the specified port on the remote host is forwarded over the connection to the local host, which then makes a connection to the specified remote destination and sends the data there.</li>
189
+ </ol>
190
+
191
+ <p>All port forwarding in the Net::SSH library is managed by the <code>#forward</code> service. Just invoke methods on that service to set up any of various port forwarding configurations.</p>
192
+
193
+
194
+ <pre>
195
+ Net::SSH.start( 'host' ) do |session|
196
+ forward = session.forward
197
+ ...
198
+ session.loop
199
+ end
200
+ </pre>
201
+ <p>You can define any number of forwards before invoking the main loop, in which case all of those forwards will be handled transparently (and silently) in parallel, over the same connection. (Isn&#8217;t <span class="caps">SSH</span> lovely?)</p>
202
+
203
+ <p>Naturally, you can also have remote processes, <span class="caps">SFTP</span> sessions, and more all working at the same time on the connection.</p>
204
+ </div>
205
+
206
+
207
+
208
+ <h2>
209
+ <a name="s2"></a>
210
+ 5.2. Local-to-Remote
211
+ </h2>
212
+
213
+
214
+
215
+ <div class="section">
216
+ <p>Forwarding a local connection to a remote destination is simply a matter of invoking the <code>#local</code> method of the <code>#forward</code> service. The simplest version of the method just takes three parameters: the local port to listen on, and the remote host and port to forward the connection to:</p>
217
+
218
+
219
+ <pre>
220
+ Net::SSH.start( 'host' ) do |session|
221
+ session.forward.local( 1234, 'www.google.com', 80 )
222
+ session.loop
223
+ end
224
+ </pre>
225
+ <p>In the above example, then, any connection received on port 1234 will be forwarded to port 80 on &#8220;www.google.com&#8221;. This means that if you were to point a browser at &#8220;http://localhost:1234&#8221;, it would pull up <a href="http://www.google.com">Google</a>.</p>
226
+
227
+ <p>By default, only connections <em>from the local host</em> are accepted. This is because the default bind address is 127.0.0.1. You can specify any bind address you want (including 0.0.0.0 to allow connections from anywhere) by specifying that address as the first parameter to <code>#local</code>, with the local port number immediately following.</p>
228
+
229
+
230
+ <pre>
231
+ session.forward.local( '0.0.0.0', 1234, 'www.google.com', 80 )
232
+ </pre>
233
+ <p>In this configuration, anyone from anywhere can connect to your machine on port 1234 and be forwarded to Google.</p>
234
+ </div>
235
+
236
+
237
+
238
+ <h2>
239
+ <a name="s3"></a>
240
+ 5.3. Remote-to-Local
241
+ </h2>
242
+
243
+
244
+
245
+ <div class="section">
246
+ <p>Forwarding remote connections to the local host is also straightforward; simply call the <code>#remote_to</code> method of the <code>#forward</code> service. This takes three (or four) parameters: the local port and host to be forwarded to (in that order), and the remote port to listen on. The fourth parameter is optional, and is the bind address on the remote machine; this defaults to &#8220;127.0.0.1&#8221;.</p>
247
+
248
+
249
+ <pre>
250
+ Net::SSH.start( 'host' ) do |session|
251
+ session.forward.remote_to( 80, 'www.google.com', 1234 )
252
+ session.loop
253
+ end
254
+ </pre>
255
+ <p>The above example causes any connection on port 1234 of the remote machine (<em>from</em> the remote machine) to be forwarded via the local host to port 80 at www.google.com. To make things a bit more open, you could specify a bind address of 0.0.0.0:</p>
256
+
257
+
258
+ <pre>
259
+ session.forward.remote_to( 80, 'www.google.com', 1234, '0.0.0.0' )
260
+ </pre>
261
+ </div>
262
+
263
+
264
+
265
+ <h2>
266
+ <a name="s4"></a>
267
+ 5.4. Direct Channels
268
+ </h2>
269
+
270
+
271
+
272
+ <div class="section">
273
+ <p>Sometimes it might be nice to programmatically simulate a network connection on a local port and have it forwarded to the remote host. You can do this by means of the <code>#direct_channel</code> method.</p>
274
+
275
+ <p>The <code>#direct_channel</code> method looks similar to <code>#local</code>: the first three parameters are the local port to simulate the connection from, and the remote host and port that the connection should be forwarded to. The fourth parameter, however, is a <em>handler</em>, an object that is used as a callback for a variety of different events.</p>
276
+
277
+ <p>The handler for the <code>#direct_channel</code> method may implement any of the following callbacks (all are optional, though you probably want to implement at least one or two of them):</p>
278
+
279
+ <table class="list">
280
+ <tr>
281
+ <th>Callback </th>
282
+ <th>Description </th>
283
+ </tr>
284
+ <tr>
285
+ <td style="vertical-align:top;text-align:center;"><code>confirm</code> </td>
286
+ <td> This is invoked when the channel has been opened and the remote host has confirmed it. This accepts four parameters: the channel itself, the local port, remote host, and remote port. (In this way, the same handler may be used for multiple forward requests.)</td>
287
+ </tr>
288
+ <tr>
289
+ <td style="vertical-align:top;text-align:center;"><code>process</code> </td>
290
+ <td> After the channel has been confirmed, this is invoked, to process the channel. This callback will be invoked in a new Thread, so that if your handler needs to listen to a socket and then send data received from it over the channel, it can do so without blocking the main loop. The callback accepts a single parameter, the channel handle itself.</td>
291
+ </tr>
292
+ <tr>
293
+ <td style="vertical-align:top;text-align:center;"><code>on_close</code> </td>
294
+ <td> This is called when the channel over which this forwarded connection is being processed has been closed. The callback accepts a single parameter, the channel itself.</td>
295
+ </tr>
296
+ <tr>
297
+ <td style="vertical-align:top;text-align:center;"><code>on_eof</code> </td>
298
+ <td> When the remote machine indicates it will send no more data, this callback will be invoked. It accepts a single parameter, the channel itself.</td>
299
+ </tr>
300
+ <tr>
301
+ <td style="vertical-align:top;text-align:center;"><code>on_receive</code> </td>
302
+ <td> This is invoked when data is received from the remote machine. It accepts two parameters: the channel handle, and the data that was received.</td>
303
+ </tr>
304
+ </table>
305
+
306
+
307
+
308
+ <p>For example, the following example pretends to be a client that has connected to the local host on a forwarded port:</p>
309
+
310
+
311
+ <pre>
312
+ class Handler
313
+ def on_receive( channel, data )
314
+ puts "got data: #{data.inspect}"
315
+ channel.send_data "subsequent request"
316
+ end
317
+
318
+ def process( channel )
319
+ channel.send_data "initial request"
320
+ end
321
+ end
322
+
323
+ Net::SSH.start( 'host' ) do |session|
324
+ session.forward.direct_channel( 1234, 'somewhere.else.net',
325
+ 4321, Handler.new )
326
+
327
+ session.loop
328
+ end
329
+ </pre>
330
+ <p>The local port number for <code>#direct_channel</code> has no real purpose, other than to report to the <span class="caps">SSH</span> server that the &#8220;virtual&#8221; connection occurred on that port.</p>
331
+ </div>
332
+
333
+
334
+
335
+ <h2>
336
+ <a name="s5"></a>
337
+ 5.5. Remote-to-Local Handlers
338
+ </h2>
339
+
340
+
341
+
342
+ <div class="section">
343
+ <p>You can use handlers going in the other direction, too. If you want to programmatically process forwarded data from a remote host, you can use the <code>#remote</code> method. This takes two parameters, with an optional third parameter. The two required parameters are the handler to use, and the remote port that should be listened to. The optional parameter is the remote bind address, which defaults to &#8216;127.0.0.1&#8217;.</p>
344
+
345
+ <p>(Incidentally, if the port is 0, a new port will be allocated for you automatically by the server.)</p>
346
+
347
+ <p>Whenever connections are received on the remote port, they will be forwarded to the handler, which may implement the following callbacks:</p>
348
+
349
+ <table class="list">
350
+ <tr>
351
+ <th>Callback </th>
352
+ <th>Description </th>
353
+ </tr>
354
+ <tr>
355
+ <td style="vertical-align:top;text-align:center;"><code>error</code> </td>
356
+ <td> This is invoked if the forward could not be initiated. It accepts a single parameter, which is the error message. </td>
357
+ </tr>
358
+ <tr>
359
+ <td style="vertical-align:top;text-align:center;"><code>on_close</code> </td>
360
+ <td> This is invoked when the channel that was assigned to process this forwarded connection has been closed. The callback takes one parameter: the channel itself. </td>
361
+ </tr>
362
+ <tr>
363
+ <td style="vertical-align:top;text-align:center;"><code>on_eof</code> </td>
364
+ <td> This is invoked when the remote end of the connection has promised not to send any more data. The local end of the channel may continue to send data, however. This callback takes on parameter: the channel itself.</td>
365
+ </tr>
366
+ <tr>
367
+ <td style="vertical-align:top;text-align:center;"><code>on_open</code> </td>
368
+ <td> This is invoked when a new connection is received over the forwarded channel. It accepts five parameters: the channel object, the connected address, the connected port, the originator address, and the originator port. </td>
369
+ </tr>
370
+ <tr>
371
+ <td style="vertical-align:top;text-align:center;"><code>on_receive</code> </td>
372
+ <td> This is invoked when data is received over the channel from the remote connection. It accepts two parameters: the channel object, and the data that was received. </td>
373
+ </tr>
374
+ <tr>
375
+ <td style="vertical-align:top;text-align:center;"><code>setup</code> </td>
376
+ <td> This is invoked immediately after the forward request has been acknowledged as successful. It accepts a single parameter, which is the port that was assigned to this forward. If the port parameter to <code>#remote</code> was not 0, then that same value will be passed to the callback. Otherwise, the newly allocated port number will be passed to the callback. </td>
377
+ </tr>
378
+ </table>
379
+
380
+
381
+
382
+ <p>Note that the <code>on_receive</code> handler is required&#8212;all other callbacks may remain unimplemented by the handler.</p>
383
+ </div>
384
+
385
+
386
+
387
+
388
+ </div>
389
+
390
+ </td></tr>
391
+ </table>
392
+ </body>
393
+ </html>
@@ -0,0 +1,296 @@
1
+ <html>
2
+ <head>
3
+ <title>Net::SSH Manual :: Chapter 6: Using Proxies</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>
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><strong>
132
+ <a href="chapter-6.html">
133
+ Using Proxies
134
+ </a>
135
+ </strong> <big>&larr;</big>
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>6. Using Proxies</h1>
174
+
175
+
176
+
177
+ <h2>
178
+ <a name="s1"></a>
179
+ 6.1. Introduction
180
+ </h2>
181
+
182
+
183
+
184
+ <div class="section">
185
+ <p>Many times, you may find yourself behind a firewall, unable to <span class="caps">SSH</span> out. In such instances, you can often take advantage of your existing proxies to tunnel an <span class="caps">SSH</span> connection through your firewall.</p>
186
+ </div>
187
+
188
+
189
+
190
+ <h2>
191
+ <a name="s2"></a>
192
+ 6.2. <span class="caps">HTTP</span>
193
+ </h2>
194
+
195
+
196
+
197
+ <div class="section">
198
+ <p>If you have an <span class="caps">HTTP</span> proxy running, you may be able to use it to your advantage. The following snippet demonstrates how to tunnel an <span class="caps">SSH</span> connection through an <span class="caps">HTTP</span> proxy:</p>
199
+
200
+
201
+ <pre>
202
+ require 'net/ssh'
203
+ require 'net/ssh/proxy/http'
204
+
205
+ proxy_host = 'my.proxy.com'
206
+ proxy_port = 8080
207
+ proxy = Net::SSH::Proxy::HTTP.new( proxy_host, proxy_port )
208
+
209
+ Net::SSH.start( 'host', :proxy =&gt; proxy ) do |session|
210
+ ...
211
+ end
212
+ </pre>
213
+ <p>As you can see, you first create an instance of the proxy you want to use. (This flexibility allows for other proxy types to be supported, although at present only <span class="caps">HTTP</span> and <span class="caps">SOCKS</span> are available.)</p>
214
+
215
+ <p>Once you&#8217;ve created your proxy, you just start your <span class="caps">SSH</span> session, as usual, except you also pass a <code>:proxy</code> option. The proxy will then be used to obtain a connection to the remote host.</p>
216
+
217
+ <p>Note: If your proxy does not allow connects to be made to other hosts on port 22, then you&#8217;ll have to do some magic to allow <span class="caps">SSH</span> connections on your remote host on ports other than 22. Port forwarding on that remote host (from itself, to itself), can help you there.</p>
218
+
219
+ <p>For instance, if your proxy disallows connections to any port except (say) 443, you could run the following command on the remote host:</p>
220
+
221
+
222
+ <pre>
223
+ ssh -gL 443:localhost:22 localhost
224
+ </pre>
225
+ <p>Then, as long as that command is running, port 443 will always be forwarded to port 22. Naturally, this means that you must run this command while you have access to the box; if you can&#8217;t access that machine in the first place (ie, because you&#8217;re behind a firewall), then it does you no good.</p>
226
+
227
+ <h3>Proxy Authentication</h3>
228
+
229
+ <p>Some proxies require authentication. Net::SSH supports these proxies as well. If you specify the user name either as a <code>:user</code> option to the <span class="caps">HTTP</span> proxy constructor, or in the <code>HTTP_PROXY_USER</code> or <code>CONNECT_USER</code> environment variables, that name will be used to authenticate with the proxy. Likewise, the password may be given either via the <code>:password</code> constructor option, or via the <code>HTTP_PROXY_PASSWORD</code> or <code>CONNECT_PASSWORD</code> environment variables.</p>
230
+
231
+
232
+ <pre>
233
+ require 'net/ssh'
234
+ require 'net/ssh/proxy/http'
235
+
236
+ proxy_host = 'my.proxy.com'
237
+ proxy_port = 8080
238
+ proxy_user = 'my-name'
239
+ proxy_password = 'my-password'
240
+
241
+ proxy = Net::SSH::Proxy::HTTP.new( proxy_host, proxy_port,
242
+ :user =&gt; proxy_user,
243
+ :password =&gt; proxy_password )
244
+
245
+ Net::SSH.start( 'host', :proxy =&gt; proxy ) do |session|
246
+ ...
247
+ end
248
+ </pre>
249
+ <p>Note that currently, only basic authentication is supported; in the future, digest authentication may be added for proxies that support it.</p>
250
+ </div>
251
+
252
+
253
+
254
+ <h2>
255
+ <a name="s3"></a>
256
+ 6.3. <span class="caps">SOCKS</span>
257
+ </h2>
258
+
259
+
260
+
261
+ <div class="section">
262
+ <p>In addition to the <span class="caps">HTTP</span> proxy, Net::SSH also supports <span class="caps">SOCKS</span> proxies (both versions 4 and 5). Their usage is almost identical to the <span class="caps">HTTP</span> version (except <span class="caps">SOCKS4</span> does not use passwords, just user names):</p>
263
+
264
+
265
+ <pre>
266
+ require 'net/ssh'
267
+ require 'net/ssh/proxy/socks4'
268
+ require 'net/ssh/proxy/socks5'
269
+
270
+ proxy_host = 'my.proxy.com'
271
+ proxy_port = 1080
272
+ proxy_user = 'my-name'
273
+ proxy_password = 'my-password'
274
+
275
+ socks4 = Net::SSH::Proxy::SOCKS4.new( proxy_host, proxy_port,
276
+ :user =&gt; proxy_user )
277
+
278
+ socks5 = Net::SSH::Proxy::SOCKS5.new( proxy_host, proxy_port,
279
+ :user =&gt; proxy_user,
280
+ :password =&gt; proxy_password)
281
+
282
+ Net::SSH.start( 'host', :proxy =&gt; socks4 ) do |session|
283
+ ...
284
+ end
285
+ </pre>
286
+ </div>
287
+
288
+
289
+
290
+
291
+ </div>
292
+
293
+ </td></tr>
294
+ </table>
295
+ </body>
296
+ </html>