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.


Installation of freeSSHd:

Download freeSSHd from below mentioned link:

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


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
            ssh = paramiko.SSHClient()
            print "Connected to %s" % hostname
        except paramiko.AuthenticationException:
            print “Failed to connect to %s due to wrong username/password” %hostname
            print “Failed to connect to %s” %hostname
            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 :

Recent search terms:

  • https://blog calsoftinc com/2017/02/run-commands-remote-windows-machine-using-python-paramiko-module html
  • build a window utility for running script on remote linux server using python
  • enable ssh on windows machine
  • for ssh which module need to use in pythn
  • how to connect a remote desktop using python scripts
  • how to execute command in remote linux server from windows in python
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1824 z-rRDtWA2JoMeEnJkq8l-ATodcytv0I_cUe_KWHJWLFZ0MXudKhggosOtutZ2ZYM 6d2be37283fe79b0aa3cfb096a2340ab6b84ef3d&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1828 oOjAHNZz4h-ZBWISJobi3XKoWQ2Y8k_EZlRehfXgLUIepOjDfv1XoFDpuy-_4Kas dcd3b0b02c6f89fba382a4b99657ce3da3edd1ae&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1829 bim7Cj1YybT457rENSVATRWFQj5Gp9IoGOut9SnE2vuwCLivIbeWlQkdNaOs2nxs ad3a782377e3a2cf31153654693a2722c04b0848&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1883 KVec_L_oU2SJO2h2Gbc3NESdEDvA1xRuNBkuxevWY7xRRrA1SZgRsGD1huc6CD5E e09ab50b9bf9755fcefb8c969020bb40fd9e406a&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
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 *