Completed
Pull Request — master (#499)
by Anthony
02:47
created

WinRMCmdAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 19 2
1
from winrm.protocol import Protocol
2
3
from st2actions.runners.pythonrunner import Action
4
5
__all__ = [
6
    'WinRMCmdAction'
7
]
8
9
10
class WinRMCmdAction(Action):
11
    def run(self, host, password, command, params, username='Administrator',
12
            port=5732, secure=True):
13
        proto = 'https' if secure else 'http'
14
        p = Protocol(
15
            endpoint='%s://%s:%i/wsman' % (proto, host, port),  # RFC 2732?
16
            transport='ntlm',
17
            username=username,
18
            password=password,
19
            server_cert_validation='ignore')
20
        shell_id = p.open_shell()
21
22
        # run the command
23
        command_id = p.run_command(shell_id, command, params)
24
        std_out, std_err, status_code = p.get_command_output(shell_id,
25
                                                             command_id)
26
        p.cleanup_command(shell_id, command_id)
27
28
        p.close_shell(shell_id)
29
        return {'stdout': std_out, 'stderr': std_err}
30