Skip to main content

Python library for Windows Remote Management

Project description

pywinrm is a Python client for the Windows Remote Management (WinRM) service. It allows you to invoke commands on target Windows machines from any machine that can run Python.

WinRM allows you to perform various management tasks remotely. These include, but are not limited to: running batch scripts, powershell scripts, and fetching WMI variables.

For more information on WinRM, please visit Microsoft’s WinRM site.

Requirements

  • Linux, Mac OS X or Windows

  • CPython 2.6-2.7, 3.2-3.5 or PyPy 1.9

  • requests-kerberos is optional

Installation

To install pywinrm with support for basic, certificate, and NTLM auth, simply

$ pip install pywinrm

To use Kerberos authentication you need these optional dependencies

# for Debian/Ubuntu/etc:
$ sudo apt-get install python-dev libkrb5-dev
$ pip install pywinrm[kerberos]

# for RHEL/CentOS/etc:
$ sudo yum install gcc krb5-devel krb5-workstation
$ pip install pywinrm[kerberos]

Example Usage

Run a process on a remote host

import winrm

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

NOTE: pywinrm will try and guess the correct endpoint url from the following formats:

Run Powershell script on remote host

import winrm

ps_script = """$strComputer = $Host
Clear
$RAM = WmiObject Win32_ComputerSystem
$MB = 1048576

"Installed Memory: " + [int]($RAM.TotalPhysicalMemory /$MB) + " MB" """

s = winrm.Session('windows-host.example.com', auth=('john.smith', 'secret'))
r = s.run_ps(ps_script)
>>> r.status_code
0
>>> r.std_out
Installed Memory: 3840 MB

>>> r.std_err

Powershell scripts will be base64 UTF16 little-endian encoded prior to sending to the Windows host. Error messages are converted from the Powershell CLIXML format to a human readable format as a convenience.

Run process with low-level API with domain user, disabling HTTPS cert validation

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)

Enabling WinRM on remote host

Enable WinRM over HTTP and HTTPS with self-signed certificate (includes firewall rules):

# from powershell:
Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1'))

Enable WinRM over HTTP for test usage (includes firewall rules):

winrm quickconfig

Enable WinRM basic authentication. For domain users, it is necessary to use NTLM or Kerberos authentication (both are enabled by default).

# from cmd:
winrm set winrm/config/service/auth @{Basic="true"}

Allow unencrypted message passing over WinRM (recommended only for troubleshooting and internal use)

# from cmd:
winrm set winrm/config/service @{AllowUnencrypted="true"}

Contributors (alphabetically)

  • Reina Abolofia

  • Lukas Bednar

  • Chris Church

  • Matt Clark

  • Nir Cohen

  • David Cournapeau

  • Matt Davis

  • Patrick Dunnigan

  • Gema Gomez

  • Juan J. Martinez

  • Alessandro Pilotti

  • Manuel Sabban

  • Jijo Varghese

Want to help - send a pull request. I will accept good pull requests for sure.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywinrm-0.2.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

pywinrm-0.2.0-py2.py3-none-any.whl (24.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pywinrm-0.2.0.tar.gz.

File metadata

  • Download URL: pywinrm-0.2.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pywinrm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 be059a6e35aba9a632950773386d334fc60bb22dc5b1945f96c1d9b1dd005ce4
MD5 4d587b0808268553a71eacfe75fd8d76
BLAKE2b-256 c739d2a72a0847727f2db84ec85550a48e46c69cd1abb4888950671563065f54

See more details on using hashes here.

Provenance

File details

Details for the file pywinrm-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pywinrm-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c7aca1b88d80b776b91adc7a3e2eb520ba43e33d6c9e92d4e1753ed145981178
MD5 68fd7724eec63c14762289ccef911756
BLAKE2b-256 e6b548b986333cc56226b77965420b07f98dcd5d298d3c5fdf60a14374b0defe

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page