How to Run Commands on Remote Windows Machine Using Python Paramiko Module

The traditional notion of office-based work is taking a backseat as work from home is becoming the new normal, leaving room for a more decentralized approach. Today, a staycation where people work from the location of their choice blending work and leisure is grabbing attention too. However, the physical office notion is confined to remote and flexible work arrangements in the limelight. This is why remotely managing Windows machines is becoming the need of the hour.

In this tech-driven world, remotely managing Windows machines is essential for seamless operations. Our latest blog explores how to streamline this process using Python’s Paramiko module. Whether you’re a sysadmin, developer, or tech enthusiast, mastering remote command execution on Windows with Paramiko opens a world of possibilities. Carefully curated, the blog delves into the intricacies of this powerful tool, empowering the management of Windows infrastructure efficiently.

Running commands on a remote Windows machine using Python’s Paramiko module offers secure, automated management and control. It can add efficiency by enabling remote task execution, system monitoring, reduction in the need for physical access, script deployment, and manual intervention, ultimately leading to less time and better operational productivity.

What is Python Paramiko

Paramiko is a powerful Python library designed for SSH (Secure Shell) connections, enabling secure, automated, and programmatic interactions with remote machines. Although it is traditionally used with Unix-based systems, it can also connect to Windows machines running an SSH server, such as OpenSSH. This capability is particularly useful for tasks such as remote command execution, file transfers, and automation scripts.

By leveraging Paramiko, you can streamline administrative tasks, enhance security through encrypted connections, and improve efficiency by enabling script-based operations on remote Windows servers. This eliminates the need for manual interventions or less secure protocols, making it an excellent choice for IT professionals and developers looking to automate and secure their workflows across different operating systems.

Using Paramiko for remote operations on Windows machines offers several advantages. It supports key-based authentication, which enhances security by eliminating the need for passwords and reducing the risk of brute-force attacks. Additionally, Paramiko’s flexibility allows it to be integrated into various automation frameworks and tools, making it a versatile choice for complex workflows. Its robust error handling and exception management capabilities ensure reliable execution of remote commands and scripts, even in unpredictable network conditions. Overall, Paramiko simplifies remote server management, boosts productivity, and ensures secure communication, making it an indispensable tool for modern IT infrastructure management.

Check out how customers can leverage our expertise in the diverse virtual ecosystem.

How to Run Commands on Remote Windows Machine using Python Paramiko Module?

Now that we have a fair understanding of what Python Paramiko is, it is time to delve into understanding how it can be used on a remote Windows machine. Through this blog, I want to share my experience to help readers understand in detail how to run Windows machines using the Python Paramiko module.

There are various readily available Python modules to connect to a Windows client. Some of them are pywinrm, wmi etc. There are often several issues with these modules. Some of them are listed below:

  1. Failure to connect to the remote Windows machine
  2. Not able to get the output of executed commands on remote Windows machine etc.

In my assignment, I am already using the paramiko module to connect to remote Linux clients hence tried to use it for Windows machines as well.

Paramiko (Python Module) works on SSHv2 protocol. It provides both client and server functionality. Paramiko module can be used if the SSH server is running on the target machine. The target machine can be a Linux or Windows system. For Linux systems, SSH packages are already available hence we can use them easily. But for Windows systems, the SSH package is not available by default. Hence to enable the SSH server on the Windows machine we must install some extra third-party software.

Using freeSSHd application, a user can set up a secure shell on a Windows machine. It also has a GUI tool for the configuration of both services. Below are the required steps for getting an SSH server up and running on any user’s Windows client.

Pre-Requirements:

  • Windows machine (a desktop or a server)
  • freeSSHd installable
  • Admin right to open port 22

Installation of freeSSHd:

Download freeSSHd:

Double-click the installation file. The user has to look at the below-mentioned points while installing:

  • The user must generate the Private keys. User will be prompted for this while installation) and
  • It is recommended that freeSSHd should not be started as a system service

How to use freeSSHd:

Just double-click on the freeSSHd desktop icon. A new icon can be seen in the system tray.

freeSSHd

Users can see the freeSSHd in the system tray.

Just after right click the system tray icon and select Settings. Users can see a green check next to the SSH server.

on freeSSHd GUI

By default, Telent server and SSH server is not running on freeSSHd GUI. User must click on them to start any required service.

I have used it for an SSH server hence user has to click on the ‘SSH server option’ on the GUI. freeSSHd doesn’t use AD information, hence user needs to create a new user who can access the machine.
Below are the steps for creating a new user on freeSSHd.

Step 1: Open freeSSHd. Then open the freeSSHd settings window.

Step 2: Now, click on the Users tab.

Step 3: After that, click on the Add button.

Now, the user needs to fill in required and necessary details for a new user in the User Properties dialog and then click on the OK button. Using this user, now secure shell connection can be established for the windows machine.

SSH server

Once the above is done, the SSH server will be running on the Windows machine. Using the required username (which was provided in freeSSHd GUI) user can connect to this Windows machine using Secure Shell.

Code snippet to use Windows machine using paramiko:

import paramiko
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,username=username,password=password)
print “Connected to %s” % hostname
except paramiko.AuthenticationException:
print “Failed to connect to %s due to wrong username/password” %hostname
exit(1)
except:
print “Failed to connect to %s” %hostname
exit(2)
try:
stdin, stdout, stderr = ssh.exec_command(cmd)
except Exception as e:
print e.message
err = ”.join(stderr.readlines())
out = ”.join(stdout.readlines())
final_output = str(out)+str(err)

Parting Thoughts

Leveraging Python’s Paramiko module for remote command execution on Windows machines streamlines administrative tasks, enhances security, and boosts productivity. By following the outlined steps and utilizing freeSSHd, you can establish a secure SSH connection, ensuring efficient management of remote systems. Embrace this powerful tool to automate and simplify your workflows, making your IT infrastructure management more robust and reliable.

At Calsoft, we provide the right approach and solutions to help businesses succeed with advanced technology. If you are looking for more information on Python Modules.

 
Share:

Related Posts

Gen AI Trends 2025

Top Generative AI Trends Shaping 2025

Modernization of industries began with the Industrial Revolution in the early 19th Century with the use of machines, and it has continued with the digitization of devices…

Share:
IoT and its Applications in Driving Smart Manufacturing

IoT and its Applications in Driving Smart Manufacturing

The Internet of Things (IoT) is a key element of global industrial transformation, and the manufacturing sector leads in leveraging this technology. The millions of IoT devices,…

Share:
Product Lifecycle Management in Software Development using Large Language Models

Product Lifecycle Management in Software Development using Large Language Models

The data of any organization is of extreme value. But what happens when that data is not trustworthy and accessible to your teams? You will face challenges…

Share:
Kubernetes Introduction and Architecture Overview

Kubernetes: Introduction and Architecture Overview

Containers are taking over and have become one of the most promising methods for developing applications as they provide the end-to-end packages necessary to run your applications….

Share:
How to Perform Hardware and Firmware Testing of Storage Box

How to Perform Hardware and Firmware Testing of Storage Box

In this blog will discuss about how to do the Hardware and firmware testing, techniques used, then the scope of testing for both. To speed up your testing you can use tools mentioned end of this blog, all those tools are available on internet. Knowing about the Hardware/Firmware and how to test all these will help you for upgrade testing of a product which involve firmware

Share:
Cloud Application Development

Challenges of Cloud Application Development

Explore the challenges and solutions of cloud application development, including benefits, performance issues, and overcoming vendor lock-in for seamless cloud integration.

Share:

This Post Has One Comment

Comments are closed.