-
Notifications
You must be signed in to change notification settings - Fork 118
/
rwinrm
executable file
·90 lines (75 loc) · 2.11 KB
/
rwinrm
1
2
3
4
5
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
#!/usr/bin/env ruby
# Copyright 2014 Shawn Neal <sneal@sneal.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# rubocop:disable all
$LOAD_PATH.push File.expand_path('../../lib', __FILE__)
require 'readline'
require 'io/console'
require 'winrm'
def help_msg
puts 'Usage: rwinrm user@host'
puts ''
end
def parse_options
options = {}
fail 'Missing required options' unless ARGV.length == 1
m = /^(?<user>[a-z0-9\.\!\$ _-]+)@{1}(?<host>[a-z0-9\.\-]+)(?<port>:[0-9]+)?/i.match(ARGV[0])
fail "#{ARGV[0]} is an invalid host" unless m
options[:user] = m[:user]
options[:endpoint] = "http://#{m[:host]}#{m[:port] || ':5985'}/wsman"
# Get the password
print 'Password: '
options[:password] = STDIN.noecho(&:gets).chomp
puts
# Set some defaults required by WinRM WS
options[:transport] = :plaintext
options[:basic_auth_only] = true
options[:operation_timeout] = 3600
options
rescue StandardError => e
puts e.message
help_msg
exit 1
end
def repl(options)
shell = nil
client = WinRM::Connection.new(options)
shell = client.shell(:powershell)
prompt = "PS #{ARGV[0]}> "
while (buf = Readline.readline(prompt, true))
if buf =~ /^exit/
shell.close
shell = nil
exit 0
else
shell.run(buf) do |stdout, stderr|
$stdout.write stdout
$stderr.write stderr
end
end
end
rescue Interrupt
puts 'exiting'
# ctrl-c
rescue WinRM::WinRMAuthorizationError
puts 'Authentication failed, bad user name or password'
exit 1
rescue StandardError => e
puts e.message
exit 1
ensure
shell.close unless shell.nil?
end
repl(parse_options)
# rubocop:enable all