Pywinrm

From UVOO Tech Wiki
Revision as of 19:36, 11 March 2021 by Busk (talk | contribs)
Jump to navigation Jump to search
import winrm
from decouple import config

# pulls from .env file
USERNAME = config('USERNAME')
USERPASS = config('USERPASS')
HOST = config('HOST')
s = winrm.Session(HOST, auth=(USERNAME, USERPASS), transport='ntlm')
r = s.run_cmd('ipconfig', ['/all'])
if r.status_code == 0:
    print(r.std_out.decode())
else:
    print(r.std_err.decode())

low-level without wrapper

from winrm.protocol import Protocol

p = Protocol(
    endpoint='https://windows-host:5986/wsman',
    transport='ntlm',
    username=r'somedomain\someuser',
    password='secret',
    server_cert_validation='ignore')
shell_id = p.open_shell()
command_id = p.run_command(shell_id, 'ipconfig', ['/all'])
std_out, std_err, status_code = p.get_command_output(shell_id, command_id)
p.cleanup_command(shell_id, command_id)
p.close_shell(shell_id)