net-ssh 0.6.0 → 0.9.0

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 (166) hide show
  1. data/doc/manual-html/chapter-1.html +59 -24
  2. data/doc/manual-html/chapter-2.html +116 -77
  3. data/doc/manual-html/chapter-3.html +80 -41
  4. data/doc/manual-html/chapter-4.html +111 -71
  5. data/doc/manual-html/chapter-5.html +267 -155
  6. data/doc/manual-html/chapter-6.html +211 -75
  7. data/doc/manual-html/chapter-7.html +331 -0
  8. data/doc/manual-html/index.html +39 -13
  9. data/doc/manual-html/{manual.css → stylesheets/manual.css} +81 -8
  10. data/doc/manual-html/stylesheets/ruby.css +17 -0
  11. data/doc/manual/chapter.erb +20 -0
  12. data/doc/manual/manual.rb +80 -9
  13. data/doc/manual/manual.yml +12 -2
  14. data/doc/manual/page.erb +1 -1
  15. data/doc/manual/parts/channels_callbacks.txt +11 -11
  16. data/doc/manual/parts/channels_loop.txt +6 -6
  17. data/doc/manual/parts/channels_open.txt +9 -9
  18. data/doc/manual/parts/exec_channels.txt +15 -15
  19. data/doc/manual/parts/exec_open.txt +17 -18
  20. data/doc/manual/parts/exec_popen3.txt +18 -18
  21. data/doc/manual/parts/forward_direct.txt +16 -16
  22. data/doc/manual/parts/forward_intro.txt +7 -7
  23. data/doc/manual/parts/forward_local.txt +9 -9
  24. data/doc/manual/parts/forward_remote.txt +9 -9
  25. data/doc/manual/parts/intro_getting.txt +11 -11
  26. data/doc/manual/parts/proxy_http.txt +27 -27
  27. data/doc/manual/parts/proxy_socks.txt +17 -17
  28. data/doc/manual/parts/session_key.txt +13 -14
  29. data/doc/manual/parts/session_options.txt +14 -14
  30. data/doc/manual/parts/session_session.txt +9 -9
  31. data/doc/manual/parts/session_start.txt +27 -27
  32. data/doc/manual/parts/shells_channels.txt +72 -0
  33. data/doc/manual/parts/shells_clients.txt +51 -0
  34. data/doc/manual/parts/shells_intro.txt +7 -0
  35. data/doc/manual/parts/shells_shell.txt +50 -0
  36. data/doc/manual/parts/shells_sync.txt +42 -0
  37. data/doc/manual/{manual.css → stylesheets/manual.css} +81 -8
  38. data/doc/manual/stylesheets/ruby.css +17 -0
  39. data/examples/channel-demo.rb +1 -1
  40. data/examples/port-forward.rb +1 -1
  41. data/examples/process-demo.rb +1 -1
  42. data/examples/remote-net-port-forward.rb +1 -1
  43. data/examples/remote-port-forward.rb +1 -1
  44. data/examples/shell-demo.rb +46 -0
  45. data/examples/ssh-client.rb +67 -0
  46. data/examples/sync-shell-demo.rb +69 -0
  47. data/examples/tail-demo.rb +1 -1
  48. data/lib/net/ssh.rb +1 -1
  49. data/lib/net/ssh/connection/channel.rb +61 -7
  50. data/lib/net/ssh/connection/constants.rb +1 -1
  51. data/lib/net/ssh/connection/driver.rb +61 -8
  52. data/lib/net/ssh/connection/services.rb +1 -1
  53. data/lib/net/ssh/connection/term.rb +1 -1
  54. data/lib/net/ssh/errors.rb +1 -1
  55. data/lib/net/ssh/proxy/errors.rb +1 -1
  56. data/lib/net/ssh/proxy/http.rb +1 -1
  57. data/lib/net/ssh/proxy/socks4.rb +1 -1
  58. data/lib/net/ssh/proxy/socks5.rb +1 -1
  59. data/lib/net/ssh/service/forward/driver.rb +1 -1
  60. data/lib/net/ssh/service/forward/local-network-handler.rb +1 -1
  61. data/lib/net/ssh/service/forward/remote-network-handler.rb +1 -1
  62. data/lib/net/ssh/service/forward/services.rb +1 -1
  63. data/lib/net/ssh/service/process/driver.rb +1 -1
  64. data/lib/net/ssh/service/process/open.rb +1 -1
  65. data/lib/net/ssh/service/process/popen3.rb +20 -2
  66. data/lib/net/ssh/service/process/services.rb +1 -1
  67. data/lib/net/ssh/service/services.rb +3 -1
  68. data/lib/net/ssh/service/shell/driver.rb +86 -0
  69. data/lib/net/ssh/service/shell/services.rb +54 -0
  70. data/lib/net/ssh/service/shell/shell.rb +213 -0
  71. data/lib/net/ssh/service/shell/sync.rb +114 -0
  72. data/lib/net/ssh/session.rb +9 -1
  73. data/lib/net/ssh/transport/algorithm-negotiator.rb +1 -1
  74. data/lib/net/ssh/transport/compress/compressor.rb +1 -1
  75. data/lib/net/ssh/transport/compress/decompressor.rb +1 -1
  76. data/lib/net/ssh/transport/compress/none-compressor.rb +1 -1
  77. data/lib/net/ssh/transport/compress/none-decompressor.rb +1 -1
  78. data/lib/net/ssh/transport/compress/services.rb +1 -1
  79. data/lib/net/ssh/transport/compress/zlib-compressor.rb +1 -1
  80. data/lib/net/ssh/transport/compress/zlib-decompressor.rb +1 -1
  81. data/lib/net/ssh/transport/constants.rb +1 -1
  82. data/lib/net/ssh/transport/errors.rb +1 -1
  83. data/lib/net/ssh/transport/identity-cipher.rb +1 -1
  84. data/lib/net/ssh/transport/kex/dh-gex.rb +1 -1
  85. data/lib/net/ssh/transport/kex/dh.rb +1 -1
  86. data/lib/net/ssh/transport/kex/services.rb +1 -1
  87. data/lib/net/ssh/transport/ossl/buffer-factory.rb +1 -1
  88. data/lib/net/ssh/transport/ossl/buffer.rb +1 -1
  89. data/lib/net/ssh/transport/ossl/cipher-factory.rb +1 -1
  90. data/lib/net/ssh/transport/ossl/digest-factory.rb +1 -1
  91. data/lib/net/ssh/transport/ossl/hmac-factory.rb +1 -1
  92. data/lib/net/ssh/transport/ossl/hmac/hmac.rb +1 -1
  93. data/lib/net/ssh/transport/ossl/hmac/md5-96.rb +1 -1
  94. data/lib/net/ssh/transport/ossl/hmac/md5.rb +1 -1
  95. data/lib/net/ssh/transport/ossl/hmac/none.rb +1 -1
  96. data/lib/net/ssh/transport/ossl/hmac/services.rb +1 -1
  97. data/lib/net/ssh/transport/ossl/hmac/sha1-96.rb +1 -1
  98. data/lib/net/ssh/transport/ossl/hmac/sha1.rb +1 -1
  99. data/lib/net/ssh/transport/ossl/key-factory.rb +5 -2
  100. data/lib/net/ssh/transport/ossl/services.rb +1 -1
  101. data/lib/net/ssh/transport/packet-stream.rb +1 -1
  102. data/lib/net/ssh/transport/services.rb +1 -1
  103. data/lib/net/ssh/transport/session.rb +10 -1
  104. data/lib/net/ssh/transport/version-negotiator.rb +1 -1
  105. data/lib/net/ssh/userauth/agent.rb +1 -1
  106. data/lib/net/ssh/userauth/constants.rb +1 -1
  107. data/lib/net/ssh/userauth/driver.rb +1 -1
  108. data/lib/net/ssh/userauth/methods/hostbased.rb +1 -1
  109. data/lib/net/ssh/userauth/methods/keyboard-interactive.rb +104 -0
  110. data/lib/net/ssh/userauth/methods/password.rb +1 -1
  111. data/lib/net/ssh/userauth/methods/publickey.rb +1 -1
  112. data/lib/net/ssh/userauth/methods/services.rb +28 -6
  113. data/lib/net/ssh/userauth/services.rb +8 -5
  114. data/lib/net/ssh/userauth/userkeys.rb +1 -1
  115. data/lib/net/ssh/util/buffer.rb +1 -1
  116. data/lib/net/ssh/util/openssl.rb +1 -1
  117. data/lib/net/ssh/util/prompter.rb +1 -1
  118. data/lib/net/ssh/version.rb +2 -2
  119. data/test/ALL-TESTS.rb +1 -1
  120. data/test/connection/tc_channel.rb +1 -1
  121. data/test/connection/tc_driver.rb +1 -1
  122. data/test/connection/tc_integration.rb +1 -1
  123. data/test/proxy/tc_http.rb +1 -1
  124. data/test/proxy/tc_socks4.rb +1 -1
  125. data/test/proxy/tc_socks5.rb +1 -1
  126. data/test/service/forward/tc_driver.rb +1 -1
  127. data/test/service/forward/tc_local_network_handler.rb +1 -1
  128. data/test/service/forward/tc_remote_network_handler.rb +1 -1
  129. data/test/service/process/tc_driver.rb +1 -1
  130. data/test/service/process/tc_integration.rb +1 -1
  131. data/test/service/process/tc_open.rb +1 -1
  132. data/test/service/process/tc_popen3.rb +13 -13
  133. data/test/tc_integration.rb +1 -1
  134. data/test/transport/compress/tc_none_compress.rb +1 -1
  135. data/test/transport/compress/tc_none_decompress.rb +1 -1
  136. data/test/transport/compress/tc_zlib_compress.rb +1 -1
  137. data/test/transport/compress/tc_zlib_decompress.rb +1 -1
  138. data/test/transport/kex/tc_dh.rb +2 -1
  139. data/test/transport/kex/tc_dh_gex.rb +1 -1
  140. data/test/transport/ossl/hmac/tc_hmac.rb +1 -1
  141. data/test/transport/ossl/hmac/tc_md5.rb +1 -1
  142. data/test/transport/ossl/hmac/tc_md5_96.rb +1 -1
  143. data/test/transport/ossl/hmac/tc_none.rb +1 -1
  144. data/test/transport/ossl/hmac/tc_sha1.rb +1 -1
  145. data/test/transport/ossl/hmac/tc_sha1_96.rb +1 -1
  146. data/test/transport/ossl/tc_buffer.rb +1 -1
  147. data/test/transport/ossl/tc_buffer_factory.rb +1 -1
  148. data/test/transport/ossl/tc_cipher_factory.rb +1 -1
  149. data/test/transport/ossl/tc_digest_factory.rb +1 -1
  150. data/test/transport/ossl/tc_hmac_factory.rb +1 -1
  151. data/test/transport/ossl/tc_key_factory.rb +1 -1
  152. data/test/transport/tc_algorithm_negotiator.rb +1 -1
  153. data/test/transport/tc_identity_cipher.rb +1 -1
  154. data/test/transport/tc_integration.rb +1 -1
  155. data/test/transport/tc_packet_stream.rb +1 -1
  156. data/test/transport/tc_session.rb +1 -1
  157. data/test/transport/tc_version_negotiator.rb +1 -1
  158. data/test/userauth/methods/tc_hostbased.rb +1 -1
  159. data/test/userauth/methods/tc_password.rb +1 -1
  160. data/test/userauth/methods/tc_publickey.rb +1 -1
  161. data/test/userauth/tc_agent.rb +1 -1
  162. data/test/userauth/tc_driver.rb +1 -1
  163. data/test/userauth/tc_integration.rb +15 -1
  164. data/test/userauth/tc_userkeys.rb +1 -1
  165. data/test/util/tc_buffer.rb +5 -5
  166. metadata +26 -6
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -72,6 +72,9 @@ module Net
72
72
  # SSH channel. This class should never be instantiated directly; use
73
73
  # the popen3 method instead.
74
74
  class SSHStdinPipe
75
+
76
+ # The channel used by this pipe.
77
+ attr_reader :channel
75
78
 
76
79
  # Create a new +stdin+ pipe on the given channel.
77
80
  def initialize( channel )
@@ -81,6 +84,7 @@ module Net
81
84
  # Write the given data as channel data to the underlying channel.
82
85
  def write( data )
83
86
  @channel.send_data data
87
+ @channel.connection.process true
84
88
  end
85
89
 
86
90
  # Write the given data as channel data to the underlying channel,
@@ -95,17 +99,31 @@ module Net
95
99
  # is subclassed by SSHStdoutPipe and SSHStderrPipe.
96
100
  class SSHOutputPipe
97
101
 
102
+ # The channel used by this pipe.
103
+ attr_reader :channel
104
+
98
105
  # Create a new output pipe on the given channel.
99
106
  def initialize( channel )
100
107
  @channel = channel
101
108
  @data = ""
102
109
  end
103
110
 
111
+ # Returns true if there are any bytes available on this pipe. This
112
+ # will do a non-blocking read on the connection to determine if
113
+ # there
114
+ def data_available?
115
+ if @data.length == 0
116
+ connection = @channel.connection
117
+ connection.process while connection.reader_ready?
118
+ end
119
+ @data.length > 0
120
+ end
121
+
104
122
  # Read all available bytes from the pipe. If there are no available
105
123
  # bytes, then this will block until data becomes available.
106
124
  def read
107
125
  if @data.length < 1
108
- @channel.connection.loop { @data.length < 1 }
126
+ @channel.connection.process while @data.length < 1
109
127
  end
110
128
 
111
129
  data, @data = @data, ""
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@byu.edu)
4
4
  # All rights reserved.
5
5
  #
6
6
  # This source file is distributed as part of the Net::SSH Secure Shell Client
@@ -33,11 +33,13 @@ module Net
33
33
  container.namespace_define :service do |ns|
34
34
  ns.require "net/ssh/service/forward/services", "#{self}::Forward"
35
35
  ns.require "net/ssh/service/process/services", "#{self}::Process"
36
+ ns.require "net/ssh/service/shell/services", "#{self}::Shell"
36
37
  end
37
38
 
38
39
  # Add the services to the services hash.
39
40
  container.services[ :forward ] = container.service.forward.driver
40
41
  container.services[ :process ] = container.service.process.driver
42
+ container.services[ :shell ] = container.service.shell.driver
41
43
 
42
44
  # Register the external services and add them to the collection of
43
45
  # known services.
@@ -0,0 +1,86 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@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
+ module Net
18
+ module SSH
19
+ module Service
20
+ module Shell
21
+
22
+ # The service driver for the Shell service. It manages the creation of
23
+ # new Shell::Shell and Shell::SyncShell subservices.
24
+ #
25
+ # Usage:
26
+ #
27
+ # Net::SSH.start( host ) do |session|
28
+ # shell = session.shell.open
29
+ #
30
+ # shell.cd "/home/foo"
31
+ # shell.mkdir "-p some/long/dir"
32
+ # shell.cd "some/long/dir"
33
+ # shell.touch "foo.txt"
34
+ # shell.exit
35
+ #
36
+ # session.loop
37
+ # end
38
+ #
39
+ # Or:
40
+ #
41
+ # Net::SSH.start( host ) do |session|
42
+ # shell = session.shell.sync
43
+ #
44
+ # shell.cd "/home/foo"
45
+ #
46
+ # out = shell.test "-e some/file.txt"
47
+ # if out.status == 0
48
+ # out = shell.cat "some/file.txt"
49
+ # puts out.stdout
50
+ # else
51
+ # puts "no such file 'some/file.txt'"
52
+ # end
53
+ #
54
+ # end
55
+ class Driver
56
+
57
+ # Create a new driver with the given logger and shell and
58
+ # sync factories.
59
+ def initialize( log, shell_factory, sync_factory )
60
+ @log = log
61
+ @shell_factory = shell_factory
62
+ @sync_factory = sync_factory
63
+ end
64
+
65
+ # Open a new shell, using the Shell::Shell subservice and
66
+ # the given options.
67
+ def open( options={} )
68
+ pty_opts = options[:pty]
69
+
70
+ @shell_factory.call( pty_opts )
71
+ end
72
+
73
+ # Open a new shell, using the Shell::SyncShell subservice and
74
+ # the given options.
75
+ def sync( options={} )
76
+ pty_opts = options[:pty]
77
+
78
+ @sync_factory.call( pty_opts )
79
+ end
80
+
81
+ end
82
+
83
+ end # Shell
84
+ end # Service
85
+ end # SSH
86
+ end # Net
@@ -0,0 +1,54 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@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
+ module Net
18
+ module SSH
19
+ module Service
20
+ module Shell
21
+
22
+ def register_services( container )
23
+
24
+ container.namespace_define :shell do |ns|
25
+
26
+ ns.shell do |c,p|
27
+ require 'net/ssh/service/shell/shell'
28
+ connection = c[:connection][:driver]
29
+ log = c[:log_for, p]
30
+ lambda { |pty| Shell.new( connection, log, pty ) }
31
+ end
32
+
33
+ ns.sync do |c,p|
34
+ require 'net/ssh/service/shell/sync'
35
+ connection = c[:connection][:driver]
36
+ log = c[:log_for, p]
37
+ shell = c[:shell]
38
+ lambda { |pty| SyncShell.new( shell, log, pty ) }
39
+ end
40
+
41
+ ns.driver do |c,p|
42
+ require 'net/ssh/service/shell/driver'
43
+ Driver.new( c[:log_for, p], c[:shell], c[:sync] )
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ module_function :register_services
50
+
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,213 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004,2005 Jamis Buck (jamis_buck@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
+ require 'stringio'
18
+
19
+ module Net
20
+ module SSH
21
+ module Service
22
+ module Shell
23
+
24
+ # A service class for interacting with a user's shell on a remote
25
+ # machine. The shell may be interacted with either with or without a
26
+ # pty.
27
+ class Shell
28
+
29
+ # Create a new shell over the given connection. The +pty_opts+
30
+ # parameter must be either a Hash of the allowed values for the
31
+ # Net::SSH::Connection::Channel#request_pty method, or a boolean
32
+ # value (indicating whether a pty should be allocated or not). This
33
+ # will block until the shell is open and ready to receive input.
34
+ def initialize( connection, log, pty_opts )
35
+ @connection = connection
36
+ @log = log
37
+
38
+ @pty_opts = pty_opts
39
+
40
+ @stdout = ""
41
+ @stderr = ""
42
+
43
+ @state = :opening
44
+ @connection.open_channel( "session", &method( :on_confirm ) )
45
+
46
+ @connection.loop { @state != :open && @state != :closed }
47
+ raise "could not open shell" if @state != :open
48
+ end
49
+
50
+ # Returns +true+ if the shell is open.
51
+ def open?
52
+ @state == :open
53
+ end
54
+
55
+ # Return the stdout output (if any) that the shell has generated
56
+ # since the last time this method was invoked.
57
+ def stdout
58
+ string, @stdout = @stdout, ""
59
+ string
60
+ end
61
+
62
+ # Returns +true+ if there is any data from the shell on stdout,
63
+ # consuming input on the connection in a non-blocking manner to make
64
+ # sure that any available data is considered.
65
+ def stdout?
66
+ exists = @stdout.length > 0
67
+ unless exists
68
+ consume_connection
69
+ exists = @stdout.length > 0
70
+ end
71
+ exists
72
+ end
73
+
74
+ # Return the stderr output (if any) that the shell has generated
75
+ # since the last time this method was invoked.
76
+ def stderr
77
+ string, @stderr = @stderr, ""
78
+ string
79
+ end
80
+
81
+ # Returns +true+ if there is any data from the shell on stderr,
82
+ # consuming input on the connection in a non-blocking manner to make
83
+ # sure that any available data is considered.
84
+ def stderr?
85
+ exists = @stderr.length > 0
86
+ unless exists
87
+ consume_connection
88
+ exists = @stderr.length > 0
89
+ end
90
+ exists
91
+ end
92
+
93
+ # Sends the given data to the shell on the shell's stdin stream.
94
+ def send_data( data )
95
+ raise "channel not open" unless @state == :open
96
+ @channel.send_data data
97
+ end
98
+
99
+ # Sends the given data to the shell on the stream indicated by the
100
+ # +type+ parameter.
101
+ def send_extended_data( type, data )
102
+ raise "channel not open" unless @state == :open
103
+ @channel.send_extended_data type, data
104
+ end
105
+
106
+ # Reinterprets method invocations as requests to send data to the
107
+ # shell. The method name and the arguments are concatenated together
108
+ # with spaces and a newline appended. The resulting string is sent
109
+ # to the shell via #send_data.
110
+ def method_missing( sym, *args )
111
+ cmd = sym.to_s
112
+ cmd << " " << args.join( " " ) unless args.empty?
113
+ send_data cmd + "\n"
114
+ end
115
+
116
+ undef_method :exit
117
+
118
+ private
119
+
120
+ # Consumes all available input on the connection.
121
+ def consume_connection
122
+ return unless @channel
123
+ connection = @channel.connection
124
+ connection.process while connection.reader_ready?
125
+ end
126
+
127
+ # Invoked when the channel has been confirmed.
128
+ def on_confirm( channel )
129
+ @channel = channel
130
+
131
+ @channel.on_close( &method( :on_close ) )
132
+ @channel.on_data( &method( :on_data ) )
133
+ @channel.on_eof( &method( :on_eof ) )
134
+ @channel.on_extended_data( &method( :on_extended_data ) )
135
+ @channel.on_request( &method( :on_request ) )
136
+ @channel.on_failure( &method( :on_failure ) )
137
+ @channel.on_success( &method( :on_success ) )
138
+
139
+ @pty_opts ? request_pty : request_shell
140
+ end
141
+
142
+ # Request a pty from the server for this channel, using the
143
+ # parameters given when the service was started.
144
+ def request_pty
145
+ @state = :pty
146
+ pty_opts = { :want_reply=>true }
147
+ pty_opts = @pty_opts.merge( pty_opts ) if @pty_opts.is_a?( Hash )
148
+ @channel.request_pty pty_opts
149
+ end
150
+
151
+ # Request that the user's shell be started on this channel. All
152
+ # subsequent input to the channel is interpreted as input to the
153
+ # shell.
154
+ def request_shell
155
+ @state = :shell
156
+ @channel.send_request "shell", nil, true
157
+ end
158
+
159
+ # Invoked when the channel closes. Changes the shell's state to
160
+ # closed.
161
+ def on_close( channel )
162
+ @state = :closed
163
+ end
164
+
165
+ # Invoked when data is received over the channel. It is written
166
+ # to the stdout stream.
167
+ def on_data( channel, data )
168
+ @stdout << data if @state == :open
169
+ end
170
+
171
+ # Invoked when the channel receives an eof notification. (Not
172
+ # currently used.)
173
+ def on_eof( channel )
174
+ end
175
+
176
+ # Invoked when extended data (stderr) is recieved. If type == 1,
177
+ # this data is written to the stderr stream; otherwise, it is
178
+ # ignored.
179
+ def on_extended_data( channel, type, data )
180
+ @stderr << data if @state == :open && type == 1
181
+ end
182
+
183
+ # Invoked when a request is received over the channel. (Not
184
+ # currently used.)
185
+ def on_request( channel, request, want_reply, data )
186
+ end
187
+
188
+ # Invoked when a request fails. Currently, this is only used in
189
+ # response to the pty or shell request, and will close the shell
190
+ # in reponse.
191
+ def on_failure( channel )
192
+ if @state == :pty || @state == :shell
193
+ @state = :closed
194
+ end
195
+ end
196
+
197
+ # Invoked when a request succeeds. Currently used only to manage
198
+ # the state machine and make sure that the shell gets opened after
199
+ # a successful pty request.
200
+ def on_success( channel )
201
+ if @state == :pty
202
+ request_shell
203
+ elsif @state == :shell
204
+ @state = :open
205
+ end
206
+ end
207
+
208
+ end
209
+
210
+ end # Shell
211
+ end # Service
212
+ end # SSH
213
+ end # Net