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:
- Failed to connect to the remote windows machine
- 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.
- Windows machine (a desktop or a server)
- freeSSHd installable
- Admin right to open port 22
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.
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.
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.
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/
Latest posts by Virendra Kumawat (see all)
- How to Run Commands on Remote Windows Machine Using Python Paramiko Module - February 8, 2017