ddollar-net-ssh 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. data/CHANGELOG.rdoc +42 -0
  2. data/Manifest +101 -0
  3. data/README.rdoc +110 -0
  4. data/Rakefile +26 -0
  5. data/THANKS.rdoc +16 -0
  6. data/lib/net/ssh.rb +199 -0
  7. data/lib/net/ssh/authentication/agent.rb +175 -0
  8. data/lib/net/ssh/authentication/constants.rb +18 -0
  9. data/lib/net/ssh/authentication/key_manager.rb +169 -0
  10. data/lib/net/ssh/authentication/methods/abstract.rb +60 -0
  11. data/lib/net/ssh/authentication/methods/hostbased.rb +71 -0
  12. data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +66 -0
  13. data/lib/net/ssh/authentication/methods/password.rb +39 -0
  14. data/lib/net/ssh/authentication/methods/publickey.rb +92 -0
  15. data/lib/net/ssh/authentication/pageant.rb +176 -0
  16. data/lib/net/ssh/authentication/session.rb +127 -0
  17. data/lib/net/ssh/buffer.rb +339 -0
  18. data/lib/net/ssh/buffered_io.rb +149 -0
  19. data/lib/net/ssh/config.rb +173 -0
  20. data/lib/net/ssh/connection/channel.rb +625 -0
  21. data/lib/net/ssh/connection/constants.rb +33 -0
  22. data/lib/net/ssh/connection/session.rb +569 -0
  23. data/lib/net/ssh/connection/term.rb +178 -0
  24. data/lib/net/ssh/errors.rb +85 -0
  25. data/lib/net/ssh/key_factory.rb +85 -0
  26. data/lib/net/ssh/known_hosts.rb +129 -0
  27. data/lib/net/ssh/loggable.rb +61 -0
  28. data/lib/net/ssh/packet.rb +102 -0
  29. data/lib/net/ssh/prompt.rb +93 -0
  30. data/lib/net/ssh/proxy/errors.rb +14 -0
  31. data/lib/net/ssh/proxy/http.rb +94 -0
  32. data/lib/net/ssh/proxy/socks4.rb +70 -0
  33. data/lib/net/ssh/proxy/socks5.rb +128 -0
  34. data/lib/net/ssh/service/forward.rb +267 -0
  35. data/lib/net/ssh/test.rb +89 -0
  36. data/lib/net/ssh/test/channel.rb +129 -0
  37. data/lib/net/ssh/test/extensions.rb +152 -0
  38. data/lib/net/ssh/test/kex.rb +44 -0
  39. data/lib/net/ssh/test/local_packet.rb +51 -0
  40. data/lib/net/ssh/test/packet.rb +81 -0
  41. data/lib/net/ssh/test/remote_packet.rb +38 -0
  42. data/lib/net/ssh/test/script.rb +157 -0
  43. data/lib/net/ssh/test/socket.rb +59 -0
  44. data/lib/net/ssh/transport/algorithms.rb +384 -0
  45. data/lib/net/ssh/transport/cipher_factory.rb +72 -0
  46. data/lib/net/ssh/transport/constants.rb +30 -0
  47. data/lib/net/ssh/transport/hmac.rb +31 -0
  48. data/lib/net/ssh/transport/hmac/abstract.rb +48 -0
  49. data/lib/net/ssh/transport/hmac/md5.rb +12 -0
  50. data/lib/net/ssh/transport/hmac/md5_96.rb +11 -0
  51. data/lib/net/ssh/transport/hmac/none.rb +15 -0
  52. data/lib/net/ssh/transport/hmac/sha1.rb +13 -0
  53. data/lib/net/ssh/transport/hmac/sha1_96.rb +11 -0
  54. data/lib/net/ssh/transport/identity_cipher.rb +40 -0
  55. data/lib/net/ssh/transport/kex.rb +13 -0
  56. data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +208 -0
  57. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +77 -0
  58. data/lib/net/ssh/transport/openssl.rb +128 -0
  59. data/lib/net/ssh/transport/packet_stream.rb +230 -0
  60. data/lib/net/ssh/transport/server_version.rb +61 -0
  61. data/lib/net/ssh/transport/session.rb +262 -0
  62. data/lib/net/ssh/transport/state.rb +170 -0
  63. data/lib/net/ssh/verifiers/lenient.rb +30 -0
  64. data/lib/net/ssh/verifiers/null.rb +12 -0
  65. data/lib/net/ssh/verifiers/strict.rb +53 -0
  66. data/lib/net/ssh/version.rb +60 -0
  67. data/net-ssh.gemspec +56 -0
  68. data/setup.rb +1585 -0
  69. data/test/authentication/methods/common.rb +28 -0
  70. data/test/authentication/methods/test_abstract.rb +51 -0
  71. data/test/authentication/methods/test_hostbased.rb +108 -0
  72. data/test/authentication/methods/test_keyboard_interactive.rb +98 -0
  73. data/test/authentication/methods/test_password.rb +50 -0
  74. data/test/authentication/methods/test_publickey.rb +123 -0
  75. data/test/authentication/test_agent.rb +205 -0
  76. data/test/authentication/test_key_manager.rb +100 -0
  77. data/test/authentication/test_session.rb +93 -0
  78. data/test/common.rb +106 -0
  79. data/test/configs/exact_match +8 -0
  80. data/test/configs/wild_cards +14 -0
  81. data/test/connection/test_channel.rb +452 -0
  82. data/test/connection/test_session.rb +483 -0
  83. data/test/test_all.rb +6 -0
  84. data/test/test_buffer.rb +336 -0
  85. data/test/test_buffered_io.rb +63 -0
  86. data/test/test_config.rb +78 -0
  87. data/test/test_key_factory.rb +67 -0
  88. data/test/transport/hmac/test_md5.rb +34 -0
  89. data/test/transport/hmac/test_md5_96.rb +25 -0
  90. data/test/transport/hmac/test_none.rb +34 -0
  91. data/test/transport/hmac/test_sha1.rb +34 -0
  92. data/test/transport/hmac/test_sha1_96.rb +25 -0
  93. data/test/transport/kex/test_diffie_hellman_group1_sha1.rb +146 -0
  94. data/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +92 -0
  95. data/test/transport/test_algorithms.rb +302 -0
  96. data/test/transport/test_cipher_factory.rb +163 -0
  97. data/test/transport/test_hmac.rb +34 -0
  98. data/test/transport/test_identity_cipher.rb +40 -0
  99. data/test/transport/test_packet_stream.rb +433 -0
  100. data/test/transport/test_server_version.rb +55 -0
  101. data/test/transport/test_session.rb +312 -0
  102. data/test/transport/test_state.rb +173 -0
  103. metadata +222 -0
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ === 2.0.1 / 5 May 2008
2
+
3
+ * Teach Net::SSH about a handful of default key names [Jamis Buck]
4
+
5
+
6
+ === 2.0.0 / 1 May 2008
7
+
8
+ * Allow the :verbose argument to accept symbols (:debug, etc.) as well as Logger level constants (Logger::DEBUG, etc.) [Jamis Buck]
9
+
10
+
11
+ === 2.0 Preview Release 4 (1.99.3) / 19 Apr 2008
12
+
13
+ * Make sure HOME is set to something sane, even on OS's that don't set it by default [Jamis Buck]
14
+
15
+ * Add a :passphrase option to specify the passphrase to use with private keys [Francis Sullivan]
16
+
17
+ * Open a new auth agent connection for every auth-agent channel request [Jamis Buck]
18
+
19
+
20
+ === 2.0 Preview Release 3 (1.99.2) / 10 Apr 2008
21
+
22
+ * Session properties [Jamis Buck]
23
+
24
+ * Make channel open failure work with a callback so that failures can be handled similarly to successes [Jamis Buck]
25
+
26
+
27
+ === 2.0 Preview Release 2 (1.99.1) / 22 Mar 2008
28
+
29
+ * Partial support for ~/.ssh/config (and related) SSH configuration files [Daniel J. Berger, Jamis Buck]
30
+
31
+ * Added Net::SSH::Test to facilitate testing complex SSH state machines [Jamis Buck]
32
+
33
+ * Reworked Net::SSH::Prompt to use conditionally-selected modules [Jamis Buck, suggested by James Rosen]
34
+
35
+ * Added Channel#eof? and Channel#eof! [Jamis Buck]
36
+
37
+ * Fixed bug in strict host key verifier on cache miss [Mike Timm]
38
+
39
+
40
+ === 2.0 Preview Release 1 (1.99.0) / 21 Aug 2007
41
+
42
+ * First preview release of Net::SSH v2
data/Manifest ADDED
@@ -0,0 +1,101 @@
1
+ CHANGELOG.rdoc
2
+ lib/net/ssh/authentication/agent.rb
3
+ lib/net/ssh/authentication/constants.rb
4
+ lib/net/ssh/authentication/key_manager.rb
5
+ lib/net/ssh/authentication/methods/abstract.rb
6
+ lib/net/ssh/authentication/methods/hostbased.rb
7
+ lib/net/ssh/authentication/methods/keyboard_interactive.rb
8
+ lib/net/ssh/authentication/methods/password.rb
9
+ lib/net/ssh/authentication/methods/publickey.rb
10
+ lib/net/ssh/authentication/pageant.rb
11
+ lib/net/ssh/authentication/session.rb
12
+ lib/net/ssh/buffer.rb
13
+ lib/net/ssh/buffered_io.rb
14
+ lib/net/ssh/config.rb
15
+ lib/net/ssh/connection/channel.rb
16
+ lib/net/ssh/connection/constants.rb
17
+ lib/net/ssh/connection/session.rb
18
+ lib/net/ssh/connection/term.rb
19
+ lib/net/ssh/errors.rb
20
+ lib/net/ssh/key_factory.rb
21
+ lib/net/ssh/known_hosts.rb
22
+ lib/net/ssh/loggable.rb
23
+ lib/net/ssh/packet.rb
24
+ lib/net/ssh/prompt.rb
25
+ lib/net/ssh/proxy/errors.rb
26
+ lib/net/ssh/proxy/http.rb
27
+ lib/net/ssh/proxy/socks4.rb
28
+ lib/net/ssh/proxy/socks5.rb
29
+ lib/net/ssh/service/forward.rb
30
+ lib/net/ssh/test/channel.rb
31
+ lib/net/ssh/test/extensions.rb
32
+ lib/net/ssh/test/kex.rb
33
+ lib/net/ssh/test/local_packet.rb
34
+ lib/net/ssh/test/packet.rb
35
+ lib/net/ssh/test/remote_packet.rb
36
+ lib/net/ssh/test/script.rb
37
+ lib/net/ssh/test/socket.rb
38
+ lib/net/ssh/test.rb
39
+ lib/net/ssh/transport/algorithms.rb
40
+ lib/net/ssh/transport/cipher_factory.rb
41
+ lib/net/ssh/transport/constants.rb
42
+ lib/net/ssh/transport/hmac/abstract.rb
43
+ lib/net/ssh/transport/hmac/md5.rb
44
+ lib/net/ssh/transport/hmac/md5_96.rb
45
+ lib/net/ssh/transport/hmac/none.rb
46
+ lib/net/ssh/transport/hmac/sha1.rb
47
+ lib/net/ssh/transport/hmac/sha1_96.rb
48
+ lib/net/ssh/transport/hmac.rb
49
+ lib/net/ssh/transport/identity_cipher.rb
50
+ lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb
51
+ lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb
52
+ lib/net/ssh/transport/kex.rb
53
+ lib/net/ssh/transport/openssl.rb
54
+ lib/net/ssh/transport/packet_stream.rb
55
+ lib/net/ssh/transport/server_version.rb
56
+ lib/net/ssh/transport/session.rb
57
+ lib/net/ssh/transport/state.rb
58
+ lib/net/ssh/verifiers/lenient.rb
59
+ lib/net/ssh/verifiers/null.rb
60
+ lib/net/ssh/verifiers/strict.rb
61
+ lib/net/ssh/version.rb
62
+ lib/net/ssh.rb
63
+ Rakefile
64
+ README.rdoc
65
+ setup.rb
66
+ test/authentication/methods/common.rb
67
+ test/authentication/methods/test_abstract.rb
68
+ test/authentication/methods/test_hostbased.rb
69
+ test/authentication/methods/test_keyboard_interactive.rb
70
+ test/authentication/methods/test_password.rb
71
+ test/authentication/methods/test_publickey.rb
72
+ test/authentication/test_agent.rb
73
+ test/authentication/test_key_manager.rb
74
+ test/authentication/test_session.rb
75
+ test/common.rb
76
+ test/configs/exact_match
77
+ test/configs/wild_cards
78
+ test/connection/test_channel.rb
79
+ test/connection/test_session.rb
80
+ test/test_all.rb
81
+ test/test_buffer.rb
82
+ test/test_buffered_io.rb
83
+ test/test_config.rb
84
+ test/test_key_factory.rb
85
+ test/transport/hmac/test_md5.rb
86
+ test/transport/hmac/test_md5_96.rb
87
+ test/transport/hmac/test_none.rb
88
+ test/transport/hmac/test_sha1.rb
89
+ test/transport/hmac/test_sha1_96.rb
90
+ test/transport/kex/test_diffie_hellman_group1_sha1.rb
91
+ test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb
92
+ test/transport/test_algorithms.rb
93
+ test/transport/test_cipher_factory.rb
94
+ test/transport/test_hmac.rb
95
+ test/transport/test_identity_cipher.rb
96
+ test/transport/test_packet_stream.rb
97
+ test/transport/test_server_version.rb
98
+ test/transport/test_session.rb
99
+ test/transport/test_state.rb
100
+ THANKS.rdoc
101
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,110 @@
1
+ = Net::SSH
2
+
3
+ * http://net-ssh.rubyforge.org/ssh
4
+
5
+ == DESCRIPTION:
6
+
7
+ Net::SSH is a pure-Ruby implementation of the SSH2 client protocol. It allows you to write programs that invoke and interact with processes on remote servers, via SSH2.
8
+
9
+ == FEATURES:
10
+
11
+ * Execute processes on remote servers and capture their output
12
+ * Run multiple processes in parallel over a single SSH connection
13
+ * Support for SSH subsystems
14
+ * Forward local and remote ports via an SSH connection
15
+
16
+ == SYNOPSIS:
17
+
18
+ In a nutshell:
19
+
20
+ require 'net/ssh'
21
+
22
+ Net::SSH.start('host', 'user', :password => "password") do |ssh|
23
+ # capture all stderr and stdout output from a remote process
24
+ output = ssh.exec!("hostname")
25
+
26
+ # capture only stdout matching a particular pattern
27
+ stdout = ""
28
+ ssh.exec!("ls -l /home/jamis") do |channel, stream, data|
29
+ stdout << data if stream == :stdout
30
+ end
31
+ puts stdout
32
+
33
+ # run multiple processes in parallel to completion
34
+ ssh.exec "sed ..."
35
+ ssh.exec "awk ..."
36
+ ssh.exec "rm -rf ..."
37
+ ssh.loop
38
+
39
+ # open a new channel and configure a minimal set of callbacks, then run
40
+ # the event loop until the channel finishes (closes)
41
+ channel = ssh.open_channel do |ch|
42
+ ch.exec "/usr/local/bin/ruby /path/to/file.rb" do |ch, success|
43
+ raise "could not execute command" unless success
44
+
45
+ # "on_data" is called when the process writes something to stdout
46
+ ch.on_data do |c, data|
47
+ $STDOUT.print data
48
+ end
49
+
50
+ # "on_extended_data" is called when the process writes something to stderr
51
+ ch.on_extended_data do |c, type, data|
52
+ $STDERR.print data
53
+ end
54
+
55
+ ch.on_close { puts "done!" }
56
+ end
57
+ end
58
+
59
+ channel.wait
60
+
61
+ # forward connections on local port 1234 to port 80 of www.capify.org
62
+ ssh.forward.local(1234, "www.capify.org", 80)
63
+ ssh.loop { true }
64
+ end
65
+
66
+ See Net::SSH for more documentation, and links to further information.
67
+
68
+ == REQUIREMENTS:
69
+
70
+ The only requirement you might be missing is the OpenSSL bindings for Ruby. These are built by default on most platforms, but you can verify that they're built and installed on your system by running the following command line:
71
+
72
+ ruby -ropenssl -e 'puts OpenSSL::OPENSSL_VERSION'
73
+
74
+ If that spits out something like "OpenSSL 0.9.8g 19 Oct 2007", then you're set. If you get an error, then you'll need to see about rebuilding ruby with OpenSSL support, or (if your platform supports it) installing the OpenSSL bindings separately.
75
+
76
+ Additionally: if you are going to be having Net::SSH prompt you for things like passwords or certificate passphrases, you'll want to have either the Highline (recommended) or Termios (unix systems only) gem installed, so that the passwords don't echo in clear text.
77
+
78
+ Lastly, if you want to run the tests or use any of the Rake tasks, you'll need:
79
+
80
+ * Echoe (for the Rakefile)
81
+ * Mocha (for the tests)
82
+
83
+ == INSTALL:
84
+
85
+ * gem install net-ssh (might need sudo privileges)
86
+
87
+ == LICENSE:
88
+
89
+ (The MIT License)
90
+
91
+ Copyright (c) 2008 Jamis Buck <jamis@37signals.com>
92
+
93
+ Permission is hereby granted, free of charge, to any person obtaining
94
+ a copy of this software and associated documentation files (the
95
+ 'Software'), to deal in the Software without restriction, including
96
+ without limitation the rights to use, copy, modify, merge, publish,
97
+ distribute, sublicense, and/or sell copies of the Software, and to
98
+ permit persons to whom the Software is furnished to do so, subject to
99
+ the following conditions:
100
+
101
+ The above copyright notice and this permission notice shall be
102
+ included in all copies or substantial portions of the Software.
103
+
104
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
105
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
106
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
107
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
108
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
109
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
110
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require './lib/net/ssh/version'
2
+
3
+ begin
4
+ require 'echoe'
5
+ rescue LoadError
6
+ abort "You'll need to have `echoe' installed to use Net::SSH's Rakefile"
7
+ end
8
+
9
+ version = Net::SSH::Version::STRING.dup
10
+ if ENV['SNAPSHOT'].to_i == 1
11
+ version << "." << Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ Echoe.new('net-ssh', version) do |p|
15
+ p.changelog = "CHANGELOG.rdoc"
16
+
17
+ p.author = "Jamis Buck"
18
+ p.email = "jamis@jamisbuck.org"
19
+ p.summary = "a pure-Ruby implementation of the SSH2 client protocol"
20
+ p.url = "http://net-ssh.rubyforge.org/ssh"
21
+
22
+ p.need_zip = true
23
+ p.include_rakefile = true
24
+
25
+ p.rdoc_pattern = /^(lib|README.rdoc|CHANGELOG.rdoc|THANKS.rdoc)/
26
+ end
data/THANKS.rdoc ADDED
@@ -0,0 +1,16 @@
1
+ Net::SSH was originally written by Jamis Buck <jamis@37signals.com>. In
2
+ addition, the following individuals are gratefully acknowledged for their
3
+ contributions:
4
+
5
+ GOTOU Yuuzou <gotoyuzo@notwork.org>
6
+ * help and code related to OpenSSL
7
+
8
+ Guillaume Mar�ais <guillaume.marcais@free.fr>
9
+ * support for communicating with the the PuTTY "pageant" process
10
+
11
+ Daniel Berger <djberg96@yahoo.com>
12
+ * help getting unit tests in earlier Net::SSH versions to pass in Windows
13
+ * initial version of Net::SSH::Config provided inspiration and encouragement
14
+
15
+ Chris Andrews <chris@nodnol.org> and Lee Jensen <lee@outerim.com>
16
+ * support for ssh agent forwarding
data/lib/net/ssh.rb ADDED
@@ -0,0 +1,199 @@
1
+ # Make sure HOME is set, regardless of OS, so that File.expand_path works
2
+ # as expected with tilde characters.
3
+ ENV['HOME'] ||= ENV['HOMEPATH'] ? "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" : "."
4
+
5
+ require 'logger'
6
+
7
+ require 'net/ssh/config'
8
+ require 'net/ssh/errors'
9
+ require 'net/ssh/loggable'
10
+ require 'net/ssh/transport/session'
11
+ require 'net/ssh/authentication/session'
12
+ require 'net/ssh/connection/session'
13
+
14
+ module Net
15
+
16
+ # Net::SSH is a library for interacting, programmatically, with remote
17
+ # processes via the SSH2 protocol. Sessions are always initiated via
18
+ # Net::SSH.start. From there, a program interacts with the new SSH session
19
+ # via the convenience methods on Net::SSH::Connection::Session, by opening
20
+ # and interacting with new channels (Net::SSH::Connection:Session#open_channel
21
+ # and Net::SSH::Connection::Channel), or by forwarding local and/or
22
+ # remote ports through the connection (Net::SSH::Service::Forward).
23
+ #
24
+ # The SSH protocol is very event-oriented. Requests are sent from the client
25
+ # to the server, and are answered asynchronously. This gives great flexibility
26
+ # (since clients can have multiple requests pending at a time), but it also
27
+ # adds complexity. Net::SSH tries to manage this complexity by providing
28
+ # some simpler methods of synchronous communication (see Net::SSH::Connection::Session#exec!).
29
+ #
30
+ # In general, though, and if you want to do anything more complicated than
31
+ # simply executing commands and capturing their output, you'll need to use
32
+ # channels (Net::SSH::Connection::Channel) to build state machines that are
33
+ # executed while the event loop runs (Net::SSH::Connection::Session#loop).
34
+ #
35
+ # Net::SSH::Connection::Session and Net::SSH::Connection::Channel have more
36
+ # information about this technique.
37
+ #
38
+ # = "Um, all I want to do is X, just show me how!"
39
+ #
40
+ # == X == "execute a command and capture the output"
41
+ #
42
+ # Net::SSH.start("host", "user", :password => "password") do |ssh|
43
+ # result = ssh.exec!("ls -l")
44
+ # puts result
45
+ # end
46
+ #
47
+ # == X == "forward connections on a local port to a remote host"
48
+ #
49
+ # Net::SSH.start("host", "user", :password => "password") do |ssh|
50
+ # ssh.forward.local(1234, "www.google.com", 80)
51
+ # ssh.loop { true }
52
+ # end
53
+ #
54
+ # == X == "forward connections on a remote port to the local host"
55
+ #
56
+ # Net::SSH.start("host", "user", :password => "password") do |ssh|
57
+ # ssh.forward.remote(80, "www.google.com", 1234)
58
+ # ssh.loop { true }
59
+ # end
60
+ module SSH
61
+ # This is the set of options that Net::SSH.start recognizes. See
62
+ # Net::SSH.start for a description of each option.
63
+ VALID_OPTIONS = [
64
+ :auth_methods, :compression, :compression_level, :config, :encryption,
65
+ :forward_agent, :hmac, :host_key, :kex, :keys, :languages,
66
+ :logger, :paranoid, :password, :port, :proxy, :rekey_blocks_limit,
67
+ :rekey_limit, :rekey_packet_limit, :timeout, :verbose,
68
+ :global_known_hosts_file, :user_known_hosts_file, :host_key_alias,
69
+ :host_name, :user, :properties, :passphrase
70
+ ]
71
+
72
+ # The standard means of starting a new SSH connection. When used with a
73
+ # block, the connection will be closed when the block terminates, otherwise
74
+ # the connection will just be returned. The yielded (or returned) value
75
+ # will be an instance of Net::SSH::Connection::Session (q.v.). (See also
76
+ # Net::SSH::Connection::Channel and Net::SSH::Service::Forward.)
77
+ #
78
+ # Net::SSH.start("host", "user") do |ssh|
79
+ # ssh.exec! "cp /some/file /another/location"
80
+ # hostname = ssh.exec!("hostname")
81
+ #
82
+ # ssh.open_channel do |ch|
83
+ # ch.exec "sudo -p 'sudo password: ' ls" do |ch, success|
84
+ # abort "could not execute sudo ls" unless success
85
+ #
86
+ # ch.on_data do |ch, data|
87
+ # print data
88
+ # if data =~ /sudo password: /
89
+ # ch.send_data("password\n")
90
+ # end
91
+ # end
92
+ # end
93
+ # end
94
+ #
95
+ # ssh.loop
96
+ # end
97
+ #
98
+ # This method accepts the following options (all are optional):
99
+ #
100
+ # * :auth_methods => an array of authentication methods to try
101
+ # * :compression => the compression algorithm to use, or +true+ to use
102
+ # whatever is supported.
103
+ # * :compression_level => the compression level to use when sending data
104
+ # * :config => set to +true+ to load the default OpenSSH config files
105
+ # (~/.ssh/config, /etc/ssh_config), or to +false+ to not load them, or to
106
+ # a file-name (or array of file-names) to load those specific configuration
107
+ # files. Defaults to +true+.
108
+ # * :encryption => the encryption cipher (or ciphers) to use
109
+ # * :forward_agent => set to true if you want the SSH agent connection to
110
+ # be forwarded
111
+ # * :global_known_hosts_file => the location of the global known hosts
112
+ # file. Set to an array if you want to specify multiple global known
113
+ # hosts files. Defaults to %w(/etc/ssh/known_hosts /etc/ssh/known_hosts2).
114
+ # * :hmac => the hmac algorithm (or algorithms) to use
115
+ # * :host_key => the host key algorithm (or algorithms) to use
116
+ # * :host_key_alias => the host name to use when looking up or adding a
117
+ # host to a known_hosts dictionary file
118
+ # * :host_name => the real host name or IP to log into. This is used
119
+ # instead of the +host+ parameter, and is primarily only useful when
120
+ # specified in an SSH configuration file. It lets you specify an
121
+ # "alias", similarly to adding an entry in /etc/hosts but without needing
122
+ # to modify /etc/hosts.
123
+ # * :kex => the key exchange algorithm (or algorithms) to use
124
+ # * :keys => an array of file names of private keys to use for publickey
125
+ # and hostbased authentication
126
+ # * :logger => the logger instance to use when logging
127
+ # * :paranoid => either true, false, or :very, specifying how strict
128
+ # host-key verification should be
129
+ # * :passphrase => the passphrase to use when loading a private key (default
130
+ # is +nil+, for no passphrase)
131
+ # * :password => the password to use to login
132
+ # * :port => the port to use when connecting to the remote host
133
+ # * :properties => a hash of key/value pairs to add to the new connection's
134
+ # properties (see Net::SSH::Connection::Session#properties)
135
+ # * :proxy => a proxy instance (see Proxy) to use when connecting
136
+ # * :rekey_blocks_limit => the max number of blocks to process before rekeying
137
+ # * :rekey_limit => the max number of bytes to process before rekeying
138
+ # * :rekey_packet_limit => the max number of packets to process before rekeying
139
+ # * :timeout => how long to wait for the initial connection to be made
140
+ # * :user => the user name to log in as; this overrides the +user+
141
+ # parameter, and is primarily only useful when provided via an SSH
142
+ # configuration file.
143
+ # * :user_known_hosts_file => the location of the user known hosts file.
144
+ # Set to an array to specify multiple user known hosts files.
145
+ # Defaults to %w(~/.ssh/known_hosts ~/.ssh/known_hosts2).
146
+ # * :verbose => how verbose to be (Logger verbosity constants, Logger::DEBUG
147
+ # is very verbose, Logger::FATAL is all but silent). Logger::FATAL is the
148
+ # default. The symbols :debug, :info, :warn, :error, and :fatal are also
149
+ # supported and are translated to the corresponding Logger constant.
150
+ def self.start(host, user, options={}, &block)
151
+ invalid_options = options.keys - VALID_OPTIONS
152
+ if invalid_options.any?
153
+ raise ArgumentError, "invalid option(s): #{invalid_options.join(', ')}"
154
+ end
155
+
156
+ files = case options.fetch(:config, true)
157
+ when true then Net::SSH::Config.default_files
158
+ when false, nil then []
159
+ else Array(options[:config])
160
+ end
161
+
162
+ options = Net::SSH::Config.for(host, files).merge(options)
163
+ host = options.fetch(:host_name, host)
164
+
165
+ if !options.key?(:logger)
166
+ options[:logger] = Logger.new(STDERR)
167
+ options[:logger].level = Logger::FATAL
168
+ end
169
+
170
+ if options[:verbose]
171
+ options[:logger].level = case options[:verbose]
172
+ when Fixnum then options[:verbose]
173
+ when :debug then Logger::DEBUG
174
+ when :info then Logger::INFO
175
+ when :warn then Logger::WARN
176
+ when :error then Logger::ERROR
177
+ when :fatal then Logger::FATAL
178
+ else raise ArgumentError, "can't convert #{options[:verbose].inspect} to any of the Logger level constants"
179
+ end
180
+ end
181
+
182
+ transport = Transport::Session.new(host, options)
183
+ auth = Authentication::Session.new(transport, options)
184
+
185
+ user = options.fetch(:user, user)
186
+ if auth.authenticate("ssh-connection", user, options[:password])
187
+ connection = Connection::Session.new(transport, options)
188
+ if block_given?
189
+ yield connection
190
+ connection.close
191
+ else
192
+ return connection
193
+ end
194
+ else
195
+ raise AuthenticationFailed, user
196
+ end
197
+ end
198
+ end
199
+ end