« Python : Interactions avec Windows » : différence entre les versions
(Page créée avec « = Le module Pywinrm = https://pypi.org/project/pywinrm/0.2.2/ Le module Pywinrm est un module pour Python permettant d'interagir avec Windows par l'intermédiaire du prot... ») |
Aucun résumé des modifications |
||
(Une version intermédiaire par la même utilisatrice non affichée) | |||
Ligne 1 : | Ligne 1 : | ||
[[Category:prog]] | |||
[[Category:python]] | |||
[[Category:windows]] | |||
= Le module Pywinrm = | = Le module Pywinrm = | ||
https://pypi.org/project/pywinrm/0.2.2/ | https://pypi.org/project/pywinrm/0.2.2/ | ||
Ligne 4 : | Ligne 7 : | ||
Le module Pywinrm est un module pour Python permettant d'interagir avec Windows par l'intermédiaire du protocole WinRM. | Le module Pywinrm est un module pour Python permettant d'interagir avec Windows par l'intermédiaire du protocole WinRM. | ||
== Préparation sur la machine Windows = | = Mode Unencrypted = | ||
Par défaut, le plus simple semble être le mode unencrypted. Cependant, cela implique d'autoriser ça sur la machine Windows, et puis ce n'est PAS SECURISE | |||
== Préparation sur la machine Windows == | |||
En admettant que la machine Windows ait déjà WinRM (sinon, voir [[Ansible et Windows]], il peut être nécessaire d'utiliser la commande suivante dans le CMD de la machine : | En admettant que la machine Windows ait déjà WinRM (sinon, voir [[Ansible et Windows]], il peut être nécessaire d'utiliser la commande suivante dans le CMD de la machine : | ||
winrm set winrm/config/service @{AllowUnencrypted="true"} | winrm set winrm/config/service @{AllowUnencrypted="true"} | ||
== Utilisation sur Python == | === Utilisation sur Python === | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Ligne 27 : | Ligne 33 : | ||
>>> r.std_err | >>> r.std_err | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Avec du chiffrement = | |||
Voilà ce que donne la même chose avec du chiffrement ntlm: | |||
<syntaxhighlight lang='python'> | |||
#!/usr/bin/env python3 | |||
#coding: utf-8 | |||
import winrm | |||
from winrm.protocol import Protocol | |||
from getpass import getpass | |||
#!---------- winrmtest ---------- | |||
#Description | |||
#--------------------------------! | |||
#----------! FUNC | |||
#----------! MAIN | |||
#Configuration de l'auth | |||
p = Protocol( | |||
endpoint='https://hotewindows:5986/wsman', | |||
transport="ntlm", #basic, plaintext, certificate, ssl, kerberos, ntlm, credssp | |||
username=r"domaine.local\adm-ripley", | |||
password=getpass(prompt="Pass ? > "), | |||
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) | |||
print(std_out, std_err) | |||
p.cleanup_command(shell_id, command_id) | |||
p.close_shell(shell_id) | |||
</syntaxhighlight> | |||
On voit bien la difference. En l'occurence, j'utilise ntlm; mais le paramètre "transport" permet d'autres modes. | |||
Sur la machine hôte, une énumération des listeners donne : | |||
<nowiki> | |||
winrm enumerate winrm/config/Listener | |||
Listener | |||
Address = * | |||
Transport = HTTP | |||
Port = 5985 | |||
Hostname | |||
Enabled = true | |||
URLPrefix = wsman | |||
CertificateThumbprint | |||
ListeningOn = 127.0.0.1, #IPs | |||
Listener | |||
Address = * | |||
Transport = HTTPS | |||
Port = 5986 | |||
Hostname = #Un hostname :) | |||
Enabled = true | |||
URLPrefix = wsman | |||
CertificateThumbprint = #Un long thumbprint :) | |||
ListeningOn = 127.0.0.1, #Des IPs :) | |||
</nowiki> |
Dernière version du 22 juin 2022 à 12:05
Le module Pywinrm
https://pypi.org/project/pywinrm/0.2.2/
Le module Pywinrm est un module pour Python permettant d'interagir avec Windows par l'intermédiaire du protocole WinRM.
Mode Unencrypted
Par défaut, le plus simple semble être le mode unencrypted. Cependant, cela implique d'autoriser ça sur la machine Windows, et puis ce n'est PAS SECURISE
Préparation sur la machine Windows
En admettant que la machine Windows ait déjà WinRM (sinon, voir Ansible et Windows, il peut être nécessaire d'utiliser la commande suivante dans le CMD de la machine :
winrm set winrm/config/service @{AllowUnencrypted="true"}
Utilisation sur Python
<syntaxhighlight lang="python"> s = winrm.Session('windows-host.example.com', auth=('john.smith', 'secret')) r = s.run_cmd('ipconfig', ['/all']) >>> r.status_code 0 >>> r.std_out Windows IP Configuration
Host Name . . . . . . . . . . . . : WINDOWS-HOST Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Hybrid IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No
... >>> r.std_err </syntaxhighlight>
Avec du chiffrement
Voilà ce que donne la même chose avec du chiffrement ntlm:
<syntaxhighlight lang='python'>
- !/usr/bin/env python3
- coding: utf-8
import winrm from winrm.protocol import Protocol from getpass import getpass
- !---------- winrmtest ----------
- Description
- --------------------------------!
- ----------! FUNC
- ----------! MAIN
- Configuration de l'auth
p = Protocol(
endpoint='https://hotewindows:5986/wsman', transport="ntlm", #basic, plaintext, certificate, ssl, kerberos, ntlm, credssp username=r"domaine.local\adm-ripley", password=getpass(prompt="Pass ? > "), 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) print(std_out, std_err) p.cleanup_command(shell_id, command_id) p.close_shell(shell_id) </syntaxhighlight>
On voit bien la difference. En l'occurence, j'utilise ntlm; mais le paramètre "transport" permet d'autres modes. Sur la machine hôte, une énumération des listeners donne :
winrm enumerate winrm/config/Listener Listener Address = * Transport = HTTP Port = 5985 Hostname Enabled = true URLPrefix = wsman CertificateThumbprint ListeningOn = 127.0.0.1, #IPs Listener Address = * Transport = HTTPS Port = 5986 Hostname = #Un hostname :) Enabled = true URLPrefix = wsman CertificateThumbprint = #Un long thumbprint :) ListeningOn = 127.0.0.1, #Des IPs :)