winrm 2.2.3 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 46a0806843f652f3d0b8d8858eb87cf0365d8e4d
4
- data.tar.gz: 76f409feeda643970222972be8c571e2905a6fb9
2
+ SHA256:
3
+ metadata.gz: cd8c4887729128df19666c0a4f2b099b088261f5cb60ecb1b2adf52810188710
4
+ data.tar.gz: 8711a914b783a6f6a6a5f5f8eb15ad74d4798dcf29242ff4b3b59679858e0c9e
5
5
  SHA512:
6
- metadata.gz: cd17e956aa071c63ab65c0a9db69bada2a256b735ee4c9e2b63b32a16920357749bc814b32b15dd3ad61e777d77144fa73eea1e851c23347e0486ded69ffd47b
7
- data.tar.gz: 119346b0543a18a8715f90b2c318df0bf98cbdeeae16d8afd7ffd70f73c6690b620e8c94d541e4a0798dc490c395f5e4feee7d12c8e83fddf64970560781602d
6
+ metadata.gz: 87eb8b34f189b70da6d6fd21e70142892cd9a8485f952a649229fb8cde0f073796af6ed9eb904e6c6b2f6d270ee901ec3d8ad69c36cd575e0b8def04faf5f9c9
7
+ data.tar.gz: eaf8d672f6f53d62029d41452cfeda2f1c9fba0164528ce057376b1ceb8805b5deb5dfd76d320c4f4eac695c66f3ac517460fbe72ca7de1b0a899bacb7cf9c91
data/.rubocop.yml CHANGED
@@ -10,8 +10,17 @@ Style/Encoding:
10
10
  Style/NumericLiterals:
11
11
  Enabled: false
12
12
 
13
+ Layout/EndOfLine:
14
+ Enabled: false
15
+
16
+ Layout/IndentHeredoc:
17
+ Enabled: false
18
+
19
+ Metrics/BlockLength:
20
+ Max: 200
21
+
13
22
  Metrics/LineLength:
14
- Max: 100
23
+ Max: 120
15
24
 
16
25
  Metrics/MethodLength:
17
26
  Max: 20
@@ -19,6 +28,9 @@ Metrics/MethodLength:
19
28
  ModuleLength:
20
29
  Max: 250
21
30
 
31
+ Naming/HeredocDelimiterNaming:
32
+ Enabled: false
33
+
22
34
  ClassLength:
23
35
  Max: 250
24
36
 
data/.travis.yml CHANGED
@@ -1,11 +1,10 @@
1
- language: ruby
2
- rvm:
3
- - 2.0.0
4
- - 2.1.0
5
- before_install:
6
- - gem update bundler
7
-
8
- # This prevents testing branches that are created just for PRs
9
- branches:
10
- only:
11
- - master
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install:
5
+ - gem update bundler
6
+
7
+ # This prevents testing branches that are created just for PRs
8
+ branches:
9
+ only:
10
+ - master
data/Gemfile CHANGED
@@ -1,3 +1,2 @@
1
- # encoding: UTF-8
2
- source 'https://rubygems.org'
3
- gemspec
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md CHANGED
@@ -72,7 +72,7 @@ The `:negotiate` transport uses the [rubyntlm gem](https://github.com/WinRb/ruby
72
72
  ### `:ssl`
73
73
  ```ruby
74
74
  WinRM::Connection.new(
75
- endpoint: 'https://myhost:59856/wsman',
75
+ endpoint: 'https://myhost:5986/wsman',
76
76
  transport: :ssl,
77
77
  user: 'administrator',
78
78
  password: 'Pass@word1'
data/Rakefile CHANGED
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'rubygems'
3
2
  require 'bundler/setup'
4
3
  require 'rspec/core/rake_task'
@@ -6,7 +5,7 @@ require 'rubocop/rake_task'
6
5
  require 'bundler/gem_tasks'
7
6
 
8
7
  # Change to the directory of this file.
9
- Dir.chdir(File.expand_path('../', __FILE__))
8
+ Dir.chdir(File.expand_path(__dir__))
10
9
 
11
10
  desc 'Open a Pry console for this library'
12
11
  task :console do
@@ -29,6 +28,6 @@ end
29
28
 
30
29
  RuboCop::RakeTask.new
31
30
 
32
- task default: [:spec, :rubocop]
31
+ task default: %i[spec rubocop]
33
32
 
34
- task all: [:default, :integration]
33
+ task all: %i[default integration]
data/appveyor.yml CHANGED
@@ -11,7 +11,7 @@ environment:
11
11
  user_key: c:\projects\winrm\key.pem
12
12
 
13
13
  matrix:
14
- - ruby_version: "21"
14
+ - ruby_version: "22"
15
15
  winrm_endpoint: http://localhost:5985/wsman
16
16
 
17
17
  clone_folder: c:\projects\winrm
@@ -40,7 +40,6 @@ install:
40
40
  - ps: Write-Host $env:PATH
41
41
  - ps: ruby --version
42
42
  - ps: gem --version
43
- - ps: gem install bundler --quiet --no-ri --no-rdoc
44
43
  - ps: bundler --version
45
44
 
46
45
  build_script:
data/bin/rwinrm CHANGED
@@ -1,97 +1,90 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Copyright 2014 Shawn Neal <sneal@sneal.net>
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
-
18
- # rubocop:disable all
19
-
20
- $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
21
-
22
- require 'readline'
23
- require 'io/console'
24
- require 'winrm'
25
-
26
- def help_msg
27
- puts 'Usage: rwinrm user@host'
28
- puts ''
29
- end
30
-
31
- def parse_options
32
- options = {}
33
- fail 'Missing required options' unless ARGV.length == 1
34
-
35
- m = /^(?<user>[a-z0-9\.\!\$ _-]+)@{1}(?<host>[a-z0-9\.\-]+)(?<port>:[0-9]+)?/i.match(ARGV[0])
36
- fail "#{ARGV[0]} is an invalid host" unless m
37
- options[:user] = m[:user]
38
- options[:endpoint] = "http://#{m[:host]}#{m[:port] || ':5985'}/wsman"
39
-
40
- # Get the password
41
- print 'Password: '
42
- options[:pass] = STDIN.noecho(&:gets).chomp
43
- puts
44
-
45
- # Set some defaults required by WinRM WS
46
- options[:auth_type] = :plaintext
47
- options[:basic_auth_only] = true
48
-
49
- options
50
- rescue StandardError => e
51
- puts e.message
52
- help_msg
53
- exit 1
54
- end
55
-
56
- def repl(options)
57
- client = WinRM::WinRMWebService.new(
58
- options[:endpoint],
59
- options[:auth_type].to_sym,
60
- options)
61
-
62
- client.set_timeout(3600)
63
- shell_id = client.open_shell
64
- command_id = client.run_command(shell_id, 'cmd', "/K prompt [#{ARGV[0]}]$P$G")
65
-
66
- read_thread = Thread.new do
67
- client.get_command_output(shell_id, command_id) do |stdout, stderr|
68
- STDOUT.write stdout
69
- STDERR.write stderr
70
- end
71
- end
72
- read_thread.abort_on_exception = true
73
-
74
- while (buf = Readline.readline('', true))
75
- if buf =~ /^exit/
76
- read_thread.exit
77
- client.cleanup_command(shell_id, command_id)
78
- client.close_shell(shell_id)
79
- exit 0
80
- else
81
- client.write_stdin(shell_id, command_id, "#{buf}\r\n")
82
- end
83
- end
84
- rescue Interrupt
85
- puts 'exiting'
86
- # ctrl-c
87
- rescue WinRM::WinRMAuthorizationError
88
- puts 'Authentication failed, bad user name or password'
89
- exit 1
90
- rescue StandardError => e
91
- puts e.message
92
- exit 1
93
- end
94
-
95
- repl(parse_options)
96
-
97
- # rubocop:enable all
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright 2014 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
+ # rubocop:disable all
18
+
19
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
20
+
21
+ require 'readline'
22
+ require 'io/console'
23
+ require 'winrm'
24
+
25
+ def help_msg
26
+ puts 'Usage: rwinrm user@host'
27
+ puts ''
28
+ end
29
+
30
+ def parse_options
31
+ options = {}
32
+ fail 'Missing required options' unless ARGV.length == 1
33
+
34
+ m = /^(?<user>[a-z0-9\.\!\$ _-]+)@{1}(?<host>[a-z0-9\.\-]+)(?<port>:[0-9]+)?/i.match(ARGV[0])
35
+ fail "#{ARGV[0]} is an invalid host" unless m
36
+ options[:user] = m[:user]
37
+ options[:endpoint] = "http://#{m[:host]}#{m[:port] || ':5985'}/wsman"
38
+
39
+ # Get the password
40
+ print 'Password: '
41
+ options[:password] = STDIN.noecho(&:gets).chomp
42
+ puts
43
+
44
+ # Set some defaults required by WinRM WS
45
+ options[:transport] = :plaintext
46
+ options[:basic_auth_only] = true
47
+ options[:operation_timeout] = 3600
48
+
49
+ options
50
+ rescue StandardError => e
51
+ puts e.message
52
+ help_msg
53
+ exit 1
54
+ end
55
+
56
+ def repl(options)
57
+ shell = nil
58
+
59
+ client = WinRM::Connection.new(options)
60
+ shell = client.shell(:powershell)
61
+ prompt = "PS #{ARGV[0]}> "
62
+
63
+ while (buf = Readline.readline(prompt, true))
64
+ if buf =~ /^exit/
65
+ shell.close
66
+ shell = nil
67
+ exit 0
68
+ else
69
+ shell.run(buf) do |stdout, stderr|
70
+ $stdout.write stdout
71
+ $stderr.write stderr
72
+ end
73
+ end
74
+ end
75
+ rescue Interrupt
76
+ puts 'exiting'
77
+ # ctrl-c
78
+ rescue WinRM::WinRMAuthorizationError
79
+ puts 'Authentication failed, bad user name or password'
80
+ exit 1
81
+ rescue StandardError => e
82
+ puts e.message
83
+ exit 1
84
+ ensure
85
+ shell.close unless shell.nil?
86
+ end
87
+
88
+ repl(parse_options)
89
+
90
+ # rubocop:enable all
data/changelog.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # WinRM Gem Changelog
2
2
 
3
+ # 2.3.0
4
+ - Fixes rwinrm command
5
+ - Use pack('S*') instead of .chr so full utf-16 can be supported
6
+ - Omi Server compatibility fixes
7
+
3
8
  # 2.2.3
4
9
  - Revert change made in 2.2.2 that retries network errors in Close and cleanup
5
10
 
data/lib/winrm.rb CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
- #
3
1
  # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
2
  #
5
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,9 +28,9 @@ module WinRM
30
28
  Logging.logger.root.appenders = Logging.appenders.stderr
31
29
  rescue ArgumentError
32
30
  # This means that the logging level wasn't valid
33
- $stderr.puts "Invalid WINRM_LOG level is set: #{ENV['WINRM_LOG']}"
34
- $stderr.puts ''
35
- $stderr.puts 'Please use one of the standard log levels: ' \
31
+ warn "Invalid WINRM_LOG level is set: #{ENV['WINRM_LOG']}"
32
+ warn ''
33
+ warn 'Please use one of the standard log levels: ' \
36
34
  'debug, info, warn, or error'
37
35
  end
38
36
  end
@@ -1,86 +1,84 @@
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 'connection_opts'
18
- require_relative 'http/transport_factory'
19
- require_relative 'shells/shell_factory'
20
- require_relative 'wsmv/wql_query'
21
- require_relative 'wsmv/wql_pull'
22
-
23
- module WinRM
24
- # WinRM connection used to establish a session with the remote WinRM service.
25
- class Connection
26
- # Creates a new WinRM connection
27
- # See the ConnectionOpts class for connection options.
28
- def initialize(connection_opts)
29
- configure_connection_opts(connection_opts)
30
- configure_logger
31
- end
32
-
33
- attr_accessor :logger
34
-
35
- # Creates a new shell on the remote Windows server associated with
36
- # this connection.
37
- # @param shell_type [Symbol] The shell type :cmd or :powershell
38
- # @param shell_opts [Hash] Options targeted for the created shell
39
- # @return [Shell] PowerShell or Cmd shell instance.
40
- def shell(shell_type, shell_opts = {})
41
- shell = shell_factory.create_shell(shell_type, shell_opts)
42
- if block_given?
43
- begin
44
- yield shell
45
- ensure
46
- shell.close
47
- end
48
- else
49
- shell
50
- end
51
- end
52
-
53
- # Executes a WQL query against the WinRM connection
54
- # @param wql [String] The wql query
55
- # @param namespace [String] namespace for query - default is root/cimv2/*
56
- # @return [Hash] Hash representation of wql query response (Hash is empty if a block is given)
57
- # @yeild [type, item] Yields the time name and item for every item
58
- def run_wql(wql, namespace = 'root/cimv2/*', &block)
59
- query = WinRM::WSMV::WqlQuery.new(transport, @connection_opts, wql, namespace)
60
- query.process_response(transport.send_request(query.build), &block)
61
- end
62
-
63
- private
64
-
65
- def configure_connection_opts(connection_opts)
66
- @connection_opts = ConnectionOpts.create_with_defaults(connection_opts)
67
- end
68
-
69
- def configure_logger
70
- @logger = Logging.logger[self]
71
- logger.level = :warn
72
- logger.add_appenders(Logging.appenders.stdout)
73
- end
74
-
75
- def shell_factory
76
- @shell_factory ||= WinRM::Shells::ShellFactory.new(@connection_opts, transport, logger)
77
- end
78
-
79
- def transport
80
- @transport ||= begin
81
- transport_factory = WinRM::HTTP::TransportFactory.new
82
- transport_factory.create_transport(@connection_opts)
83
- end
84
- end
85
- end
86
- 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 'connection_opts'
16
+ require_relative 'http/transport_factory'
17
+ require_relative 'shells/shell_factory'
18
+ require_relative 'wsmv/wql_query'
19
+ require_relative 'wsmv/wql_pull'
20
+
21
+ module WinRM
22
+ # WinRM connection used to establish a session with the remote WinRM service.
23
+ class Connection
24
+ # Creates a new WinRM connection
25
+ # See the ConnectionOpts class for connection options.
26
+ def initialize(connection_opts)
27
+ configure_connection_opts(connection_opts)
28
+ configure_logger
29
+ end
30
+
31
+ attr_accessor :logger
32
+
33
+ # Creates a new shell on the remote Windows server associated with
34
+ # this connection.
35
+ # @param shell_type [Symbol] The shell type :cmd or :powershell
36
+ # @param shell_opts [Hash] Options targeted for the created shell
37
+ # @return [Shell] PowerShell or Cmd shell instance.
38
+ def shell(shell_type, shell_opts = {})
39
+ shell = shell_factory.create_shell(shell_type, shell_opts)
40
+ if block_given?
41
+ begin
42
+ yield shell
43
+ ensure
44
+ shell.close
45
+ end
46
+ else
47
+ shell
48
+ end
49
+ end
50
+
51
+ # Executes a WQL query against the WinRM connection
52
+ # @param wql [String] The wql query
53
+ # @param namespace [String] namespace for query - default is root/cimv2/*
54
+ # @return [Hash] Hash representation of wql query response (Hash is empty if a block is given)
55
+ # @yeild [type, item] Yields the time name and item for every item
56
+ def run_wql(wql, namespace = 'root/cimv2/*', &block)
57
+ query = WinRM::WSMV::WqlQuery.new(transport, @connection_opts, wql, namespace)
58
+ query.process_response(transport.send_request(query.build), &block)
59
+ end
60
+
61
+ private
62
+
63
+ def configure_connection_opts(connection_opts)
64
+ @connection_opts = ConnectionOpts.create_with_defaults(connection_opts)
65
+ end
66
+
67
+ def configure_logger
68
+ @logger = Logging.logger[self]
69
+ logger.level = :warn
70
+ logger.add_appenders(Logging.appenders.stdout)
71
+ end
72
+
73
+ def shell_factory
74
+ @shell_factory ||= WinRM::Shells::ShellFactory.new(@connection_opts, transport, logger)
75
+ end
76
+
77
+ def transport
78
+ @transport ||= begin
79
+ transport_factory = WinRM::HTTP::TransportFactory.new
80
+ transport_factory.create_transport(@connection_opts)
81
+ end
82
+ end
83
+ end
84
+ end