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

There are various readily available python modules to connect to a Windows client. Some of them are pywinrm, wmi etc. I have faced many issues with these modules. Some of them are listed below:

  1. Failed 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 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 SSH server is running on the target machine. Target machine can be Linux or Windows system. For Linux systems, SSH packages are already available hence we can use it easily. But for Windows systems, SSH package is not available by default. Hence to enable SSH server on Windows machine we have to install some extra third party software.

Using freeSSHd  application, it is possible for a user to set up a secure shell on a Windows  machine. It also have a GUI tool for the configuration of both services. Below mentioned are the required steps steps of 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 from below mentioned link:

http://www.freesshd.com/?ctt=download

Just double-click the installation file. User has to look at below mentioned points while installation:

  • User has to generate the Private keys. User will be prompted for this while installation), and
  • Please 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.

Image 1

User can see the freeSSHd in the system tray.

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

Image 2

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

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

Step1: Open freeSSHd. Then open the freeSSHd settings window.

Step2: Now, click on the Users tab.

Step3: After that, click on Add button.

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

Image 3

Conclusion:

And that’s it. Now SSH server will be running on the Windows machine. Using required user-name(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)

[Tweet “How to Run Commands on Remote #Windows Machine Using #Python Paramiko Module ~ via @CalsoftInc”]

Looking for more information on python modules? 

 
Share:

Related Posts

Navigating Big Data Storage Challenges

Navigating Big Data Storage Challenges

The last decade or so has seen a big leap in technological advancements. One of the technologies to come up at this time and see a rapid…

Share:

A Deep Dive into 5G Service-Based Architecture (SBA)

5G technology roll out signifies an immense revenue opportunity for telecom industry.

Share:
Technical Documentation

Technical Documentation Review and Tips

Technical reviews are vital for effective and quality documentation. To make this happen, have documentation and its reviews listed as one of the deliverables – just like development or testing. This will place priority on the process, and ensure everyone involved understands the importance of proper and thorough reviews.

Share:
Technology Trends 2024

Technology Trends 2024- The CXO perspective

In the rapidly evolving landscape of 2024, technology trends are reshaping industries and redefining business strategies. From the C-suite perspective, executives are navigating a dynamic environment where artificial intelligence, augmented reality, and blockchain are not just buzzwords but integral components of transformative business models. The Chief Experience Officers (CXOs) are at the forefront, leveraging cutting-edge technologies to enhance customer experiences, streamline operations, and drive innovation. This blog delves into the strategic insights and perspectives of CXOs as they navigate the ever-changing tech terrain, exploring how these leaders are shaping the future of their organizations in the era of 2024’s technological evolution.

Share:
Technology Trends 2024

The Winds of Technology Blowing into 2024

As 2023 draws to a close, the digital landscape is poised for a seismic shift in 2024. Generative Artificial Intelligence (Gen AI) continues its integrative streak, disrupting industries from B2B to healthcare. Networking trends emphasize simplicity, while the synergy of cloud and edge computing with Gen AI promises real-time workflows. Quantum computing, cybersecurity, intelligent automation, and sustainable technology are key players, reshaping the technological fabric. Join us as we navigate the transformative currents of 2024, unraveling the impact on enterprises in our forthcoming article. Stay tuned for the tech evolution ahead!

Share:
Generative AI Shaping Future Industries

[Infoblog] Generative AI Shaping Future Industries

Generative AI is at the forefront of innovation, harnessing the power of machine learning algorithms to create new and original content, from images and music to entire virtual environments. This infographic depicts how Gen AI is evolving industries and shaping its future.

Share:

This Post Has One Comment

Comments are closed.