Difference between revisions of "Pywinrm"
Jump to navigation
Jump to search
(Created page with "``` import winrm from decouple import config USERNAME = config('USERNAME') USERPASS = config('USERPASS') USERPASS = config('HOST') s = winrm.Session(HOST, auth=(USERNAME, USE...") |
|||
Line 3: | Line 3: | ||
from decouple import config | from decouple import config | ||
+ | # pulls from .env file | ||
USERNAME = config('USERNAME') | USERNAME = config('USERNAME') | ||
USERPASS = config('USERPASS') | USERPASS = config('USERPASS') | ||
− | + | HOST = config('HOST') | |
s = winrm.Session(HOST, auth=(USERNAME, USERPASS), transport='ntlm') | s = winrm.Session(HOST, auth=(USERNAME, USERPASS), transport='ntlm') | ||
r = s.run_cmd('ipconfig', ['/all']) | r = s.run_cmd('ipconfig', ['/all']) |
Revision as of 19:36, 11 March 2021
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)