Issue
I am using Paramiko to make a ssh connection to a remote window machine, and then I use paramiko's exec_command(r'python test.py') to run a 'test.py' on the remote machine. What test.py does is simply opening a 'notepad'.
However, when I use exec_command(), and login to the remote machine by Remote Desktop Connection(RDP) to see if the notepad is actually opened. Nothing is opened in the remote machine.
I know my SSH connection is correctly set up and working. But why can't I see the result in RDP, and how can I see the result in RDP?
Solution
Paramiko (Python Module) works on the SSHv2 protocol. It provides both client and server functionality. Paramiko module can be used if the SSH server is running on the target machine. The target machines can be Linux or Windows systems. For Linux systems, SSH packages are already available hence we can use them easily. But for Windows systems, the SSH package is not available by default. Hence to enable the SSH server on Windows machine we have to install some extra third-party software.
Using the freeSSHd application, it is possible for a user to set up a secure shell on a Windows machine. It also has a GUI tool for the configuration of both services. Below mentioned are the required steps by 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. The user has to look at the below-mentioned points while installing:
- 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.
The user can see the freeSSHd in the system tray.
Just after right click the system tray icon and select Settings. Users can see a green check next to the SSH server.
By default Telnet server and SSH server is not running on freeSSHd GUI. The user has to click on them to start any required service.
I have used it for the SSH server hence the user has to click on the ‘SSH server option’ on the GUI. freeSSHd doesn’t use AD information, hence the user has to create a 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, the user has to fill in all required and necessary details for a new user in the User Properties dialog and then click on the OK button. Using this user, now a secure shell connection can be established for that windows machine.
Conclusion:
And that’s it. Now SSH server will be running on the Windows machine. Using the required user name (which was provided in freeSSHd GUI) user can connect to this windows machine using Secure Shell.
Here is the sample code should be like below:
import paramiko
hostname = "your-hostname"
username = "your-username"
password = "your-password"
cmd = 'your-command'
#SSH Client connection Code:
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)
#Execution Command and get feedback:
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)
print(final_output)
ref: https://stackoverflow.com/a/48682660/18600126
Answered By - Baris Sonmez Answer Checked By - Mildred Charles (WPSolving Admin)