Class: WinRM::WinRMWebService
- Inherits:
-
Object
- Object
- WinRM::WinRMWebService
- Defined in:
- lib/winrm/winrm_service.rb
Overview
This is the main class that does the SOAP request/response logic. There are a few helper classes, but pretty
much everything comes through here first.
Constant Summary collapse
- DEFAULT_TIMEOUT =
'PT60S'
- DEFAULT_MAX_ENV_SIZE =
153600
- DEFAULT_LOCALE =
'en-US'
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#cleanup_command(shell_id, command_id) ⇒ true
Clean-up after a command.
-
#close_shell(shell_id) ⇒ true
Close the shell.
-
#get_command_output(shell_id, command_id, &block) ⇒ Hash
Get the Output of the given shell and command.
-
#initialize(endpoint, transport = :kerberos, opts = {}) ⇒ WinRMWebService
constructor
A new instance of WinRMWebService.
-
#locale(locale) ⇒ Object
Set the locale.
-
#max_env_size(byte_sz) ⇒ Object
Max envelope size.
-
#open_shell(shell_opts = {}) ⇒ String
Create a Shell on the destination host.
-
#run_cmd(command, arguments = [], &block) ⇒ Hash
(also: #cmd)
Run a CMD command.
-
#run_command(shell_id, command, arguments = [], cmd_opts = {}) ⇒ String
Run a command on a machine with an open shell.
-
#run_powershell_script(script_file, &block) ⇒ Hash
(also: #powershell)
Run a Powershell script that resides on the local box.
-
#run_wql(wql) ⇒ Hash
(also: #wql)
Run a WQL Query.
-
#set_timeout(sec) ⇒ Object
(also: #op_timeout)
Operation timeout.
Constructor Details
#initialize(endpoint, transport = :kerberos, opts = {}) ⇒ WinRMWebService
Returns a new instance of WinRMWebService.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/winrm/winrm_service.rb', line 18 def initialize(endpoint, transport = :kerberos, opts = {}) @endpoint = endpoint @timeout = DEFAULT_TIMEOUT @max_env_sz = DEFAULT_MAX_ENV_SIZE @locale = DEFAULT_LOCALE case transport when :kerberos require 'gssapi' # TODO: check fo keys and throw error if missing @xfer = HTTP::HttpGSSAPI.new(endpoint, opts[:realm], opts[:service], opts[:keytab], opts) when :plaintext @xfer = HTTP::HttpPlaintext.new(endpoint, opts[:user], opts[:pass], opts) when :ssl @xfer = HTTP::HttpSSL.new(endpoint, opts[:user], opts[:pass], opts[:ca_trust_path], opts) end end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
10 11 12 |
# File 'lib/winrm/winrm_service.rb', line 10 def endpoint @endpoint end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/winrm/winrm_service.rb', line 10 def timeout @timeout end |
Instance Method Details
#cleanup_command(shell_id, command_id) ⇒ true
Clean-up after a command.
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/winrm/winrm_service.rb', line 170 def cleanup_command(shell_id, command_id) s = Savon::SOAP::XML.new s.version = 2 s.namespaces.merge!(namespaces) s.header.merge!(merge_headers(header,resource_uri_cmd,action_signal,selector_shell_id(shell_id))) # Signal the Command references to terminate (close stdout/stderr) s.input = ["#{NS_WIN_SHELL}:Signal", {'CommandId' => command_id}] s.body = { "#{NS_WIN_SHELL}:Code" => 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate' } resp = (s.to_xml) true end |
#close_shell(shell_id) ⇒ true
Close the shell
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/winrm/winrm_service.rb', line 187 def close_shell(shell_id) s = Savon::SOAP::XML.new s.version = 2 s.namespaces.merge!(namespaces) s.namespaces.merge!(Savon::SOAP::XML::SchemaTypes) s.header.merge!(merge_headers(header,resource_uri_cmd,action_delete,selector_shell_id(shell_id))) # Because Savon does not support a nil Body we have to build it ourselves. s.xml do |b| b.tag!('env:Envelope', s.namespaces) do b.tag!('env:Header') do |bh| bh << Gyoku.xml(s.header) unless s.header.empty? end if(s.input.nil?) b.tag! 'env:Body' else b.tag! 'env:Body' do |bb| bb.tag! s.input do |bbb| bbb << Gyoku.xml(s.body) if s.body end end end end end resp = (s.to_xml) true end |
#get_command_output(shell_id, command_id, &block) ⇒ Hash
Get the Output of the given shell and command
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/winrm/winrm_service.rb', line 128 def get_command_output(shell_id, command_id, &block) s = Savon::SOAP::XML.new s.version = 2 s.namespaces.merge!(namespaces) s.header.merge!(merge_headers(header,resource_uri_cmd,action_receive,selector_shell_id(shell_id))) s.input = "#{NS_WIN_SHELL}:Receive" s.body = { "#{NS_WIN_SHELL}:DesiredStream" => 'stdout stderr', :attributes! => {"#{NS_WIN_SHELL}:DesiredStream" => {'CommandId' => command_id}}} resp = (s.to_xml) output = {:data => []} (resp/"//#{NS_WIN_SHELL}:Stream").each do |n| next if n.text.nil? || n.text.empty? stream = {n['Name'].to_sym => Base64.decode64(n.text)} output[:data] << stream yield stream[:stdout], stream[:stderr] if block_given? end # We may need to get additional output if the stream has not finished. # The CommandState will change from Running to Done like so: # @example # from... # <rsp:CommandState CommandId="..." State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Running"/> # to... # <rsp:CommandState CommandId="..." State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done"> # <rsp:ExitCode>0</rsp:ExitCode> # </rsp:CommandState> if((resp/"//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']").empty?) output.merge!(get_command_output(shell_id,command_id,&block)) do |key, old_data, new_data| old_data += new_data end else output[:exitcode] = (resp/"//#{NS_WIN_SHELL}:ExitCode").text.to_i end output end |
#locale(locale) ⇒ Object
Set the locale
53 54 55 |
# File 'lib/winrm/winrm_service.rb', line 53 def locale(locale) @locale = locale end |
#max_env_size(byte_sz) ⇒ Object
Max envelope size
46 47 48 |
# File 'lib/winrm/winrm_service.rb', line 46 def max_env_size(byte_sz) @max_env_sz = byte_sz end |
#open_shell(shell_opts = {}) ⇒ String
Create a Shell on the destination host
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 |
# File 'lib/winrm/winrm_service.rb', line 65 def open_shell(shell_opts = {}) i_stream = shell_opts.has_key?(:i_stream) ? shell_opts[:i_stream] : 'stdin' o_stream = shell_opts.has_key?(:o_stream) ? shell_opts[:o_stream] : 'stdout stderr' codepage = shell_opts.has_key?(:codepage) ? shell_opts[:codepage] : 437 noprofile = shell_opts.has_key?(:noprofile) ? shell_opts[:noprofile] : 'FALSE' s = Savon::SOAP::XML.new s.version = 2 s.namespaces.merge!(namespaces) h_opts = { "#{NS_WSMAN_DMTF}:OptionSet" => { "#{NS_WSMAN_DMTF}:Option" => [noprofile, codepage], :attributes! => {"#{NS_WSMAN_DMTF}:Option" => {'Name' => ['WINRS_NOPROFILE','WINRS_CODEPAGE']}}}} s.header.merge!(merge_headers(header,resource_uri_cmd,action_create,h_opts)) s.input = "#{NS_WIN_SHELL}:Shell" s.body = { "#{NS_WIN_SHELL}:InputStreams" => i_stream, "#{NS_WIN_SHELL}:OutputStreams" => o_stream } s.body["#{NS_WIN_SHELL}:WorkingDirectory"] = shell_opts[:working_directory] if shell_opts.has_key?(:working_directory) # TODO: research Lifetime a bit more: http://msdn.microsoft.com/en-us/library/cc251546(v=PROT.13).aspx #s.body["#{NS_WIN_SHELL}:Lifetime"] = Iso8601Duration.sec_to_dur(shell_opts[:lifetime]) if(shell_opts.has_key?(:lifetime) && shell_opts[:lifetime].is_a?(Fixnum)) # @todo make it so the input is given in milliseconds and converted to xs:duration s.body["#{NS_WIN_SHELL}:IdleTimeOut"] = shell_opts[:idle_timeout] if(shell_opts.has_key?(:idle_timeout) && shell_opts[:idle_timeout].is_a?(String)) if(shell_opts.has_key?(:env_vars) && shell_opts[:env_vars].is_a?(Hash)) keys = shell_opts[:env_vars].keys vals = shell_opts[:env_vars].values s.body["#{NS_WIN_SHELL}:Environment"] = { "#{NS_WIN_SHELL}:Variable" => vals, :attributes! => {"#{NS_WIN_SHELL}:Variable" => {'Name' => keys}} } end resp = (s.to_xml) (resp/"//*[@Name='ShellId']").text end |
#run_cmd(command, arguments = [], &block) ⇒ Hash Also known as: cmd
Run a CMD command
220 221 222 223 224 225 226 227 |
# File 'lib/winrm/winrm_service.rb', line 220 def run_cmd(command, arguments = [], &block) shell_id = open_shell command_id = run_command(shell_id, command, arguments) command_output = get_command_output(shell_id, command_id, &block) cleanup_command(shell_id, command_id) close_shell(shell_id) command_output end |
#run_command(shell_id, command, arguments = [], cmd_opts = {}) ⇒ String
Run a command on a machine with an open shell
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/winrm/winrm_service.rb', line 104 def run_command(shell_id, command, arguments = [], cmd_opts = {}) consolemode = cmd_opts.has_key?(:console_mode_stdin) ? cmd_opts[:console_mode_stdin] : 'TRUE' skipcmd = cmd_opts.has_key?(:skip_cmd_shell) ? cmd_opts[:skip_cmd_shell] : 'FALSE' s = Savon::SOAP::XML.new s.version = 2 s.namespaces.merge!(namespaces) h_opts = { "#{NS_WSMAN_DMTF}:OptionSet" => { "#{NS_WSMAN_DMTF}:Option" => [consolemode, skipcmd], :attributes! => {"#{NS_WSMAN_DMTF}:Option" => {'Name' => ['WINRS_CONSOLEMODE_STDIN','WINRS_SKIP_CMD_SHELL']}}} } s.header.merge!(merge_headers(header,resource_uri_cmd,action_command,h_opts,selector_shell_id(shell_id))) s.input = "#{NS_WIN_SHELL}:CommandLine" s.body = { "#{NS_WIN_SHELL}:Command" => "\"#{command}\"", "#{NS_WIN_SHELL}:Arguments" => arguments} resp = (s.to_xml) (resp/"//#{NS_WIN_SHELL}:CommandId").text end |
#run_powershell_script(script_file, &block) ⇒ Hash Also known as: powershell
Run a Powershell script that resides on the local box.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/winrm/winrm_service.rb', line 234 def run_powershell_script(script_file, &block) # if an IO object is passed read it..otherwise assume the contents of the file were passed script = script_file.kind_of?(IO) ? script_file.read : script_file script = script.chars.to_a.join("\x00").chomp if(defined?(script.encode)) script = script.encode('ASCII-8BIT') script = Base64.strict_encode64(script) else script = Base64.encode64(script).chomp end shell_id = open_shell command_id = run_command(shell_id, "powershell -encodedCommand #{script}") command_output = get_command_output(shell_id, command_id, &block) cleanup_command(shell_id, command_id) close_shell(shell_id) command_output end |
#run_wql(wql) ⇒ Hash Also known as: wql
Run a WQL Query
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/winrm/winrm_service.rb', line 260 def run_wql(wql) s = Savon::SOAP::XML.new s.version = 2 s.namespaces.merge!(namespaces) s.header.merge!(merge_headers(header,resource_uri_wmi,action_enumerate)) s.input = "#{NS_ENUM}:Enumerate" s.body = { "#{NS_WSMAN_DMTF}:OptimizeEnumeration" => nil, "#{NS_WSMAN_DMTF}:MaxElements" => '32000', "#{NS_WSMAN_DMTF}:Filter" => wql, :attributes! => { "#{NS_WSMAN_DMTF}:Filter" => {'Dialect' => 'http://schemas.microsoft.com/wbem/wsman/1/WQL'}} } resp = (s.to_xml) hresp = Savon::SOAP::XML.to_hash resp.to_xml # Normalize items so the type always has an array even if it's just a single item. items = {} hresp[:enumerate_response][:items].each_pair do |k,v| if v.is_a?(Array) items[k] = v else items[k] = [v] end end items end |
#set_timeout(sec) ⇒ Object Also known as: op_timeout
Operation timeout
38 39 40 |
# File 'lib/winrm/winrm_service.rb', line 38 def set_timeout(sec) @timeout = Iso8601Duration.sec_to_dur(sec) end |