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/

Recent search terms:

  • connect to remote machine using python
  • python run command on remote windows machine
Virendra Kumawat

Virendra Kumawat

Senior Principal QA Engineer at Calsoft Inc.
Having 8 Years of experience in NAS and Automation. Worked on Shell/Perl and Python scripting. Currently working in Calsoft.
Virendra Kumawat

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

Leave a Reply

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