winrm 2.2.3 → 2.3.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 (116) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +13 -1
  3. data/.travis.yml +10 -11
  4. data/Gemfile +2 -3
  5. data/README.md +1 -1
  6. data/Rakefile +3 -4
  7. data/appveyor.yml +1 -2
  8. data/bin/rwinrm +90 -97
  9. data/changelog.md +5 -0
  10. data/lib/winrm.rb +3 -5
  11. data/lib/winrm/connection.rb +84 -86
  12. data/lib/winrm/connection_opts.rb +90 -91
  13. data/lib/winrm/exceptions.rb +14 -2
  14. data/lib/winrm/http/response_handler.rb +127 -96
  15. data/lib/winrm/http/transport.rb +462 -427
  16. data/lib/winrm/http/transport_factory.rb +1 -5
  17. data/lib/winrm/output.rb +1 -2
  18. data/lib/winrm/psrp/fragment.rb +0 -2
  19. data/lib/winrm/psrp/message.rb +1 -3
  20. data/lib/winrm/psrp/message_data.rb +0 -2
  21. data/lib/winrm/psrp/message_data/base.rb +0 -2
  22. data/lib/winrm/psrp/message_data/error_record.rb +0 -2
  23. data/lib/winrm/psrp/message_data/pipeline_host_call.rb +0 -2
  24. data/lib/winrm/psrp/message_data/pipeline_output.rb +48 -54
  25. data/lib/winrm/psrp/message_data/pipeline_state.rb +0 -2
  26. data/lib/winrm/psrp/message_data/runspacepool_host_call.rb +0 -2
  27. data/lib/winrm/psrp/message_data/runspacepool_state.rb +0 -2
  28. data/lib/winrm/psrp/message_data/session_capability.rb +0 -2
  29. data/lib/winrm/psrp/message_defragmenter.rb +2 -2
  30. data/lib/winrm/psrp/message_factory.rb +2 -3
  31. data/lib/winrm/psrp/message_fragmenter.rb +1 -3
  32. data/lib/winrm/psrp/powershell_output_decoder.rb +0 -2
  33. data/lib/winrm/psrp/receive_response_reader.rb +3 -5
  34. data/lib/winrm/psrp/uuid.rb +1 -2
  35. data/lib/winrm/shells/base.rb +6 -4
  36. data/lib/winrm/shells/cmd.rb +63 -65
  37. data/lib/winrm/shells/power_shell.rb +207 -202
  38. data/lib/winrm/shells/retryable.rb +44 -45
  39. data/lib/winrm/shells/shell_factory.rb +0 -2
  40. data/lib/winrm/version.rb +1 -3
  41. data/lib/winrm/wsmv/base.rb +0 -2
  42. data/lib/winrm/wsmv/cleanup_command.rb +1 -2
  43. data/lib/winrm/wsmv/close_shell.rb +1 -2
  44. data/lib/winrm/wsmv/command.rb +2 -3
  45. data/lib/winrm/wsmv/command_output.rb +2 -3
  46. data/lib/winrm/wsmv/command_output_decoder.rb +1 -2
  47. data/lib/winrm/wsmv/configuration.rb +0 -2
  48. data/lib/winrm/wsmv/create_pipeline.rb +0 -2
  49. data/lib/winrm/wsmv/create_shell.rb +2 -6
  50. data/lib/winrm/wsmv/header.rb +213 -215
  51. data/lib/winrm/wsmv/init_runspace_pool.rb +96 -95
  52. data/lib/winrm/wsmv/iso8601_duration.rb +0 -2
  53. data/lib/winrm/wsmv/keep_alive.rb +0 -2
  54. data/lib/winrm/wsmv/receive_response_reader.rb +128 -126
  55. data/lib/winrm/wsmv/send_data.rb +0 -2
  56. data/lib/winrm/wsmv/soap.rb +0 -2
  57. data/lib/winrm/wsmv/wql_pull.rb +54 -56
  58. data/lib/winrm/wsmv/wql_query.rb +98 -99
  59. data/lib/winrm/wsmv/write_stdin.rb +0 -2
  60. data/tests/integration/auth_timeout_spec.rb +0 -1
  61. data/tests/integration/cmd_spec.rb +2 -3
  62. data/tests/integration/issue_59_spec.rb +0 -1
  63. data/tests/integration/powershell_spec.rb +4 -5
  64. data/tests/integration/spec_helper.rb +3 -6
  65. data/tests/integration/transport_spec.rb +0 -1
  66. data/tests/integration/wql_spec.rb +33 -34
  67. data/tests/matchers.rb +2 -3
  68. data/tests/spec/configuration_spec.rb +0 -1
  69. data/tests/spec/connection_spec.rb +0 -2
  70. data/tests/spec/exception_spec.rb +0 -1
  71. data/tests/spec/http/transport_factory_spec.rb +1 -3
  72. data/tests/spec/http/transport_spec.rb +0 -1
  73. data/tests/spec/output_spec.rb +4 -3
  74. data/tests/spec/psrp/fragment_spec.rb +0 -2
  75. data/tests/spec/psrp/message_data/base_spec.rb +0 -2
  76. data/tests/spec/psrp/message_data/error_record_spec.rb +0 -2
  77. data/tests/spec/psrp/message_data/pipeline_host_call_spec.rb +0 -2
  78. data/tests/spec/psrp/message_data/pipeline_output_spec.rb +0 -2
  79. data/tests/spec/psrp/message_data/pipeline_state_spec.rb +0 -2
  80. data/tests/spec/psrp/message_data/runspace_pool_host_call_spec.rb +0 -2
  81. data/tests/spec/psrp/message_data/runspacepool_state_spec.rb +0 -2
  82. data/tests/spec/psrp/message_data/session_capability_spec.rb +0 -2
  83. data/tests/spec/psrp/message_data_spec.rb +0 -2
  84. data/tests/spec/psrp/message_defragmenter_spec.rb +0 -2
  85. data/tests/spec/psrp/message_fragmenter_spec.rb +0 -2
  86. data/tests/spec/psrp/powershell_output_decoder_spec.rb +0 -2
  87. data/tests/spec/psrp/psrp_message_spec.rb +10 -7
  88. data/tests/spec/psrp/recieve_response_reader_spec.rb +0 -2
  89. data/tests/spec/psrp/uuid_spec.rb +2 -2
  90. data/tests/spec/response_handler_spec.rb +69 -61
  91. data/tests/spec/shells/base_spec.rb +7 -5
  92. data/tests/spec/shells/cmd_spec.rb +4 -4
  93. data/tests/spec/shells/powershell_spec.rb +221 -175
  94. data/tests/spec/spec_helper.rb +0 -1
  95. data/tests/spec/stubs/responses/get_omi_command_output_response.xml.erb +23 -0
  96. data/tests/spec/stubs/responses/get_omi_command_output_response_not_done.xml.erb +24 -0
  97. data/tests/spec/stubs/responses/get_omi_config_response.xml +45 -0
  98. data/tests/spec/stubs/responses/get_omi_powershell_keepalive_response.xml.erb +33 -0
  99. data/tests/spec/stubs/responses/open_shell_omi.xml +43 -0
  100. data/tests/spec/stubs/responses/soap_fault_omi.xml +31 -0
  101. data/tests/spec/wsmv/cleanup_command_spec.rb +0 -2
  102. data/tests/spec/wsmv/close_shell_spec.rb +0 -2
  103. data/tests/spec/wsmv/command_output_decoder_spec.rb +0 -2
  104. data/tests/spec/wsmv/command_output_spec.rb +1 -3
  105. data/tests/spec/wsmv/command_spec.rb +0 -2
  106. data/tests/spec/wsmv/configuration_spec.rb +0 -2
  107. data/tests/spec/wsmv/create_pipeline_spec.rb +2 -3
  108. data/tests/spec/wsmv/create_shell_spec.rb +6 -5
  109. data/tests/spec/wsmv/init_runspace_pool_spec.rb +38 -36
  110. data/tests/spec/wsmv/keep_alive_spec.rb +4 -4
  111. data/tests/spec/wsmv/receive_response_reader_spec.rb +124 -123
  112. data/tests/spec/wsmv/send_data_spec.rb +4 -4
  113. data/tests/spec/wsmv/wql_query_spec.rb +11 -13
  114. data/tests/spec/wsmv/write_stdin_spec.rb +0 -2
  115. data/winrm.gemspec +11 -12
  116. metadata +73 -67
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -59,9 +57,7 @@ module WinRM
59
57
  .select { |m| m.to_s.start_with?('init_') && m.to_s.end_with?('_transport') }
60
58
  .map { |tm| tm.to_s.split('_')[1] }
61
59
 
62
- unless valid.include?(transport.to_s)
63
- raise WinRM::InvalidTransportError.new(transport, valid)
64
- end
60
+ raise WinRM::InvalidTransportError.new(transport, valid) unless valid.include?(transport.to_s)
65
61
  end
66
62
  end
67
63
  end
data/lib/winrm/output.rb CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2014 Max Lincoln <max@devopsy.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -48,6 +46,7 @@ module WinRM
48
46
  # Sets the exitcode
49
47
  def exitcode=(code)
50
48
  raise WinRM::InvalidExitCode unless code.is_a? Integer
49
+
51
50
  @exitcode = code
52
51
  end
53
52
 
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
5
3
  #
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2015 Matt Wrock <matt@mattwrock.com>
4
2
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
5
3
  #
@@ -81,7 +79,7 @@ module WinRM
81
79
  pipeline_id = nil,
82
80
  destination = SERVER_DESTINATION
83
81
  )
84
- raise 'invalid message type' unless MESSAGE_TYPES.values.include?(type)
82
+ raise 'invalid message type' unless MESSAGE_TYPES.value?(type)
85
83
 
86
84
  @data = data
87
85
  @destination = destination
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the 'License');
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the 'License');
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,54 +1,48 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'rexml/document'
18
-
19
- module WinRM
20
- module PSRP
21
- module MessageData
22
- # Handles decoding a raw powershell output response
23
- class PipelineOutput < Base
24
- def output
25
- extract_out_string(remove_bom(raw.force_encoding('utf-8')))
26
- end
27
-
28
- private
29
-
30
- def extract_out_string(text)
31
- doc = REXML::Document.new(text)
32
- doc.root.get_elements('//S').map do |node|
33
- text = ''
34
- if node.text
35
- text << node.text.gsub(/_x(\h\h\h\h)_/) do
36
- decoded_text = Regexp.last_match[1].hex.chr.force_encoding('utf-8')
37
- if decoded_text.respond_to?(:scrub)
38
- decoded_text.scrub
39
- else
40
- decoded_text.encode('utf-16', invalid: :replace, undef: :replace).encode('utf-8')
41
- end
42
- end.chomp
43
- end
44
- text << "\r\n"
45
- end.join
46
- end
47
-
48
- def remove_bom(text)
49
- text.sub("\xEF\xBB\xBF", '')
50
- end
51
- end
52
- end
53
- end
54
- end
1
+ # Copyright 2016 Matt Wrock <matt@mattwrock.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'rexml/document'
16
+
17
+ module WinRM
18
+ module PSRP
19
+ module MessageData
20
+ # Handles decoding a raw powershell output response
21
+ class PipelineOutput < Base
22
+ def output
23
+ extract_out_string(remove_bom(raw.force_encoding('utf-8')))
24
+ end
25
+
26
+ private
27
+
28
+ def extract_out_string(text)
29
+ doc = REXML::Document.new(text)
30
+ doc.root.get_elements('//S').map do |node|
31
+ text = ''
32
+ if node.text
33
+ text << node.text.gsub(/(_x\h\h\h\h_)+/) do |match|
34
+ match.scan(/_x(\h\h\h\h)_/).flatten.map(&:hex)
35
+ .pack('S*').force_encoding('utf-16le').encode('utf-8')
36
+ end.chomp
37
+ end
38
+ text << "\r\n"
39
+ end.join
40
+ end
41
+
42
+ def remove_bom(text)
43
+ text.sub("\xEF\xBB\xBF", '')
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the 'License');
@@ -31,11 +29,13 @@ module WinRM
31
29
  @messages[fragment.object_id] ||= []
32
30
  @messages[fragment.object_id].push fragment
33
31
 
32
+ # rubocop:disable Style/GuardClause
34
33
  if fragment.end_fragment
35
34
  blob = []
36
35
  @messages.delete(fragment.object_id).each { |frag| blob += frag.blob }
37
36
  return message_from(blob.pack('C*'))
38
37
  end
38
+ # rubocop:enable Style/GuardClause
39
39
  end
40
40
 
41
41
  def fragment_from(byte_string)
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -65,7 +63,8 @@ module WinRM
65
63
  # @api private
66
64
  def render(template, context = nil)
67
65
  template_path = File.expand_path(
68
- "#{File.dirname(__FILE__)}/#{template}.xml.erb")
66
+ "#{File.dirname(__FILE__)}/#{template}.xml.erb"
67
+ )
69
68
  template = File.read(template_path)
70
69
  Erubis::Eruby.new(template).result(context)
71
70
  end
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the 'License');
@@ -45,7 +43,7 @@ module WinRM
45
43
  object_id,
46
44
  message.bytes[bytes_fragmented..last_byte - 1],
47
45
  fragment_id,
48
- bytes_fragmented == 0,
46
+ bytes_fragmented.zero?,
49
47
  last_byte == message_bytes.length
50
48
  )
51
49
  fragment_id += 1
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Matt Wrock <matt@mattwrock.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -41,6 +39,7 @@ module WinRM
41
39
  read_response(wsmv_message, wait_for_done_state) do |stream|
42
40
  message = defragmenter.defragment(stream[:text])
43
41
  next unless message
42
+
44
43
  if block_given?
45
44
  yield message
46
45
  else
@@ -62,6 +61,7 @@ module WinRM
62
61
  output.exitcode = exit_code if exit_code
63
62
  decoded_text = @output_decoder.decode(message)
64
63
  next unless decoded_text
64
+
65
65
  out = { stream_type(message) => decoded_text }
66
66
  output << out
67
67
  yield [out[:stdout], out[:stderr]] if block_given?
@@ -79,9 +79,7 @@ module WinRM
79
79
  when WinRM::PSRP::Message::MESSAGE_TYPES[:pipeline_host_call]
80
80
  type = :stderr if message.data.include?('WriteError')
81
81
  when WinRM::PSRP::Message::MESSAGE_TYPES[:pipeline_state]
82
- if message.parsed_data.pipeline_state == WinRM::PSRP::MessageData::PipelineState::FAILED
83
- type = :stderr
84
- end
82
+ type = :stderr if message.parsed_data.pipeline_state == WinRM::PSRP::MessageData::PipelineState::FAILED
85
83
  end
86
84
  type
87
85
  end
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,6 +30,7 @@ module WinRM
32
30
  # @return [Array<byte>] UUID in a Windows GUID compatible byte array layout.
33
31
  def uuid_to_windows_guid_bytes(uuid)
34
32
  return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] unless uuid
33
+
35
34
  b = uuid.scan(/[0-9a-fA-F]{2}/).map { |x| x.to_i(16) }
36
35
  b[0..3].reverse + b[4..5].reverse + b[6..7].reverse + b[8..15]
37
36
  end
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
1
  # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -86,6 +84,7 @@ module WinRM
86
84
  # Closes the shell if one is open
87
85
  def close
88
86
  return unless shell_id
87
+
89
88
  begin
90
89
  self.class.close_shell(connection_opts, transport, shell_id)
91
90
  rescue WinRMWSManFault => e
@@ -132,6 +131,7 @@ module WinRM
132
131
  yield shell_id, command_id
133
132
  rescue WinRMWSManFault => e
134
133
  raise unless FAULTS_FOR_RESET.include?(e.fault_code) && (tries -= 1) > 0
134
+
135
135
  reset_on_error(e)
136
136
  retry
137
137
  ensure
@@ -152,7 +152,8 @@ module WinRM
152
152
  connection_opts,
153
153
  shell_uri: shell_uri,
154
154
  shell_id: shell_id,
155
- command_id: command_id)
155
+ command_id: command_id
156
+ )
156
157
  transport.send_request(cleanup_msg.build)
157
158
  rescue WinRMWSManFault => e
158
159
  raise unless [ERROR_OPERATION_ABORTED, SHELL_NOT_FOUND].include?(e.fault_code)
@@ -174,7 +175,8 @@ module WinRM
174
175
  def add_finalizer
175
176
  ObjectSpace.define_finalizer(
176
177
  self,
177
- self.class.finalize(connection_opts, transport, shell_id))
178
+ self.class.finalize(connection_opts, transport, shell_id)
179
+ )
178
180
  end
179
181
 
180
182
  def remove_finalizer
@@ -1,65 +1,63 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require_relative 'base'
18
-
19
- module WinRM
20
- module Shells
21
- # Proxy to a remote cmd.exe shell
22
- class Cmd < Base
23
- include WinRM::WSMV::SOAP
24
- class << self
25
- def finalize(connection_opts, transport, shell_id)
26
- proc { Cmd.close_shell(connection_opts, transport, shell_id) }
27
- end
28
-
29
- def close_shell(connection_opts, transport, shell_id)
30
- msg = WinRM::WSMV::CloseShell.new(connection_opts, shell_id: shell_id)
31
- transport.send_request(msg.build)
32
- end
33
- end
34
-
35
- protected
36
-
37
- def send_command(command, arguments)
38
- cmd_msg = WinRM::WSMV::Command.new(
39
- connection_opts,
40
- shell_id: shell_id,
41
- command: command,
42
- arguments: arguments
43
- )
44
- resp_doc = transport.send_request(cmd_msg.build)
45
- command_id = REXML::XPath.first(resp_doc, "//#{NS_WIN_SHELL}:CommandId").text
46
- logger.debug("[WinRM] Command created for #{command} with id: #{command_id}")
47
- command_id
48
- end
49
-
50
- def response_reader
51
- @response_reader ||= WinRM::WSMV::ReceiveResponseReader.new(transport, logger)
52
- end
53
-
54
- def open_shell
55
- msg = WinRM::WSMV::CreateShell.new(connection_opts, shell_opts)
56
- resp_doc = transport.send_request(msg.build)
57
- REXML::XPath.first(resp_doc, "//*[@Name='ShellId']").text
58
- end
59
-
60
- def out_streams
61
- %w(stdout stderr)
62
- end
63
- end
64
- end
65
- end
1
+ # Copyright 2016 Shawn Neal <sneal@sneal.net>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require_relative 'base'
16
+
17
+ module WinRM
18
+ module Shells
19
+ # Proxy to a remote cmd.exe shell
20
+ class Cmd < Base
21
+ include WinRM::WSMV::SOAP
22
+ class << self
23
+ def finalize(connection_opts, transport, shell_id)
24
+ proc { Cmd.close_shell(connection_opts, transport, shell_id) }
25
+ end
26
+
27
+ def close_shell(connection_opts, transport, shell_id)
28
+ msg = WinRM::WSMV::CloseShell.new(connection_opts, shell_id: shell_id)
29
+ transport.send_request(msg.build)
30
+ end
31
+ end
32
+
33
+ protected
34
+
35
+ def send_command(command, arguments)
36
+ cmd_msg = WinRM::WSMV::Command.new(
37
+ connection_opts,
38
+ shell_id: shell_id,
39
+ command: command,
40
+ arguments: arguments
41
+ )
42
+ resp_doc = transport.send_request(cmd_msg.build)
43
+ command_id = REXML::XPath.first(resp_doc, "//*[local-name() = 'CommandId']").text
44
+ logger.debug("[WinRM] Command created for #{command} with id: #{command_id}")
45
+ command_id
46
+ end
47
+
48
+ def response_reader
49
+ @response_reader ||= WinRM::WSMV::ReceiveResponseReader.new(transport, logger)
50
+ end
51
+
52
+ def open_shell
53
+ msg = WinRM::WSMV::CreateShell.new(connection_opts, shell_opts)
54
+ resp_doc = transport.send_request(msg.build)
55
+ REXML::XPath.first(resp_doc, "//*[@Name='ShellId']").text
56
+ end
57
+
58
+ def out_streams
59
+ %w[stdout stderr]
60
+ end
61
+ end
62
+ end
63
+ end