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:

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)

Listen to podcast on same topic : http://calsoftinc.com/resources/rich-media/calsoft-podcast-use-paramiko-windows-clients/

[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

The Role of NFV in 5G Networks: Opportunities and Benefits for Telecom Providers

With 5G networks on the rise, Network Function Virtualization (NFV) has become an important part of the telecom industry. NFV enables telecom providers to reduce the cost and complexity of their networks by providing a virtualized environment for network functions. This blog will explore the role of NFV in 5G networks, the opportunities and benefits that it offers to telecom providers, and how they can use NFV to improve their networks.

Share:

Leveraging AWS for Real-Time Fault Detection in the Manufacturing Industry

With the increasing complexity of the manufacturing industry, it is essential to adopt advanced technologies such as AWS cloud computing and machine learning to detect faults in real-time. This blog will discuss how to leverage AWS for real-time fault detection in the manufacturing industry, and the various benefits of this technology.

Share:

Leveraging AWS Local Zone for Deploying 5G Network Functions

As 5G networks become increasingly important for businesses and consumers, organizations are looking for ways to deploy 5G network functions quickly and cost-effectively. AWS Local Zone is one of the most promising solutions for this, providing low-latency access to services and data while reducing costs and improving performance. In this blog, we’ll explore how AWS Local Zone can be leveraged to deploy 5G network functions in an efficient and cost-effective manner.

Share:

MWC 23 Top Technology Trends

Mobile World Congress (MWC) is the one of the greatest and most influential connectivity events in the mobile industry where mobile device manufacturers, technology providers, and other industry stakeholders come together to showcase their latest products, services, and innovations. MWC 23 was held in Barcelona from 27 February to 2 March 2023. The event highlighted several emerging technologies and latest trends in the industry market. Read the blog to discover the top technology trends at MWC 23 and how these trends grow over the coming years!

Share:

Significance of AI to Underpin the Metaverse

The term “Metaverse” generally refers to a hypothetical future version of the internet that would be much more immersive and interactive, resembling a virtual world. Artificial intelligence (AI) is likely to play a major role in the development of the metaverse. AI could be used to create more realistic virtual environments in the future. Explore the blog to understand how can AI shape the Metaverse technology.

Share:

The 5G Uprising: Influence on Business and Telco Industry

The impact of 5G on the telecom industry is likely to be substantial and transformative, leading to new growth opportunities, increased efficiency, and improved customer experiences. Explore the blog to understand how 5G will transform business and the telecom industry.

Share:

This Post Has One Comment

Leave a comment / Query / Feedback

Your email address will not be published. Required fields are marked *