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...") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | # Pywinrm | ||
+ | |||
+ | ## Install Latest Version in virtual environment | ||
+ | ``` | ||
+ | python3 -v venv .venv | ||
+ | . .venv/bin/activate | ||
+ | pip install pywinrm | ||
+ | ``` | ||
+ | |||
+ | .env | ||
+ | ``` | ||
+ | set -a | ||
+ | RHOST="myhostorip" | ||
+ | USERNAME="myuser@example.com" | ||
+ | USERPASS="mysecretpass" | ||
+ | ``` | ||
+ | |||
+ | source nv | ||
+ | ``` | ||
+ | . .env | ||
+ | ``` | ||
+ | |||
+ | ## Code | ||
+ | example.py | ||
+ | ``` | ||
+ | #!/usr/bin/env python3 | ||
+ | import winrm | ||
+ | import os | ||
+ | |||
+ | USERNAME = os.environ["USERNAME"] | ||
+ | USERPASS = os.environ["USERPASS"] | ||
+ | RHOST = os.environ["RHOST"] | ||
+ | s = winrm.Session(RHOST, auth=(USERNAME, USERPASS), transport='ntlm') | ||
+ | # transport='ssl' | ||
+ | ps_script=""" | ||
+ | $mstring = (hostname) | ||
+ | $certs = (ls Cert:\LocalMachine\My | Where-Object { $_.Subject -like "*$mstring*" }) | ||
+ | echo "$certs" | ||
+ | """ | ||
+ | # r = s.run_cmd('ipconfig', ['/all']) | ||
+ | r = s.run_ps(ps_script) | ||
+ | if r.status_code == 0: | ||
+ | print(r.std_out.decode()) | ||
+ | else: | ||
+ | print(r.std_err.decode()) | ||
``` | ``` | ||
+ | |||
+ | ``` | ||
+ | chmod +x example.py | ||
+ | ./example.py | ||
+ | ``` | ||
+ | |||
+ | |||
+ | |||
+ | ## More | ||
+ | |||
+ | ``` | ||
+ | |||
import winrm | import winrm | ||
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']) | ||
Line 14: | Line 72: | ||
``` | ``` | ||
− | # low-level without wrapper | + | ### low-level without wrapper |
``` | ``` | ||
from winrm.protocol import Protocol | from winrm.protocol import Protocol |
Latest revision as of 17:25, 15 December 2023
Pywinrm
Install Latest Version in virtual environment
python3 -v venv .venv . .venv/bin/activate pip install pywinrm
.env
set -a RHOST="myhostorip" USERNAME="myuser@example.com" USERPASS="mysecretpass"
source nv
. .env
Code
example.py
#!/usr/bin/env python3 import winrm import os USERNAME = os.environ["USERNAME"] USERPASS = os.environ["USERPASS"] RHOST = os.environ["RHOST"] s = winrm.Session(RHOST, auth=(USERNAME, USERPASS), transport='ntlm') # transport='ssl' ps_script=""" $mstring = (hostname) $certs = (ls Cert:\LocalMachine\My | Where-Object { $_.Subject -like "*$mstring*" }) echo "$certs" """ # r = s.run_cmd('ipconfig', ['/all']) r = s.run_ps(ps_script) if r.status_code == 0: print(r.std_out.decode()) else: print(r.std_err.decode())
chmod +x example.py ./example.py
More
<br />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)