net-ssh 3.2.0.rc2 → 7.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +2 -2
- data/.dockerignore +6 -0
- data/.github/config/rubocop_linter_action.yml +4 -0
- data/.github/workflows/ci-with-docker.yml +44 -0
- data/.github/workflows/ci.yml +87 -0
- data/.github/workflows/rubocop.yml +16 -0
- data/.gitignore +13 -0
- data/.rubocop.yml +22 -0
- data/.rubocop_todo.yml +1081 -0
- data/CHANGES.txt +228 -7
- data/Dockerfile +27 -0
- data/Dockerfile.openssl3 +17 -0
- data/Gemfile +13 -0
- data/Gemfile.noed25519 +12 -0
- data/ISSUE_TEMPLATE.md +30 -0
- data/Manifest +4 -5
- data/README.md +297 -0
- data/Rakefile +125 -74
- data/SECURITY.md +4 -0
- data/appveyor.yml +58 -0
- data/docker-compose.yml +23 -0
- data/lib/net/ssh/authentication/agent.rb +279 -18
- data/lib/net/ssh/authentication/certificate.rb +183 -0
- data/lib/net/ssh/authentication/constants.rb +17 -15
- data/lib/net/ssh/authentication/ed25519.rb +186 -0
- data/lib/net/ssh/authentication/ed25519_loader.rb +31 -0
- data/lib/net/ssh/authentication/key_manager.rb +86 -39
- data/lib/net/ssh/authentication/methods/abstract.rb +67 -48
- data/lib/net/ssh/authentication/methods/hostbased.rb +34 -37
- data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +13 -13
- data/lib/net/ssh/authentication/methods/none.rb +16 -19
- data/lib/net/ssh/authentication/methods/password.rb +27 -17
- data/lib/net/ssh/authentication/methods/publickey.rb +96 -55
- data/lib/net/ssh/authentication/pageant.rb +471 -367
- data/lib/net/ssh/authentication/pub_key_fingerprint.rb +43 -0
- data/lib/net/ssh/authentication/session.rb +131 -121
- data/lib/net/ssh/buffer.rb +399 -300
- data/lib/net/ssh/buffered_io.rb +154 -150
- data/lib/net/ssh/config.rb +308 -185
- data/lib/net/ssh/connection/channel.rb +635 -613
- data/lib/net/ssh/connection/constants.rb +29 -29
- data/lib/net/ssh/connection/event_loop.rb +123 -0
- data/lib/net/ssh/connection/keepalive.rb +55 -51
- data/lib/net/ssh/connection/session.rb +620 -551
- data/lib/net/ssh/connection/term.rb +125 -123
- data/lib/net/ssh/errors.rb +101 -99
- data/lib/net/ssh/key_factory.rb +197 -105
- data/lib/net/ssh/known_hosts.rb +214 -127
- data/lib/net/ssh/loggable.rb +50 -49
- data/lib/net/ssh/packet.rb +83 -79
- data/lib/net/ssh/prompt.rb +50 -81
- data/lib/net/ssh/proxy/command.rb +105 -90
- data/lib/net/ssh/proxy/errors.rb +12 -10
- data/lib/net/ssh/proxy/http.rb +82 -79
- data/lib/net/ssh/proxy/https.rb +50 -0
- data/lib/net/ssh/proxy/jump.rb +54 -0
- data/lib/net/ssh/proxy/socks4.rb +2 -6
- data/lib/net/ssh/proxy/socks5.rb +14 -17
- data/lib/net/ssh/service/forward.rb +370 -317
- data/lib/net/ssh/test/channel.rb +145 -136
- data/lib/net/ssh/test/extensions.rb +131 -110
- data/lib/net/ssh/test/kex.rb +34 -32
- data/lib/net/ssh/test/local_packet.rb +46 -44
- data/lib/net/ssh/test/packet.rb +89 -70
- data/lib/net/ssh/test/remote_packet.rb +32 -30
- data/lib/net/ssh/test/script.rb +156 -142
- data/lib/net/ssh/test/socket.rb +49 -48
- data/lib/net/ssh/test.rb +82 -77
- data/lib/net/ssh/transport/algorithms.rb +441 -360
- data/lib/net/ssh/transport/cipher_factory.rb +96 -98
- data/lib/net/ssh/transport/constants.rb +32 -24
- data/lib/net/ssh/transport/ctr.rb +42 -22
- data/lib/net/ssh/transport/hmac/abstract.rb +81 -63
- data/lib/net/ssh/transport/hmac/md5.rb +0 -2
- data/lib/net/ssh/transport/hmac/md5_96.rb +0 -2
- data/lib/net/ssh/transport/hmac/none.rb +0 -2
- data/lib/net/ssh/transport/hmac/ripemd160.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1_96.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha2_256.rb +7 -11
- data/lib/net/ssh/transport/hmac/sha2_256_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_256_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac/sha2_512.rb +6 -9
- data/lib/net/ssh/transport/hmac/sha2_512_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_512_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac.rb +14 -12
- data/lib/net/ssh/transport/identity_cipher.rb +54 -52
- data/lib/net/ssh/transport/kex/abstract.rb +130 -0
- data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256.rb +39 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +30 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +33 -40
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha256.rb +11 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +119 -213
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +53 -61
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +5 -9
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +36 -90
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +18 -10
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +18 -10
- data/lib/net/ssh/transport/kex.rb +15 -12
- data/lib/net/ssh/transport/key_expander.rb +24 -20
- data/lib/net/ssh/transport/openssl.rb +161 -124
- data/lib/net/ssh/transport/packet_stream.rb +225 -185
- data/lib/net/ssh/transport/server_version.rb +55 -56
- data/lib/net/ssh/transport/session.rb +306 -255
- data/lib/net/ssh/transport/state.rb +178 -176
- data/lib/net/ssh/verifiers/accept_new.rb +33 -0
- data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +33 -0
- data/lib/net/ssh/verifiers/always.rb +58 -0
- data/lib/net/ssh/verifiers/never.rb +19 -0
- data/lib/net/ssh/version.rb +55 -53
- data/lib/net/ssh.rb +110 -47
- data/net-ssh-public_cert.pem +18 -18
- data/net-ssh.gemspec +36 -205
- data/support/ssh_tunnel_bug.rb +5 -5
- data.tar.gz.sig +0 -0
- metadata +153 -118
- metadata.gz.sig +0 -0
- data/.travis.yml +0 -18
- data/README.rdoc +0 -182
- data/lib/net/ssh/authentication/agent/java_pageant.rb +0 -85
- data/lib/net/ssh/authentication/agent/socket.rb +0 -178
- data/lib/net/ssh/ruby_compat.rb +0 -46
- data/lib/net/ssh/verifiers/lenient.rb +0 -30
- data/lib/net/ssh/verifiers/null.rb +0 -12
- data/lib/net/ssh/verifiers/secure.rb +0 -52
- data/lib/net/ssh/verifiers/strict.rb +0 -24
- data/setup.rb +0 -1585
- data/support/arcfour_check.rb +0 -20
- data/test/README.txt +0 -18
- data/test/authentication/methods/common.rb +0 -28
- data/test/authentication/methods/test_abstract.rb +0 -51
- data/test/authentication/methods/test_hostbased.rb +0 -114
- data/test/authentication/methods/test_keyboard_interactive.rb +0 -121
- data/test/authentication/methods/test_none.rb +0 -41
- data/test/authentication/methods/test_password.rb +0 -95
- data/test/authentication/methods/test_publickey.rb +0 -148
- data/test/authentication/test_agent.rb +0 -232
- data/test/authentication/test_key_manager.rb +0 -240
- data/test/authentication/test_session.rb +0 -107
- data/test/common.rb +0 -125
- data/test/configs/auth_off +0 -5
- data/test/configs/auth_on +0 -4
- data/test/configs/empty +0 -0
- data/test/configs/eqsign +0 -3
- data/test/configs/exact_match +0 -8
- data/test/configs/host_plus +0 -10
- data/test/configs/multihost +0 -4
- data/test/configs/negative_match +0 -6
- data/test/configs/nohost +0 -19
- data/test/configs/numeric_host +0 -4
- data/test/configs/proxy_remote_user +0 -2
- data/test/configs/send_env +0 -2
- data/test/configs/substitutes +0 -8
- data/test/configs/wild_cards +0 -14
- data/test/connection/test_channel.rb +0 -487
- data/test/connection/test_session.rb +0 -564
- data/test/integration/README.txt +0 -17
- data/test/integration/Vagrantfile +0 -12
- data/test/integration/common.rb +0 -63
- data/test/integration/playbook.yml +0 -56
- data/test/integration/test_forward.rb +0 -637
- data/test/integration/test_id_rsa_keys.rb +0 -96
- data/test/integration/test_proxy.rb +0 -93
- data/test/known_hosts/github +0 -1
- data/test/known_hosts/github_hash +0 -1
- data/test/manual/test_pageant.rb +0 -37
- data/test/start/test_connection.rb +0 -53
- data/test/start/test_options.rb +0 -57
- data/test/start/test_transport.rb +0 -28
- data/test/start/test_user_nil.rb +0 -27
- data/test/test_all.rb +0 -12
- data/test/test_buffer.rb +0 -433
- data/test/test_buffered_io.rb +0 -63
- data/test/test_config.rb +0 -268
- data/test/test_key_factory.rb +0 -191
- data/test/test_known_hosts.rb +0 -66
- data/test/transport/hmac/test_md5.rb +0 -41
- data/test/transport/hmac/test_md5_96.rb +0 -27
- data/test/transport/hmac/test_none.rb +0 -34
- data/test/transport/hmac/test_ripemd160.rb +0 -36
- data/test/transport/hmac/test_sha1.rb +0 -36
- data/test/transport/hmac/test_sha1_96.rb +0 -27
- data/test/transport/hmac/test_sha2_256.rb +0 -37
- data/test/transport/hmac/test_sha2_256_96.rb +0 -27
- data/test/transport/hmac/test_sha2_512.rb +0 -37
- data/test/transport/hmac/test_sha2_512_96.rb +0 -27
- data/test/transport/kex/test_diffie_hellman_group14_sha1.rb +0 -13
- data/test/transport/kex/test_diffie_hellman_group1_sha1.rb +0 -150
- data/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +0 -96
- data/test/transport/kex/test_diffie_hellman_group_exchange_sha256.rb +0 -19
- data/test/transport/kex/test_ecdh_sha2_nistp256.rb +0 -161
- data/test/transport/kex/test_ecdh_sha2_nistp384.rb +0 -38
- data/test/transport/kex/test_ecdh_sha2_nistp521.rb +0 -38
- data/test/transport/test_algorithms.rb +0 -328
- data/test/transport/test_cipher_factory.rb +0 -443
- data/test/transport/test_hmac.rb +0 -34
- data/test/transport/test_identity_cipher.rb +0 -40
- data/test/transport/test_packet_stream.rb +0 -1762
- data/test/transport/test_server_version.rb +0 -74
- data/test/transport/test_session.rb +0 -331
- data/test/transport/test_state.rb +0 -181
- data/test/verifiers/test_secure.rb +0 -40
@@ -2,671 +2,693 @@ require 'net/ssh/loggable'
|
|
2
2
|
require 'net/ssh/connection/constants'
|
3
3
|
require 'net/ssh/connection/term'
|
4
4
|
|
5
|
-
module Net
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
5
|
+
module Net
|
6
|
+
module SSH
|
7
|
+
module Connection
|
8
|
+
# The channel abstraction. Multiple "channels" can be multiplexed onto a
|
9
|
+
# single SSH channel, each operating independently and seemingly in parallel.
|
10
|
+
# This class represents a single such channel. Most operations performed
|
11
|
+
# with the Net::SSH library will involve using one or more channels.
|
12
|
+
#
|
13
|
+
# Channels are intended to be used asynchronously. You request that one be
|
14
|
+
# opened (via Connection::Session#open_channel), and when it is opened, your
|
15
|
+
# callback is invoked. Then, you set various other callbacks on the newly
|
16
|
+
# opened channel, which are called in response to the corresponding events.
|
17
|
+
# Programming with Net::SSH works best if you think of your programs as
|
18
|
+
# state machines. Complex programs are best implemented as objects that
|
19
|
+
# wrap a channel. See Net::SCP and Net::SFTP for examples of how complex
|
20
|
+
# state machines can be built on top of the SSH protocol.
|
21
|
+
#
|
22
|
+
# ssh.open_channel do |channel|
|
23
|
+
# channel.exec("/invoke/some/command") do |ch, success|
|
24
|
+
# abort "could not execute command" unless success
|
25
|
+
#
|
26
|
+
# channel.on_data do |ch, data|
|
27
|
+
# puts "got stdout: #{data}"
|
28
|
+
# channel.send_data "something for stdin\n"
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# channel.on_extended_data do |ch, type, data|
|
32
|
+
# puts "got stderr: #{data}"
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# channel.on_close do |ch|
|
36
|
+
# puts "channel is closing!"
|
37
|
+
# end
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# ssh.loop
|
42
|
+
#
|
43
|
+
# Channels also have a basic hash-like interface, that allows programs to
|
44
|
+
# store arbitrary state information on a channel object. This helps simplify
|
45
|
+
# the writing of state machines, especially when you may be juggling
|
46
|
+
# multiple open channels at the same time.
|
47
|
+
#
|
48
|
+
# Note that data sent across SSH channels are governed by maximum packet
|
49
|
+
# sizes and maximum window sizes. These details are managed internally
|
50
|
+
# by Net::SSH::Connection::Channel, so you may remain blissfully ignorant
|
51
|
+
# if you so desire, but you can always inspect the current maximums, as
|
52
|
+
# well as the remaining window size, using the reader attributes for those
|
53
|
+
# values.
|
54
|
+
class Channel
|
55
|
+
include Loggable
|
56
|
+
include Constants
|
57
|
+
|
58
|
+
# The local id for this channel, assigned by the Net::SSH::Connection::Session instance.
|
59
|
+
attr_reader :local_id
|
60
|
+
|
61
|
+
# The remote id for this channel, assigned by the remote host.
|
62
|
+
attr_reader :remote_id
|
63
|
+
|
64
|
+
# The type of this channel, usually "session".
|
65
|
+
attr_reader :type
|
66
|
+
|
67
|
+
# The underlying Net::SSH::Connection::Session instance that supports this channel.
|
68
|
+
attr_reader :connection
|
69
|
+
|
70
|
+
# The maximum packet size that the local host can receive.
|
71
|
+
attr_reader :local_maximum_packet_size
|
72
|
+
|
73
|
+
# The maximum amount of data that the local end of this channel can
|
74
|
+
# receive. This is a total, not per-packet.
|
75
|
+
attr_reader :local_maximum_window_size
|
76
|
+
|
77
|
+
# The maximum packet size that the remote host can receive.
|
78
|
+
attr_reader :remote_maximum_packet_size
|
79
|
+
|
80
|
+
# The maximum amount of data that the remote end of this channel can
|
81
|
+
# receive. This is a total, not per-packet.
|
82
|
+
attr_reader :remote_maximum_window_size
|
83
|
+
|
84
|
+
# This is the remaining window size on the local end of this channel. When
|
85
|
+
# this reaches zero, no more data can be received.
|
86
|
+
attr_reader :local_window_size
|
87
|
+
|
88
|
+
# This is the remaining window size on the remote end of this channel. When
|
89
|
+
# this reaches zero, no more data can be sent.
|
90
|
+
attr_reader :remote_window_size
|
91
|
+
|
92
|
+
# A hash of properties for this channel. These can be used to store state
|
93
|
+
# information about this channel. See also #[] and #[]=.
|
94
|
+
attr_reader :properties
|
95
|
+
|
96
|
+
# The output buffer for this channel. Data written to the channel is
|
97
|
+
# enqueued here, to be written as CHANNEL_DATA packets during each pass of
|
98
|
+
# the event loop. See Connection::Session#process and #enqueue_pending_output.
|
99
|
+
attr_reader :output # :nodoc:
|
100
|
+
|
101
|
+
# The list of pending requests. Each time a request is sent which requires
|
102
|
+
# a reply, the corresponding callback is pushed onto this queue. As responses
|
103
|
+
# arrive, they are shifted off the front and handled.
|
104
|
+
attr_reader :pending_requests # :nodoc:
|
105
|
+
|
106
|
+
# Instantiates a new channel on the given connection, of the given type,
|
107
|
+
# and with the given id. If a block is given, it will be remembered until
|
108
|
+
# the channel is confirmed open by the server, and will be invoked at
|
109
|
+
# that time (see #do_open_confirmation).
|
110
|
+
#
|
111
|
+
# This also sets the default maximum packet size and maximum window size.
|
112
|
+
def initialize(connection, type, local_id, max_pkt_size = 0x8000, max_win_size = 0x20000, &on_confirm_open)
|
113
|
+
self.logger = connection.logger
|
114
|
+
|
115
|
+
@connection = connection
|
116
|
+
@type = type
|
117
|
+
@local_id = local_id
|
118
|
+
|
119
|
+
@local_maximum_packet_size = max_pkt_size
|
120
|
+
@local_window_size = @local_maximum_window_size = max_win_size
|
121
|
+
|
122
|
+
@on_confirm_open = on_confirm_open
|
123
|
+
|
124
|
+
@output = Buffer.new
|
125
|
+
|
126
|
+
@properties = {}
|
127
|
+
|
128
|
+
@pending_requests = []
|
129
|
+
@on_open_failed = @on_data = @on_extended_data = @on_process = @on_close = @on_eof = nil
|
130
|
+
@on_request = {}
|
131
|
+
@closing = @eof = @sent_eof = @local_closed = @remote_closed = false
|
132
|
+
end
|
131
133
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
134
|
+
# A shortcut for accessing properties of the channel (see #properties).
|
135
|
+
def [](name)
|
136
|
+
@properties[name]
|
137
|
+
end
|
136
138
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
139
|
+
# A shortcut for setting properties of the channel (see #properties).
|
140
|
+
def []=(name, value)
|
141
|
+
@properties[name] = value
|
142
|
+
end
|
141
143
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
144
|
+
# Syntactic sugar for executing a command. Sends a channel request asking
|
145
|
+
# that the given command be invoked. If the block is given, it will be
|
146
|
+
# called when the server responds. The first parameter will be the
|
147
|
+
# channel, and the second will be true or false, indicating whether the
|
148
|
+
# request succeeded or not. In this case, success means that the command
|
149
|
+
# is being executed, not that it has completed, and failure means that the
|
150
|
+
# command altogether failed to be executed.
|
151
|
+
#
|
152
|
+
# channel.exec "ls -l /home" do |ch, success|
|
153
|
+
# if success
|
154
|
+
# puts "command has begun executing..."
|
155
|
+
# # this is a good place to hang callbacks like #on_data...
|
156
|
+
# else
|
157
|
+
# puts "alas! the command could not be invoked!"
|
158
|
+
# end
|
159
|
+
# end
|
160
|
+
def exec(command, &block)
|
161
|
+
send_channel_request("exec", :string, command, &block)
|
162
|
+
end
|
161
163
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
164
|
+
# Syntactic sugar for requesting that a subsystem be started. Subsystems
|
165
|
+
# are a way for other protocols (like SFTP) to be run, using SSH as
|
166
|
+
# the transport. Generally, you'll never need to call this directly unless
|
167
|
+
# you are the implementor of something that consumes an SSH subsystem, like
|
168
|
+
# SFTP.
|
169
|
+
#
|
170
|
+
# channel.subsystem("sftp") do |ch, success|
|
171
|
+
# if success
|
172
|
+
# puts "subsystem successfully started"
|
173
|
+
# else
|
174
|
+
# puts "subsystem could not be started"
|
175
|
+
# end
|
176
|
+
# end
|
177
|
+
def subsystem(subsystem, &block)
|
178
|
+
send_channel_request("subsystem", :string, subsystem, &block)
|
179
|
+
end
|
178
180
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
181
|
+
# Syntactic sugar for setting an environment variable in the remote
|
182
|
+
# process' environment. Note that for security reasons, the server may
|
183
|
+
# refuse to set certain environment variables, or all, at the server's
|
184
|
+
# discretion. If you are connecting to an OpenSSH server, you will
|
185
|
+
# need to update the AcceptEnv setting in the sshd_config to include the
|
186
|
+
# environment variables you want to send.
|
187
|
+
#
|
188
|
+
# channel.env "PATH", "/usr/local/bin"
|
189
|
+
def env(variable_name, variable_value, &block)
|
190
|
+
send_channel_request("env", :string, variable_name, :string, variable_value, &block)
|
191
|
+
end
|
190
192
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
193
|
+
# A hash of the valid PTY options (see #request_pty).
|
194
|
+
VALID_PTY_OPTIONS = { term: "xterm",
|
195
|
+
chars_wide: 80,
|
196
|
+
chars_high: 24,
|
197
|
+
pixels_wide: 640,
|
198
|
+
pixels_high: 480,
|
199
|
+
modes: {} }
|
200
|
+
|
201
|
+
# Requests that a pseudo-tty (or "pty") be made available for this channel.
|
202
|
+
# This is useful when you want to invoke and interact with some kind of
|
203
|
+
# screen-based program (e.g., vim, or some menuing system).
|
204
|
+
#
|
205
|
+
# Note, that without a pty some programs (e.g. sudo, or subversion) on
|
206
|
+
# some systems, will not be able to run interactively, and will error
|
207
|
+
# instead of prompt if they ever need some user interaction.
|
208
|
+
#
|
209
|
+
# Note, too, that when a pty is requested, user's shell configuration
|
210
|
+
# scripts (.bashrc and such) are not run by default, whereas they are
|
211
|
+
# run when a pty is not present.
|
212
|
+
#
|
213
|
+
# channel.request_pty do |ch, success|
|
214
|
+
# if success
|
215
|
+
# puts "pty successfully obtained"
|
216
|
+
# else
|
217
|
+
# puts "could not obtain pty"
|
218
|
+
# end
|
219
|
+
# end
|
220
|
+
def request_pty(opts = {}, &block)
|
221
|
+
extra = opts.keys - VALID_PTY_OPTIONS.keys
|
222
|
+
raise ArgumentError, "invalid option(s) to request_pty: #{extra.inspect}" if extra.any?
|
223
|
+
|
224
|
+
opts = VALID_PTY_OPTIONS.merge(opts)
|
225
|
+
|
226
|
+
modes = opts[:modes].inject(Buffer.new) do |memo, (mode, data)|
|
227
|
+
memo.write_byte(mode).write_long(data)
|
228
|
+
end
|
229
|
+
# mark the end of the mode opcode list with a 0 byte
|
230
|
+
modes.write_byte(0)
|
229
231
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
232
|
+
send_channel_request("pty-req", :string, opts[:term],
|
233
|
+
:long, opts[:chars_wide], :long, opts[:chars_high],
|
234
|
+
:long, opts[:pixels_wide], :long, opts[:pixels_high],
|
235
|
+
:string, modes.to_s, &block)
|
236
|
+
end
|
235
237
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
238
|
+
# Sends data to the channel's remote endpoint. This usually has the
|
239
|
+
# effect of sending the given string to the remote process' stdin stream.
|
240
|
+
# Note that it does not immediately send the data across the channel,
|
241
|
+
# but instead merely appends the given data to the channel's output buffer,
|
242
|
+
# preparatory to being packaged up and sent out the next time the connection
|
243
|
+
# is accepting data. (A connection might not be accepting data if, for
|
244
|
+
# instance, it has filled its data window and has not yet been resized by
|
245
|
+
# the remote end-point.)
|
246
|
+
#
|
247
|
+
# This will raise an exception if the channel has previously declared
|
248
|
+
# that no more data will be sent (see #eof!).
|
249
|
+
#
|
250
|
+
# channel.send_data("the password\n")
|
251
|
+
def send_data(data)
|
252
|
+
raise EOFError, "cannot send data if channel has declared eof" if eof?
|
253
|
+
|
254
|
+
output.append(data.to_s)
|
255
|
+
end
|
253
256
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
257
|
+
# Returns true if the channel exists in the channel list of the session,
|
258
|
+
# and false otherwise. This can be used to determine whether a channel has
|
259
|
+
# been closed or not.
|
260
|
+
#
|
261
|
+
# ssh.loop { channel.active? }
|
262
|
+
def active?
|
263
|
+
connection.channels.key?(local_id)
|
264
|
+
end
|
262
265
|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
266
|
+
# Runs the SSH event loop until the channel is no longer active. This is
|
267
|
+
# handy for blocking while you wait for some channel to finish.
|
268
|
+
#
|
269
|
+
# channel.exec("grep ...") { ... }
|
270
|
+
# channel.wait
|
271
|
+
def wait
|
272
|
+
connection.loop { active? }
|
273
|
+
end
|
271
274
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
275
|
+
# True if close() has been called; NOTE: if the channel has data waiting to
|
276
|
+
# be sent then the channel will close after all the data is sent. See
|
277
|
+
# closed?() to determine if we have actually sent CHANNEL_CLOSE to server.
|
278
|
+
# This may be true for awhile before closed? returns true if we are still
|
279
|
+
# sending buffered output to server.
|
280
|
+
def closing?
|
281
|
+
@closing
|
282
|
+
end
|
280
283
|
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
284
|
+
# True if we have sent CHANNEL_CLOSE to the remote server.
|
285
|
+
def local_closed?
|
286
|
+
@local_closed
|
287
|
+
end
|
285
288
|
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
+
def remote_closed?
|
290
|
+
@remote_closed
|
291
|
+
end
|
289
292
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
+
def remote_closed!
|
294
|
+
@remote_closed = true
|
295
|
+
end
|
293
296
|
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
@closing = true
|
299
|
-
end
|
297
|
+
# Requests that the channel be closed. It only marks the channel to be closed
|
298
|
+
# the CHANNEL_CLOSE message will be sent from event loop
|
299
|
+
def close
|
300
|
+
return if @closing
|
300
301
|
|
301
|
-
|
302
|
-
|
303
|
-
# this is true will result in an exception being raised.
|
304
|
-
def eof?
|
305
|
-
@eof
|
306
|
-
end
|
302
|
+
@closing = true
|
303
|
+
end
|
307
304
|
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
end
|
305
|
+
# Returns true if the local end of the channel has declared that no more
|
306
|
+
# data is forthcoming (see #eof!). Trying to send data via #send_data when
|
307
|
+
# this is true will result in an exception being raised.
|
308
|
+
def eof?
|
309
|
+
@eof
|
310
|
+
end
|
315
311
|
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
enqueue_pending_output
|
312
|
+
# Tells the remote end of the channel that no more data is forthcoming
|
313
|
+
# from this end of the channel. The remote end may still send data.
|
314
|
+
# The CHANNEL_EOF packet will be sent once the output buffer is empty.
|
315
|
+
def eof!
|
316
|
+
return if eof?
|
322
317
|
|
323
|
-
|
324
|
-
|
325
|
-
@sent_eof = true
|
326
|
-
end
|
318
|
+
@eof = true
|
319
|
+
end
|
327
320
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
321
|
+
# If an #on_process handler has been set up, this will cause it to be
|
322
|
+
# invoked (passing the channel itself as an argument). It also causes all
|
323
|
+
# pending output to be enqueued as CHANNEL_DATA packets (see #enqueue_pending_output).
|
324
|
+
def process
|
325
|
+
@on_process.call(self) if @on_process
|
326
|
+
enqueue_pending_output
|
334
327
|
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
# channel.on_data do |ch, data|
|
340
|
-
# puts "got data: #{data.inspect}"
|
341
|
-
# end
|
342
|
-
#
|
343
|
-
# Data received this way is typically the data written by the remote
|
344
|
-
# process to its +stdout+ stream.
|
345
|
-
def on_data(&block)
|
346
|
-
old, @on_data = @on_data, block
|
347
|
-
old
|
348
|
-
end
|
328
|
+
if @eof and not @sent_eof and output.empty? and remote_id and not @local_closed
|
329
|
+
connection.send_message(Buffer.from(:byte, CHANNEL_EOF, :long, remote_id))
|
330
|
+
@sent_eof = true
|
331
|
+
end
|
349
332
|
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
#
|
357
|
-
# channel.on_extended_data do |ch, type, data|
|
358
|
-
# puts "got stderr: #{data.inspect}"
|
359
|
-
# end
|
360
|
-
def on_extended_data(&block)
|
361
|
-
old, @on_extended_data = @on_extended_data, block
|
362
|
-
old
|
363
|
-
end
|
333
|
+
if @closing and not @local_closed and output.empty? and remote_id
|
334
|
+
connection.send_message(Buffer.from(:byte, CHANNEL_CLOSE, :long, remote_id))
|
335
|
+
@local_closed = true
|
336
|
+
connection.cleanup_channel(self)
|
337
|
+
end
|
338
|
+
end
|
364
339
|
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
# end
|
380
|
-
#
|
381
|
-
# channel.on_process do |ch|
|
382
|
-
# if channel[:data] =~ /^.*?\n/
|
383
|
-
# puts $&
|
384
|
-
# channel[:data] = $'
|
385
|
-
# end
|
386
|
-
# end
|
387
|
-
def on_process(&block)
|
388
|
-
old, @on_process = @on_process, block
|
389
|
-
old
|
390
|
-
end
|
340
|
+
# Registers a callback to be invoked when data packets are received by the
|
341
|
+
# channel. The callback is called with the channel as the first argument,
|
342
|
+
# and the data as the second.
|
343
|
+
#
|
344
|
+
# channel.on_data do |ch, data|
|
345
|
+
# puts "got data: #{data.inspect}"
|
346
|
+
# end
|
347
|
+
#
|
348
|
+
# Data received this way is typically the data written by the remote
|
349
|
+
# process to its +stdout+ stream.
|
350
|
+
def on_data(&block)
|
351
|
+
old, @on_data = @on_data, block
|
352
|
+
old
|
353
|
+
end
|
391
354
|
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
355
|
+
# Registers a callback to be invoked when extended data packets are received
|
356
|
+
# by the channel. The callback is called with the channel as the first
|
357
|
+
# argument, the data type (as an integer) as the second, and the data as
|
358
|
+
# the third. Extended data is almost exclusively used to send +stderr+ data
|
359
|
+
# (+type+ == 1). Other extended data types are not defined by the SSH
|
360
|
+
# protocol.
|
361
|
+
#
|
362
|
+
# channel.on_extended_data do |ch, type, data|
|
363
|
+
# puts "got stderr: #{data.inspect}"
|
364
|
+
# end
|
365
|
+
def on_extended_data(&block)
|
366
|
+
old, @on_extended_data = @on_extended_data, block
|
367
|
+
old
|
368
|
+
end
|
402
369
|
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
370
|
+
# Registers a callback to be invoked for each pass of the event loop for
|
371
|
+
# this channel. There are no guarantees on timeliness in the event loop,
|
372
|
+
# but it will be called roughly once for each packet received by the
|
373
|
+
# connection (not the channel). This callback is invoked with the channel
|
374
|
+
# as the sole argument.
|
375
|
+
#
|
376
|
+
# Here's an example that accumulates the channel data into a variable on
|
377
|
+
# the channel itself, and displays individual lines in the input one
|
378
|
+
# at a time when the channel is processed:
|
379
|
+
#
|
380
|
+
# channel[:data] = ""
|
381
|
+
#
|
382
|
+
# channel.on_data do |ch, data|
|
383
|
+
# channel[:data] << data
|
384
|
+
# end
|
385
|
+
#
|
386
|
+
# channel.on_process do |ch|
|
387
|
+
# if channel[:data] =~ /^.*?\n/
|
388
|
+
# puts $&
|
389
|
+
# channel[:data] = $'
|
390
|
+
# end
|
391
|
+
# end
|
392
|
+
def on_process(&block)
|
393
|
+
old, @on_process = @on_process, block
|
394
|
+
old
|
395
|
+
end
|
414
396
|
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
def on_open_failed(&block)
|
426
|
-
old, @on_open_failed = @on_open_failed, block
|
427
|
-
old
|
428
|
-
end
|
397
|
+
# Registers a callback to be invoked when the server acknowledges that a
|
398
|
+
# channel is closed. This is invoked with the channel as the sole argument.
|
399
|
+
#
|
400
|
+
# channel.on_close do |ch|
|
401
|
+
# puts "remote end is closing!"
|
402
|
+
# end
|
403
|
+
def on_close(&block)
|
404
|
+
old, @on_close = @on_close, block
|
405
|
+
old
|
406
|
+
end
|
429
407
|
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
#
|
442
|
-
# Some common channel requests that your programs might want to listen
|
443
|
-
# for are:
|
444
|
-
#
|
445
|
-
# * "exit-status" : the exit status of the remote process will be reported
|
446
|
-
# as a long integer in the data buffer, which you can grab via
|
447
|
-
# data.read_long.
|
448
|
-
# * "exit-signal" : if the remote process died as a result of a signal
|
449
|
-
# being sent to it, the signal will be reported as a string in the
|
450
|
-
# data, via data.read_string. (Not all SSH servers support this channel
|
451
|
-
# request type.)
|
452
|
-
#
|
453
|
-
# channel.on_request "exit-status" do |ch, data|
|
454
|
-
# puts "process terminated with exit status: #{data.read_long}"
|
455
|
-
# end
|
456
|
-
def on_request(type, &block)
|
457
|
-
old, @on_request[type] = @on_request[type], block
|
458
|
-
old
|
459
|
-
end
|
408
|
+
# Registers a callback to be invoked when the server indicates that no more
|
409
|
+
# data will be sent to the channel (although the channel can still send
|
410
|
+
# data to the server). The channel is the sole argument to the callback.
|
411
|
+
#
|
412
|
+
# channel.on_eof do |ch|
|
413
|
+
# puts "remote end is done sending data"
|
414
|
+
# end
|
415
|
+
def on_eof(&block)
|
416
|
+
old, @on_eof = @on_eof, block
|
417
|
+
old
|
418
|
+
end
|
460
419
|
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
# puts "user shell started successfully"
|
476
|
-
# else
|
477
|
-
# puts "could not start user shell"
|
478
|
-
# end
|
479
|
-
# end
|
480
|
-
#
|
481
|
-
# Most channel requests you'll want to send are already wrapped in more
|
482
|
-
# convenient helper methods (see #exec and #subsystem).
|
483
|
-
def send_channel_request(request_name, *data, &callback)
|
484
|
-
info { "sending channel request #{request_name.inspect}" }
|
485
|
-
fail "Channel open not yet confirmed, please call send_channel_request(or exec) from block of open_channel" unless remote_id
|
486
|
-
msg = Buffer.from(:byte, CHANNEL_REQUEST,
|
487
|
-
:long, remote_id, :string, request_name,
|
488
|
-
:bool, !callback.nil?, *data)
|
489
|
-
connection.send_message(msg)
|
490
|
-
pending_requests << callback if callback
|
491
|
-
end
|
420
|
+
# Registers a callback to be invoked when the server was unable to open
|
421
|
+
# the requested channel. The channel itself will be passed to the block,
|
422
|
+
# along with the integer "reason code" for the failure, and a textual
|
423
|
+
# description of the failure from the server.
|
424
|
+
#
|
425
|
+
# channel = session.open_channel do |ch|
|
426
|
+
# # ..
|
427
|
+
# end
|
428
|
+
#
|
429
|
+
# channel.on_open_failed { |ch, code, desc| ... }
|
430
|
+
def on_open_failed(&block)
|
431
|
+
old, @on_open_failed = @on_open_failed, block
|
432
|
+
old
|
433
|
+
end
|
492
434
|
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
435
|
+
# Registers a callback to be invoked when a channel request of the given
|
436
|
+
# type is received. The callback will receive the channel as the first
|
437
|
+
# argument, and the associated (unparsed) data as the second. The data
|
438
|
+
# will be a Net::SSH::Buffer that you will need to parse, yourself,
|
439
|
+
# according to the kind of request you are watching.
|
440
|
+
#
|
441
|
+
# By default, if the request wants a reply, Net::SSH will send a
|
442
|
+
# CHANNEL_SUCCESS response for any request that was handled by a registered
|
443
|
+
# callback, and CHANNEL_FAILURE for any that wasn't, but if you want your
|
444
|
+
# registered callback to result in a CHANNEL_FAILURE response, just raise
|
445
|
+
# Net::SSH::ChannelRequestFailed.
|
446
|
+
#
|
447
|
+
# Some common channel requests that your programs might want to listen
|
448
|
+
# for are:
|
449
|
+
#
|
450
|
+
# * "exit-status" : the exit status of the remote process will be reported
|
451
|
+
# as a long integer in the data buffer, which you can grab via
|
452
|
+
# data.read_long.
|
453
|
+
# * "exit-signal" : if the remote process died as a result of a signal
|
454
|
+
# being sent to it, the signal will be reported as a string in the
|
455
|
+
# data, via data.read_string. (Not all SSH servers support this channel
|
456
|
+
# request type.)
|
457
|
+
#
|
458
|
+
# channel.on_request "exit-status" do |ch, data|
|
459
|
+
# puts "process terminated with exit status: #{data.read_long}"
|
460
|
+
# end
|
461
|
+
def on_request(type, &block)
|
462
|
+
old, @on_request[type] = @on_request[type], block
|
463
|
+
old
|
515
464
|
end
|
516
|
-
end
|
517
465
|
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
466
|
+
# Sends a new channel request with the given name. The extra +data+
|
467
|
+
# parameter must either be empty, or consist of an even number of
|
468
|
+
# arguments. See Net::SSH::Buffer.from for a description of their format.
|
469
|
+
# If a block is given, it is registered as a callback for a pending
|
470
|
+
# request, and the packet will be flagged so that the server knows a
|
471
|
+
# reply is required. If no block is given, the server will send no
|
472
|
+
# response to this request. Responses, where required, will cause the
|
473
|
+
# callback to be invoked with the channel as the first argument, and
|
474
|
+
# either true or false as the second, depending on whether the request
|
475
|
+
# succeeded or not. The meaning of "success" and "failure" in this context
|
476
|
+
# is dependent on the specific request that was sent.
|
477
|
+
#
|
478
|
+
# channel.send_channel_request "shell" do |ch, success|
|
479
|
+
# if success
|
480
|
+
# puts "user shell started successfully"
|
481
|
+
# else
|
482
|
+
# puts "could not start user shell"
|
483
|
+
# end
|
484
|
+
# end
|
485
|
+
#
|
486
|
+
# Most channel requests you'll want to send are already wrapped in more
|
487
|
+
# convenient helper methods (see #exec and #subsystem).
|
488
|
+
def send_channel_request(request_name, *data, &callback)
|
489
|
+
info { "sending channel request #{request_name.inspect}" }
|
490
|
+
fail "Channel open not yet confirmed, please call send_channel_request(or exec) from block of open_channel" unless remote_id
|
491
|
+
|
492
|
+
msg = Buffer.from(:byte, CHANNEL_REQUEST,
|
493
|
+
:long, remote_id, :string, request_name,
|
494
|
+
:bool, !callback.nil?, *data)
|
495
|
+
connection.send_message(msg)
|
496
|
+
pending_requests << callback if callback
|
497
|
+
end
|
532
498
|
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
499
|
+
public # these methods are public, but for Net::SSH internal use only
|
500
|
+
|
501
|
+
# Enqueues pending output at the connection as CHANNEL_DATA packets. This
|
502
|
+
# does nothing if the channel has not yet been confirmed open (see
|
503
|
+
# #do_open_confirmation). This is called automatically by #process, which
|
504
|
+
# is called from the event loop (Connection::Session#process). You will
|
505
|
+
# generally not need to invoke it directly.
|
506
|
+
def enqueue_pending_output # :nodoc:
|
507
|
+
return unless remote_id
|
508
|
+
|
509
|
+
while output.length > 0
|
510
|
+
length = output.length
|
511
|
+
length = remote_window_size if length > remote_window_size
|
512
|
+
length = remote_maximum_packet_size if length > remote_maximum_packet_size
|
513
|
+
|
514
|
+
if length > 0
|
515
|
+
connection.send_message(Buffer.from(:byte, CHANNEL_DATA, :long, remote_id, :string, output.read(length)))
|
516
|
+
output.consume!
|
517
|
+
@remote_window_size -= length
|
518
|
+
else
|
519
|
+
break
|
520
|
+
end
|
521
|
+
end
|
542
522
|
end
|
543
|
-
end
|
544
523
|
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
524
|
+
# Invoked when the server confirms that a channel has been opened.
|
525
|
+
# The remote_id is the id of the channel as assigned by the remote host,
|
526
|
+
# and max_window and max_packet are the maximum window and maximum
|
527
|
+
# packet sizes, respectively. If an open-confirmation callback was
|
528
|
+
# given when the channel was created, it is invoked at this time with
|
529
|
+
# the channel itself as the sole argument.
|
530
|
+
def do_open_confirmation(remote_id, max_window, max_packet) # :nodoc:
|
531
|
+
@remote_id = remote_id
|
532
|
+
@remote_window_size = @remote_maximum_window_size = max_window
|
533
|
+
@remote_maximum_packet_size = max_packet
|
534
|
+
connection.forward.agent(self) if connection.options[:forward_agent] && type == "session"
|
535
|
+
forward_local_env(connection.options[:send_env]) if connection.options[:send_env]
|
536
|
+
set_remote_env(connection.options[:set_env]) if connection.options[:set_env]
|
537
|
+
@on_confirm_open.call(self) if @on_confirm_open
|
538
|
+
end
|
553
539
|
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
begin
|
566
|
-
callback = @on_request[request] or raise ChannelRequestFailed
|
567
|
-
callback.call(self, data)
|
568
|
-
rescue ChannelRequestFailed
|
569
|
-
result = false
|
570
|
-
end
|
571
|
-
|
572
|
-
if want_reply
|
573
|
-
msg = Buffer.from(:byte, result ? CHANNEL_SUCCESS : CHANNEL_FAILURE, :long, remote_id)
|
574
|
-
connection.send_message(msg)
|
540
|
+
# Invoked when the server failed to open the channel. If an #on_open_failed
|
541
|
+
# callback was specified, it will be invoked with the channel, reason code,
|
542
|
+
# and description as arguments. Otherwise, a ChannelOpenFailed exception
|
543
|
+
# will be raised.
|
544
|
+
def do_open_failed(reason_code, description)
|
545
|
+
if @on_open_failed
|
546
|
+
@on_open_failed.call(self, reason_code, description)
|
547
|
+
else
|
548
|
+
raise ChannelOpenFailed.new(reason_code, description)
|
549
|
+
end
|
575
550
|
end
|
576
|
-
end
|
577
551
|
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
end
|
552
|
+
# Invoked when the server sends a CHANNEL_WINDOW_ADJUST packet, and
|
553
|
+
# causes the remote window size to be adjusted upwards by the given
|
554
|
+
# number of bytes. This has the effect of allowing more data to be sent
|
555
|
+
# from the local end to the remote end of the channel.
|
556
|
+
def do_window_adjust(bytes) # :nodoc:
|
557
|
+
@remote_maximum_window_size += bytes
|
558
|
+
@remote_window_size += bytes
|
559
|
+
end
|
587
560
|
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
561
|
+
# Invoked when the server sends a channel request. If any #on_request
|
562
|
+
# callback has been registered for the specific type of this request,
|
563
|
+
# it is invoked. If +want_reply+ is true, a packet will be sent of
|
564
|
+
# either CHANNEL_SUCCESS or CHANNEL_FAILURE type. If there was no callback
|
565
|
+
# to handle the request, CHANNEL_FAILURE will be sent. Otherwise,
|
566
|
+
# CHANNEL_SUCCESS, unless the callback raised ChannelRequestFailed. The
|
567
|
+
# callback should accept the channel as the first argument, and the
|
568
|
+
# request-specific data as the second.
|
569
|
+
def do_request(request, want_reply, data) # :nodoc:
|
570
|
+
result = true
|
571
|
+
|
572
|
+
begin
|
573
|
+
callback = @on_request[request] or raise ChannelRequestFailed
|
574
|
+
callback.call(self, data)
|
575
|
+
rescue ChannelRequestFailed
|
576
|
+
result = false
|
577
|
+
end
|
596
578
|
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
end
|
579
|
+
if want_reply
|
580
|
+
msg = Buffer.from(:byte, result ? CHANNEL_SUCCESS : CHANNEL_FAILURE, :long, remote_id)
|
581
|
+
connection.send_message(msg)
|
582
|
+
end
|
583
|
+
end
|
603
584
|
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
585
|
+
# Invokes the #on_data callback when the server sends data to the
|
586
|
+
# channel. This will reduce the available window size on the local end,
|
587
|
+
# but does not actually throttle requests that come in illegally when
|
588
|
+
# the window size is too small. The callback is invoked with the channel
|
589
|
+
# as the first argument, and the data as the second.
|
590
|
+
def do_data(data) # :nodoc:
|
591
|
+
update_local_window_size(data.length)
|
592
|
+
@on_data.call(self, data) if @on_data
|
593
|
+
end
|
609
594
|
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
595
|
+
# Invokes the #on_extended_data callback when the server sends
|
596
|
+
# extended data to the channel. This will reduce the available window
|
597
|
+
# size on the local end. The callback is invoked with the channel,
|
598
|
+
# type, and data.
|
599
|
+
def do_extended_data(type, data)
|
600
|
+
update_local_window_size(data.length)
|
601
|
+
@on_extended_data.call(self, type, data) if @on_extended_data
|
617
602
|
end
|
618
|
-
end
|
619
603
|
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
else
|
626
|
-
error { "channel success recieved with no pending request to handle it (bug?)" }
|
604
|
+
# Invokes the #on_eof callback when the server indicates that no
|
605
|
+
# further data is forthcoming. The callback is invoked with the channel
|
606
|
+
# as the argument.
|
607
|
+
def do_eof
|
608
|
+
@on_eof.call(self) if @on_eof
|
627
609
|
end
|
628
|
-
end
|
629
610
|
|
630
|
-
|
611
|
+
# Invokes the #on_close callback when the server closes a channel.
|
612
|
+
# The channel is the only argument.
|
613
|
+
def do_close
|
614
|
+
@on_close.call(self) if @on_close
|
615
|
+
end
|
631
616
|
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
617
|
+
# Invokes the next pending request callback with +false+ as the second
|
618
|
+
# argument.
|
619
|
+
def do_failure
|
620
|
+
if callback = pending_requests.shift
|
621
|
+
callback.call(self, false)
|
622
|
+
else
|
623
|
+
error { "channel failure received with no pending request to handle it (bug?)" }
|
624
|
+
end
|
625
|
+
end
|
637
626
|
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
:long, remote_id, :long, 0x20000))
|
647
|
-
@local_window_size += 0x20000
|
648
|
-
@local_maximum_window_size += 0x20000
|
627
|
+
# Invokes the next pending request callback with +true+ as the second
|
628
|
+
# argument.
|
629
|
+
def do_success
|
630
|
+
if callback = pending_requests.shift
|
631
|
+
callback.call(self, true)
|
632
|
+
else
|
633
|
+
error { "channel success received with no pending request to handle it (bug?)" }
|
634
|
+
end
|
649
635
|
end
|
650
|
-
end
|
651
636
|
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
637
|
+
private
|
638
|
+
|
639
|
+
# Runs the SSH event loop until the remote confirmed channel open
|
640
|
+
# experimental api
|
641
|
+
def wait_until_open_confirmed
|
642
|
+
connection.loop { !remote_id }
|
643
|
+
end
|
644
|
+
|
645
|
+
LOCAL_WINDOW_SIZE_INCREMENT = 0x20000
|
646
|
+
GOOD_LOCAL_MAXIUMUM_WINDOW_SIZE = 10 * LOCAL_WINDOW_SIZE_INCREMENT
|
647
|
+
|
648
|
+
# Updates the local window size by the given amount. If the window
|
649
|
+
# size drops to less than half of the local maximum (an arbitrary
|
650
|
+
# threshold), a CHANNEL_WINDOW_ADJUST message will be sent to the
|
651
|
+
# server telling it that the window size has grown.
|
652
|
+
def update_local_window_size(size)
|
653
|
+
@local_window_size -= size
|
654
|
+
if local_window_size < local_maximum_window_size / 2
|
655
|
+
connection.send_message(
|
656
|
+
Buffer.from(:byte, CHANNEL_WINDOW_ADJUST, :long, remote_id, :long, LOCAL_WINDOW_SIZE_INCREMENT)
|
657
|
+
)
|
658
|
+
@local_window_size += LOCAL_WINDOW_SIZE_INCREMENT
|
659
|
+
|
660
|
+
if @local_maximum_window_size < @local_window_size || @local_maximum_window_size < GOOD_LOCAL_MAXIUMUM_WINDOW_SIZE
|
661
|
+
@local_maximum_window_size += LOCAL_WINDOW_SIZE_INCREMENT
|
663
662
|
end
|
664
663
|
end
|
665
|
-
|
666
|
-
|
664
|
+
end
|
665
|
+
|
666
|
+
# Gets an +Array+ of local environment variables in the remote process'
|
667
|
+
# environment.
|
668
|
+
# A variable name can either be described by a +Regexp+ or +String+.
|
669
|
+
#
|
670
|
+
# channel.forward_local_env [/^GIT_.*$/, "LANG"]
|
671
|
+
def forward_local_env(env_variable_patterns)
|
672
|
+
Array(env_variable_patterns).each do |env_variable_pattern|
|
673
|
+
matched_variables = ENV.find_all do |env_name, _|
|
674
|
+
case env_variable_pattern
|
675
|
+
when Regexp then env_name =~ env_variable_pattern
|
676
|
+
when String then env_name == env_variable_pattern
|
677
|
+
end
|
678
|
+
end
|
679
|
+
matched_variables.each do |env_name, env_value|
|
680
|
+
self.env(env_name, env_value)
|
681
|
+
end
|
667
682
|
end
|
668
683
|
end
|
684
|
+
|
685
|
+
# Set a +Hash+ of environment variables in the remote process' environment.
|
686
|
+
#
|
687
|
+
# channel.set_remote_env foo: 'bar', baz: 'whale'
|
688
|
+
def set_remote_env(env)
|
689
|
+
env.each { |key, value| self.env(key, value) }
|
690
|
+
end
|
669
691
|
end
|
692
|
+
end
|
670
693
|
end
|
671
|
-
|
672
|
-
end; end; end
|
694
|
+
end
|