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,18 @@
1
+ Forwarding a local connection to a remote destination is simply a matter of invoking the @#local@ method of the @#forward@ 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:
2
+
3
+ <pre>
4
+ Net::SSH.start( 'host' ) do |session|
5
+ session.forward.local( 1234, 'www.google.com', 80 )
6
+ session.loop
7
+ end
8
+ </pre>
9
+
10
+ In the above example, then, any connection received on port 1234 will be forwarded to port 80 on "www.google.com". This means that if you were to point a browser at "http://localhost:1234", it would pull up "Google":http://www.google.com.
11
+
12
+ By default, only connections _from the local host_ 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 @#local@, with the local port number immediately following.
13
+
14
+ <pre>
15
+ session.forward.local( '0.0.0.0', 1234, 'www.google.com', 80 )
16
+ </pre>
17
+
18
+ In this configuration, anyone from anywhere can connect to your machine on port 1234 and be forwarded to Google.
@@ -0,0 +1,14 @@
1
+ Forwarding remote connections to the local host is also straightforward; simply call the @#remote_to@ method of the @#forward@ 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 "127.0.0.1".
2
+
3
+ <pre>
4
+ Net::SSH.start( 'host' ) do |session|
5
+ session.forward.remote_to( 80, 'www.google.com', 1234 )
6
+ session.loop
7
+ end
8
+ </pre>
9
+
10
+ The above example causes any connection on port 1234 of the remote machine (_from_ 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:
11
+
12
+ <pre>
13
+ session.forward.remote_to( 80, 'www.google.com', 1234, '0.0.0.0' )
14
+ </pre>
@@ -0,0 +1 @@
1
+ Net::SSH was written by "Jamis Buck":mailto:jgb3@email.byu.edu. Feel free to send him compliments, candy, money, praise, or new feature patches--he likes all those things. You can send him questions and suggestions, too, if you really want to. However, for bug reports and general feature requests, please use the trackers on the "Net::SSH project page":http://rubyforge.org/projects/net-ssh.
@@ -0,0 +1,39 @@
1
+ h3. Prerequisites:
2
+
3
+ In order to use Net::SSH, you must be using a supported version of Ruby's OpenSSL module. The version distributed with Ruby 1.8.1 and earlier is not sufficient, and is lacking several features that Net::SSH relies on. If you are using Ruby 1.8.1 (or earlier), you should either upgrade to 1.8.2, or download and install an updated version of the OpenSSL module. You can download a usable snapshot from the "Net::SSH downloads page":http://rubyforge.org/projects/net-ssh.
4
+
5
+ Furthermore, you must make sure that Ruby's OpenSSL module has been compiled against _at least_ version 0.9.7 of the OpenSSL library. Prior versions lacked functionality that Net::SSH depends on (notably, the 'padding' property of ciphers).
6
+
7
+ Optionally, you can install the "ruby-termios":http://raa.ruby-lang.org/project/ruby-termios and "ruby-password":http://raa.ruby-lang.org/project/ruby-password modules. If you do, then the ruby-password module will be used when prompting you for the passphrases needed to load your keys (if your keys have passphrases).
8
+
9
+ h3. Using "RubyGems":http://rubygems.rubyforge.org
10
+
11
+ If you have "RubyGems":http://rubygems.rubyforge.org installed, installing Net::SSH is simple:
12
+
13
+ <pre>
14
+ gem install net-ssh
15
+ </pre>
16
+
17
+ You still need to make sure you have a working version of Ruby's OpenSSL module, but other than that, you should be good to go!
18
+
19
+ h3. Using "rpa-base":http://rpa-base.rubyforge.org
20
+
21
+ If you have "rpa-base":http://rpa-base.rubyforge.org installed:
22
+
23
+ <pre>
24
+ rpa install net-ssh
25
+ </pre>
26
+
27
+ As with the gem install, you still need to make sure you have a working version of Ruby's OpenSSL module, but other than that, you should be good to go!
28
+
29
+ h3. Doing it the hard way
30
+
31
+ If you don't have "RubyGems":http://rubygems.rubyforge.org or "rpa-base":http://rpa-base.rubyforge.org, or if you just prefer to install things by hand, you can always go to the "Net::SSH downloads page":http://rubyforge.org/projects/net-ssh and grab the package of your choice: @tar.gz@, @tar.bz2@, or @zip@.
32
+
33
+ Then, unpack the archive and run the @setup.rb@ script:
34
+
35
+ <pre>
36
+ ruby setup.rb config
37
+ ruby setup.rb setup
38
+ ruby setup.rb install
39
+ </pre>
@@ -0,0 +1,6 @@
1
+ Net::SSH is made available under either the BSD license, or the same license Ruby (which, by extension, also allows the GPL as a permissable license as well). You can view the full text of any of these licenses in the @doc@ subdirectory of the Net::SSH distrubtion. The texts of the BSD and GPL licenses are also available online: "BSD":http://www.opensource.org/licenses/bsd-license.php and "GPL":http://www.opensource.org/licenses/gpl-license.php.
2
+
3
+ This manual (in any form, be it source or otherwise) and the scripts and templates used to generate it, are all distributed under the "Creative Commons":http://creativecommons.org "Attribution-ShareAlike":http://creativecommons.org/licenses/by-sa/2.0 license.
4
+
5
+ If you desire permission to use either Net::SSH or the manual in a manner incompatible with these licenses, please contact the copyright holder ("Jamis Buck":mailto:jgb3@email.byu.edu) in order to negotiate a more compatible license.
6
+
@@ -0,0 +1,7 @@
1
+ Mailing lists, bug trackers, feature requests, and public forums are all available (courtesty of "RubyForge":http://rubyforge.org) at the "Net::SSH project page":http://rubyforge.org/projects/net-ssh.
2
+
3
+ h3. Mailing Lists
4
+
5
+ |_. *List Name* |_. -- |_. *Description* |
6
+ |^=. "net-ssh-users":http://rubyforge.org/pipermail/net-ssh-users|^=. "subscribe / unsubscribe":http://rubyforge.org/mailman/listinfo/net-ssh-users| The Net::SSH users list is devoted to the discussion of and questions about the usage of the Net::SSH module. If you can't quite figure out how to get a feature of Net::SSH to work, this is the list you would go to in order to ask your questions.|
7
+ |^=. "net-ssh-devel":http://rubyforge.org/pipermail/net-ssh-devel|^=. "subscribe / unsubscribe":http://rubyforge.org/mailman/listinfo/net-ssh-devel| The Net::SSH developers list is devoted to the discussion of Net::SSH's implementation. If you have created a patch that you would like to discuss, or if you would like to discuss a new feature, this is the list for you.|
@@ -0,0 +1,7 @@
1
+ Net::SSH is a pure-Ruby implementation of the SSH2 client protocol. It supports the following features:
2
+
3
+ * User authentication via explicit username/password, or using a public-key/private-key pair.
4
+
5
+ * Port forwarding, both from the local host to a remote computer via the remote host, and from the remote host to the local host.
6
+
7
+ * Execute processes on the remote machine, both interactively and non-interactively ("batch").
@@ -0,0 +1,3 @@
1
+ Net::SSH is only a _client_ implementation, not a server. Given sufficient motivation and encouragement from the community, perhaps it will someday include an SSH server, but as of right now, it does not.
2
+
3
+ Furthermore, it is only an _SSH2_ client. This means that it cannot connect to SSH servers that only understand the older SSH1 protocol.
@@ -0,0 +1,52 @@
1
+ If you have an HTTP proxy running, you may be able to use it to your advantage. The following snippet demonstrates how to tunnel an SSH connection through an HTTP proxy:
2
+
3
+ <pre>
4
+ require 'net/ssh'
5
+ require 'net/ssh/proxy/http'
6
+
7
+ proxy_host = 'my.proxy.com'
8
+ proxy_port = 8080
9
+ proxy = Net::SSH::Proxy::HTTP.new( proxy_host, proxy_port )
10
+
11
+ Net::SSH.start( 'host', :proxy => proxy ) do |session|
12
+ ...
13
+ end
14
+ </pre>
15
+
16
+ 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 HTTP and SOCKS are available.)
17
+
18
+ Once you've created your proxy, you just start your SSH session, as usual, except you also pass a @:proxy@ option. The proxy will then be used to obtain a connection to the remote host.
19
+
20
+ Note: If your proxy does not allow connects to be made to other hosts on port 22, then you'll have to do some magic to allow SSH connections on your remote host on ports other than 22. Port forwarding on that remote host (from itself, to itself), can help you there.
21
+
22
+ For instance, if your proxy disallows connections to any port except (say) 443, you could run the following command on the remote host:
23
+
24
+ <pre>
25
+ ssh -gL 443:localhost:22 localhost
26
+ </pre>
27
+
28
+ 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't access that machine in the first place (ie, because you're behind a firewall), then it does you no good.
29
+
30
+ h3. Proxy Authentication
31
+
32
+ Some proxies require authentication. Net::SSH supports these proxies as well. If you specify the user name either as a @:user@ option to the HTTP proxy constructor, or in the @HTTP_PROXY_USER@ or @CONNECT_USER@ environment variables, that name will be used to authenticate with the proxy. Likewise, the password may be given either via the @:password@ constructor option, or via the @HTTP_PROXY_PASSWORD@ or @CONNECT_PASSWORD@ environment variables.
33
+
34
+ <pre>
35
+ require 'net/ssh'
36
+ require 'net/ssh/proxy/http'
37
+
38
+ proxy_host = 'my.proxy.com'
39
+ proxy_port = 8080
40
+ proxy_user = 'my-name'
41
+ proxy_password = 'my-password'
42
+
43
+ proxy = Net::SSH::Proxy::HTTP.new( proxy_host, proxy_port,
44
+ :user => proxy_user,
45
+ :password => proxy_password )
46
+
47
+ Net::SSH.start( 'host', :proxy => proxy ) do |session|
48
+ ...
49
+ end
50
+ </pre>
51
+
52
+ Note that currently, only basic authentication is supported; in the future, digest authentication may be added for proxies that support it.
@@ -0,0 +1 @@
1
+ Many times, you may find yourself behind a firewall, unable to SSH out. In such instances, you can often take advantage of your existing proxies to tunnel an SSH connection through your firewall.
@@ -0,0 +1,23 @@
1
+ In addition to the HTTP proxy, Net::SSH also supports SOCKS proxies (both versions 4 and 5). Their usage is almost identical to the HTTP version (except SOCKS4 does not use passwords, just user names):
2
+
3
+ <pre>
4
+ require 'net/ssh'
5
+ require 'net/ssh/proxy/socks4'
6
+ require 'net/ssh/proxy/socks5'
7
+
8
+ proxy_host = 'my.proxy.com'
9
+ proxy_port = 1080
10
+ proxy_user = 'my-name'
11
+ proxy_password = 'my-password'
12
+
13
+ socks4 = Net::SSH::Proxy::SOCKS4.new( proxy_host, proxy_port,
14
+ :user => proxy_user )
15
+
16
+ socks5 = Net::SSH::Proxy::SOCKS5.new( proxy_host, proxy_port,
17
+ :user => proxy_user,
18
+ :password => proxy_password)
19
+
20
+ Net::SSH.start( 'host', :proxy => socks4 ) do |session|
21
+ ...
22
+ end
23
+ </pre>
@@ -0,0 +1,66 @@
1
+ Just as with the "OpenSSH":http://www.openssh.org version of the @ssh@ utilities, Net::SSH supports authentication using public/private keys.
2
+
3
+ h3. I don't know what public/private keys are... Explain, please?
4
+
5
+ Public key/private key encryption is just one way of hiding information from prying eyes. The idea is that you have two tokens: a _public key_, and a _private key_. The private key is yours alone--you never let _anyone_ else see it. The _public key_, on the other hand, is distributable. You give it to anyone that you want to be able to communicate with you securely.
6
+
7
+ The remote party uses your public key to encrypt information. Anything encrypted with your public key may only be decrypted with the corresponding private key, and since you have the only copy of that, you can rest easily knowing that no one can easily intercept your communications!
8
+
9
+ Net::SSH allows you to define a private key, which it will then attempt to use during authentication with the remote server. If the remote server has a copy of the corresponding public key, you will be able to log into that remote server without having to specify a password. Not only is this convenient, but for Ruby scripts, it is much more secure, since you don't have to hard-code your password in your script.
10
+
11
+ h3. Setting up public/private keys
12
+
13
+ Net::SSH, by default, will use the private keys that you have set up for use with ssh. These keys are called "id_dsa" and "id_rsa", and are located under your home directory, either in a ".ssh" subdirectory, or a ".ssh2" subdirectory.
14
+
15
+ The "id_dsa" key is the preferred key (since it uses the stronger DSA encryption), but both DSA and RSA are supported.
16
+
17
+ To create these keys, you can use the "ssh-keygen" utility from "OpenSSH":http://www.openssh.org. Alternatively, if you have the Net::SSH::Utilities package installed, you can use the "rb-keygen" utility (which is a pure-Ruby implementation of most of the functionality of ssh-keygen).
18
+
19
+ <pre>
20
+ ssh-keygen -t dsa
21
+ </pre>
22
+
23
+
24
+ (If you would rather use an RSA key, replace "dsa" with "rsa" in the command given above.)
25
+
26
+ Accept all the defaults when prompted. You will also be asked for a passphrase. This passphrase is an additional level of protection, which prevents anyone from being able to use your private key without knowing the passphrase. Unfortunately, it also means that you have to enter the passphrase every time you use your key. It is up to you what price you want to pay for security, but if you _can_ leave the passphrase blank. In this case, anyone that has a copy of your private key can use it, but it's a little more convenient to deal with.
27
+
28
+ Once you create your keys, you then need to set up your account on each remote server so that it knows about your public key. To do this, log into the remote server and edit (or create) the file (in your home directory) ".ssh/authorized_keys". Just copy the contents of your public key (in your local machine's home directory, called ".ssh/id_dsa.pub" or ".ssh/id_rsa.pub") into the "authorized_keys" file on a line of its own. Then save the file and logout. Everything _should_ now be set up.
29
+
30
+ (Note: if you have an SSH client installed, it will typically have its own key generation utility. You can use that instead, if you prefer.)
31
+
32
+ h3. Connecting using public/private keys
33
+
34
+ Public/private keys are always tried before the explicit password authentication, even if you provide a password. Thus, if you _only_ want to use public/private key authentication, simply remove the password from the argument list. If you can successfully obtain a session handle, then your keys are set up correctly!
35
+
36
+ <pre>
37
+ Net::SSH.start( 'host', 'user' ) do |session|
38
+ ...
39
+ end
40
+ </pre>
41
+
42
+ Furthermore, if your @USER@ environment variable is set to the username that you want to log into the remote machine as, you can even leave the @username@ parameter off:
43
+
44
+ <pre>
45
+ Net::SSH.start( 'host' ) do |session|
46
+ ...
47
+ end
48
+ </pre>
49
+
50
+ h3. Using keys with passphrases
51
+
52
+ When you use a private key that was created with a passphrase, you will be prompted to enter the passphrase when the key is loaded. This may make such a key inappropriate for use in automated environments, but it is certainly more secure than the use of unprotected private keys.
53
+
54
+ If you have the "ruby-termios":http://raa.ruby-lang.org/project/ruby-termios and "ruby-password":http://raa.ruby-lang.org/project/ruby-password modules installed, the ruby-password module will be used when prompting for passphrases. Otherwise, a generic message (courtesy of the OpenSSL library) will be presented, which will not be very informative.
55
+
56
+ h3. Using an SSH agent
57
+
58
+ Most SSH clients come with what is called an _agent_. This is a program that is continually running, and which keeps track of all of a user's keys. When an SSH client needs to perform an operation using one of the user's keys, it requests the operation via the agent, rather than performing the operation itself directly with a key.
59
+
60
+ The benefit of this is what is known as _single sign-on_. If any of your keys have a passphrase, this allows you to enter the passphrase _once_ (when the key is loaded by the agent), and then any SSH program you use will never prompt you for that passphrase again.
61
+
62
+ Net::SSH includes support for interfacing with an SSH agent. Currently, only Unix-ish systems are supported (due to issues with interprocess communication on Windows). Eventually, perhaps an interface will be created to the "PuTTY agent (pageant)":http://www.chiark.greenend.org.uk/~sgtatham/putty/, but don't hold your breath. (It's not very easy to interface with.)
63
+
64
+ To allow your Net::SSH programs to interface with a running agent, simply make sure that the @SSH_AGENT_SOCK@ environment variable is set to the location of the Unix domain socket that the agent is listening to. Also, make sure you have added all of your keys to the agent (typically by running the @ssh-add@ utility.
65
+
66
+ A future version of Net::SSH may include it's own agent implementation as well, to make using an agent on a variety of platforms simpler.
@@ -0,0 +1,42 @@
1
+ There are various additional options that you can specify when connecting. These options allow you to specify such things as the cipher algorithm to use, whether or not the data stream will be compressed, or explicit paths to the private keys to use.
2
+
3
+
4
+ Options are specified as a hash in the last parameter to the @start@ method. If using the keyword parameters version of the @start@ method, the options hash is whatever is left after processing the @:username@, @:password@, and @:port@ options.
5
+
6
+ The complete list of available options, and their valid values, is given in the following table.
7
+
8
+ table(list).
9
+ |_. Option |_. Description |
10
+ |^=. @:auth_methods@ | This is the list of authorization methods to try. It defaults to "publickey", "keyboard-interactive", "password", and "hostbased". (These are also the only authorization methods that are supported.) If you want them to be tried in a different order, or if you don't want certain methods to be used, you can specify your own list via this option.|
11
+ |^=. @:compression@ | The compression algorithm to use when compressing the data stream. Valid values are @none@ and @zlib@. The default is @none@.|
12
+ |^=. @:compression_level@ | This is only used when the compression algorithm is @zlib@. It is an integer value from 0 to 9, representing the quality of the compression. A 0 is no compression, and a 9 is most compression. The default is 6.|
13
+ |^=. @:container@ | This is the dependency injection container to use when registering all of the services that Net::SSH uses internally. If unspecified (the default) a new container will be created. This option allows you to reuse a single container for multiple application components.|
14
+ |^=. @:crypto_backend@ | This is the cryptography backend to use. It defaults to @:ossl@, which specifies the OpenSSL cryptography engine. Currently, this is the only supported backend, but in the future others may be provided, and this is how they would be selected.|
15
+ |^=. @:encryption@ | This is the cipher algorithm to use when sending/receiving data to/from the remote server. It defaults to @3des-cbc@. Other valid algorithms supported by Net::SSH are @aes128-cbc@, @blowfish-cbc@, @aes256-cbc@, @aes192-cbc@, @idea-cbc@, and @none@. Note that the values you specify here are only _suggestions_, and if the server you are contacting cannot use your recommended algorithm, a fallback algorithm will be used (typically chosen in the order the algorithms were listed, above). This option may take an array, if you want to specify the order of the fallback algorithms to try, as well. |
16
+ |^=. @:hmac@ | This specifies the "message authentication code" (MAC) algorithm to use to ensure that each packet transmitted and recieved is authentic. This defaults to @hmac-md5@. Other valid algorithms supported by Net::SSH are @hmac-sha1@, @hmac-md5-96@, @hmac-md5-sha1@, and @none@. Note that the values you specify here are only _suggestions_, and if the server you are contacting cannot use your recommended algorithm, a fallback algorithm will be used (typically chosen in the order the algorithms were listed, above). This option may take an array, if you want to specify the order of the fallback algorithms to try, as well. |
17
+ |^=. @:host_key@ | This specifies the host key type that should be used when negotiating keys with the server. This defaults to @ssh-dss@, but may also be @ssh-rsa@. As with some other option types, the value you specify is only a recommendation, not a commandment, and if the server cannot honor the key type you specified, a fallback will be chosen from among the other supported types. If you wish to specify the fallback algorithms to try, you may pass an array as the value of this option, which contains (in order) the key types to try. |
18
+ |^=. @:host_keys@ | This is an array of file names that contain the private keys which identify the host your script is running on. These default to @/etc/ssh/ssh_host_dsa_key@ and @/etc/ssh/ssh_host_rsa_key@ (which are both typically only readable by root). These keys are only used in hostbased authentication.|
19
+ |^=. @:kex@ | This specifies the "key-exchange" (KEX) algorithm to use when exchanging keys. Two algorithms are currently supported: @diffie-hellman-group-exchange-sha1@, and @diffie-hellman-group1-sha1@. The default is @diffie-hellman-group-exchange-sha1@.|
20
+ |^=. @:keys@ | This specifies the list of private key files to use _instead_ of the defaults (@$HOME/.ssh/id_dsa@, @$HOME/.ssh2/id_dsa@, @$HOME/.ssh/id_rsa@, and @$HOME/.ssh2/id_rsa@). The value of this option should be an array of strings.|
21
+ |^=. @:languages@ | This option specifies the preferred language (or languages) that should be used when communicating error messages. It has no effect on Net::SSH, but may cause the server (if it supports your suggested language) to send errors in the language you request. The default is empty.|
22
+ |^=. @:log@ | Specifies either a string or an IO object. If it is a string, it names the file that all log messages should be written to. Otherwise, the messages will be written to the IO object directly. Defaults to STDERR.|
23
+ |^=. @:port@ | This is the port number that should be used to connect to the remote machine. If you wish to specify the port, you are generally better off specifying it as the second parameter to @start@, rather than as an option, but you _can_ specify it this way, if you prefer.|
24
+ |^=. @:registry_options@ | If the @:container@ option is not specified, a new container will be created. This option specifies a hash of additional options that may be used to configure the new container (registry). By default, it is empty.|
25
+ |^=. @:verbose@ | Specifies how verbose the logging should be. Valid values are @:fatal@, @:error@, @:warn@, @:info@, and @:debug@. Defaults to @:warn@. WARNING: selecting @:debug@ will result in LOTS of output! (Further customization of verbosity can be accomplished by specifying which Net::SSH components should have which logging levels, via the @:registry_options@ option.)|
26
+
27
+ For example, the following code snippet will connect to the given remote host, and requests that the @ssh-rsa@ host key type be used, with the @blowfish-cbc@ cipher algorithm, and requests that the given private key file be used. Also, the data stream will be compressed.
28
+
29
+ <pre>
30
+ require 'net/ssh'
31
+ require 'logger'
32
+
33
+ Net::SSH.start(
34
+ 'host', 'user',
35
+ :host_key => "ssh-rsa",
36
+ :encryption => "blowfish-cbc",
37
+ :keys => [ "/tmp/temporary-key" ],
38
+ :compression => "zlib"
39
+ ) do |session|
40
+ ...
41
+ end
42
+ </pre>
@@ -0,0 +1,14 @@
1
+ Alternatively, you can use Net::SSH::Session to start your SSH sessions. The @Net::SSH.start@ interface described above is simply a convenience for creating a new Session object explicitly.
2
+
3
+ <pre>
4
+ require 'net/ssh'
5
+
6
+ Net::SSH::Session.new(
7
+ 'host', 'username', 'password',
8
+ :compression => "zlib"
9
+ ) do |session|
10
+ ...
11
+ end
12
+ </pre>
13
+
14
+ Note that Net::SSH::Session#new accepts the same parameters as Net::SSH.start, and may also be called without a block.
@@ -0,0 +1,49 @@
1
+ Before you can do anything with Net::SSH, you need to require the @net/ssh@ module:
2
+
3
+ <pre>
4
+ require 'net/ssh'
5
+ </pre>
6
+
7
+ Once you have required the @net/ssh@ module, you can begin an SSH session by calling @Net::SSH.start@. This may be used in one of two ways. If called without a block, it will return a reference to the new session as an instance of a @Net::SSH::Session@. Used this way, you must explicitly close the session when you are finished with it.
8
+
9
+ <pre>
10
+ session = Net::SSH.start( 'host', 'user', 'passwd' )
11
+ ...
12
+ session.close
13
+ </pre>
14
+
15
+ The other approach involves attaching a block to the start method. When used this way, the new session is passed to the block, and the session is automatically closed when the block exits.
16
+
17
+ <pre>
18
+ Net::SSH.start( 'host', 'user', 'passwd' ) do |session|
19
+ ...
20
+ end
21
+ </pre>
22
+
23
+ If you need to specify a different port on the host to connect to (the default is 22), you can specify it immediately after the @host@ parameter, like so:
24
+
25
+ <pre>
26
+ Net::SSH.start( 'host', 1234, 'user', 'passwd' ) do |session|
27
+ ...
28
+ end
29
+ </pre>
30
+
31
+ h3. Using Keyword Arguments
32
+
33
+ Some people prefer using keyword arguments for functions with more than a couple of parameters. The @start@ method supports this approach as well, although the @host@ parameter is always positional and always comes first.
34
+
35
+ <pre>
36
+ Net::SSH.start( 'host',
37
+ :password=>'passwd',
38
+ :port=>1234,
39
+ :username=>'user',
40
+ ... ) do |session|
41
+ ...
42
+ end
43
+ </pre>
44
+
45
+ (More about the "@...@" stuff, later.)
46
+
47
+ h3. Failed Authentication
48
+
49
+ If the username and/or password given to @start@ are incorrect, authentication will fail. If authentication fails, a @Net::SSH::AuthenticationFailed@ exception will be raised.
@@ -0,0 +1,30 @@
1
+ <h1>Tutorial #<%= object.index %>. <%= object.title %></h1>
2
+
3
+ <p>The sources for this tutorial may be found in the <tt>tutorial/<%= "%02d" % object.index %></tt>
4
+ directory of the Copland distribution.</p>
5
+
6
+ <% if object.intro %>
7
+
8
+ <h2>Introduction</h2>
9
+
10
+ <%= object.intro.to_html %>
11
+
12
+ <% end %>
13
+
14
+ <h2>Steps</h2>
15
+
16
+ <ol>
17
+ <% object.steps.each do |step| %>
18
+
19
+ <li><%= step.to_html %></li>
20
+
21
+ <% end %>
22
+ </ol>
23
+
24
+ <% if object.summary %>
25
+
26
+ <h2>Summary</h2>
27
+
28
+ <%= object.summary.to_html %>
29
+
30
+ <% end %>
@@ -0,0 +1,81 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SSH Secure Shell Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-ssh website : http://net-ssh.rubyforge.org
13
+ # project website: http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../lib"
18
+ require 'net/ssh'
19
+
20
+ # This assumes three things:
21
+ #
22
+ # 1) That you have an SSH server running on your local machine,
23
+ # 2) That the USER environment variable is set to your user name, and
24
+ # 3) That you have public and private keys conigured so that you can log into
25
+ # your machine via SSH without being prompted for a password.
26
+ #
27
+ # If #2 or #3 are not true, you can add your user-name and password as the
28
+ # second and third parameters (respectively) to Net::SSH.start.
29
+
30
+ Net::SSH.start( 'localhost' ) do |session|
31
+
32
+ # Note: two things here,
33
+ #
34
+ # 1) open_channel does not immediately invoke the associated block. It only
35
+ # calls the block after the server has confirmed that the channel is valid.
36
+ # 2) channel.exec does not block--it just sends the request to the server and
37
+ # returns.
38
+ #
39
+ # For these two reasons, you MUST call session.loop, so that packets get
40
+ # processed and dispatched to the appropriate channel for handling.
41
+
42
+ def exec( command )
43
+ lambda do |channel|
44
+ channel.exec command
45
+ channel.on_data do |ch,data|
46
+ ch[:data] ||= ""
47
+ ch[:data] << data
48
+ end
49
+ channel.on_extended_data do |ch,type,data|
50
+ ch[:extended_data] ||= []
51
+ ch[:extended_data][type] ||= ""
52
+ ch[:extended_data][type] << data
53
+ end
54
+ end
55
+ end
56
+
57
+ channels = []
58
+ channels.push session.open_channel( &exec( "echo $HOME" ) )
59
+ channels.push session.open_channel( &exec( "ls -la /" ) )
60
+ channels.push session.open_channel( &exec( "bogus-command" ) )
61
+
62
+ # Process packets from the server and route them to the appropriate channel
63
+ # for handling.
64
+
65
+ session.loop
66
+
67
+ # Display the results.
68
+
69
+ channels.each do |c|
70
+ puts "----------------------------------"
71
+ if c.valid?
72
+ puts c[:data]
73
+ if c[:extended_data] && c[:extended_data][1]
74
+ puts "-- stderr: --"
75
+ puts c[:extended_data][1]
76
+ end
77
+ else
78
+ puts "channel was not opened: #{c.reason} (#{c.reason_code})"
79
+ end
80
+ end
81
+ end