How to use Python WinRM on Linux to Query Windows Hosts

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

In this article, we will explore how to use Python WinRM on Linux to query Windows hosts. We will also cover installation of the required components and learning some basics before going into more advanced techniques.

The “python winrm copy file” is a command-line tool that allows users to query Windows hosts. The user must have Python installed on their computer in order to use the tool.

How to use Python WinRM on Linux to Query Windows Hosts

When PowerShell v2 was released, PowerShell Remoting was a big time saving. Despite the addition of SSH functionality in PowerShell Core, it is still feasible to connect to Linux systems through WinRM from Windows hosts using Python. In this blog article, you’ll learn how to connect to Windows hosts using Python and WinRM on Linux.

If you want to learn how to set up OpenSSH on Windows, have a look at this blog article.

When connecting from Linux to Windows via WinRM, you have two options:

  • HTTPS is the secure version of HTTP (certificate-based)
  • HTTP (Hypertext Transfer Protocol) (unencrypted, basic authentication)

If you’re setting up a lab environment or perhaps doing some basic testing, connecting over HTTP is fine but if setting up Linux -> Windows connections, you should opt for certificate-based communication. In this post, you will learn how to set up connectivity using basic authentication (HTTP).

If you’d want to learn more about how to set up WinRM utilizing certificates, have a look at this blog post. But be warned: it’s a long and painful procedure!

Six steps are required to connect a Linux server to Windows via WinRM using Python and simple authentication.

  1. On the Windows host, enable PowerShell Remoting.
  2. On the Windows host, unencrypted (HTTP) communication is allowed.
  3. On the Windows host, basic authentication is enabled.
  4. The pywinrm Python package is being installed.
  5. Python import of the winrm library.
  6. Using the winrm package to create a WinRM session.

On Windows, Enabling PowerShell Remoting

Most current Windows operating systems already have PowerShell Remoting enabled. If not, all that is necessary is to execute Enable-PSRemoting -Force on the host.

On the Windows host, running Enable-PSRemoting -Force ensures that the WinRM service is launched, the required firewall rules are set up, a WinRM listener is set up, and more. Check out the Enable-PSRemoting help page for a complete explanation.

Allowing Traffic That Isn’t Encrypted

Allowing HTTP traffic is the next stage. This is accomplished by running the command below on the Windows hosts to configure the WinRM listener:

> winrm set winrm/config/service ‘@{AllowUnencrypted=”true”}’

Authentication on a Basic Level

You must accept basic authentication since you’re enabling WinRM to authenticate against local Windows users rather than Kerberos (Active Directory) or other more sophisticated approaches like certificates.

The WinRM listener does not support simple authentication by default. You may use the winrm set command to enable basic authentication once again, as illustrated below.

> winrm set winrm/config/service/auth ‘@{Basic=”true”}’

pywinrm (Python WinRM) Package Installation

After you’ve finished with the Windows setup, you may go on to the Linux client. Because Linux cannot directly communicate with a Windows node through WinRM. It will need some assistance. Fortunately, Python comes pre-installed on the majority of Linux variants.

You may install a package called pywinrm that will take care of the first connection for you.

The pip package manager may be used to obtain and install the pywinrm package. If you don’t have this package management installed yet, run sudo easy install pip or use your favorite method to do so.

The pywinrm package may then be downloaded using pip.

> sudo pip install pywinrm

The pywinrm Library is being imported.

It’s time to connect to the Windows host now that it’s been installed. To put this to the test, we’ll open Python in interactive mode.

Import the winrm library once you’re in interactive mode. If there are no problems in this section, the library was successfully imported.

Creating a Remote PowerShell Session

A WinRM session to the Windows node must then be established. To do so, use the Session() function and provide in the following parameters:

  • the Windows host’s name or IP address
  • the name of the local Windows user who will be used to authenticate
  • the password for the user

Here, I’ll use the local administrator password. If you want to utilize a different user account, make sure they’re a member of the Remote Management Users group.

> session = winrm.Session(‘<IPorHost>’, auth=(‘administrator’,'<password here>’))

If no errors have been thrown yet, a session has been formed. To test things out, execute a command on the remote Windows node at this point.

Putting things to the test

The run ps() technique is one approach to run PowerShell commands from Linux via WinRM on a Windows host. This function on the session object lets you execute any command you want within a PowerShell session on the remote Windows host.

When the run ps() function is called, the command is run on the remote Windows machine and the output (if any) is sent to the Linux client’s stdout.

The Python syntax for capturing the output of a command and printing it to the console is shown below. To get the hostname, I just execute the command hostname on the Windows host.

> result = session.run_ps(“hostname”) > print(result.std_out)

Summary

You should now have all of the abilities needed to run PowerShell commands from Linux clients on a Windows server remotely. This approach may alter somewhat depending on the Linux distribution, but because we’ve utilized Python, the variations should be minor.

The “pip pywinrm” is a Python module that allows you to use WinRM on Linux. The module is installed using the pip command.

Frequently Asked Questions

Does WinRM work on Linux?

A: Windows Remote Management is a very old protocol, and has been superseded by more modern protocols like SSH and RDP. Its not possible to use WinRM on Linux without third party software.

How do I execute Windows PowerShell command remotely from Linux machine using Python?

A: dpkg -i python-2.7.14.1-x86_64.deb

How do I connect to WinRM?

A: WinRM is a Windows component which enables the user to remotely execute commands on another computer. Its possible that your firewall may be preventing you from connecting, so please ensure its open and try again.

Related Tags

  • test winrm connection from linux
  • python winrm example
  • how to check winrm version in linux
  • pip install winrm
  • python3-winrm

Table of Content