paramiko exec_command how can I see the result using RDP? [closed] - python

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 months ago.
Improve this question
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?

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://www.calsoftinc.com/blogs/2017/02/run-commands-remote-windows-machine-using-python-paramiko-module.html
ref: https://stackoverflow.com/a/48682660/18600126

Related

pywinauto automation of jump server login in putty is not behaving same as manual jump server login in putty

I manually login to one of my Application GUI consoles(https://localhost:port) hosted on a server on the enterprise internal network, through a jump server. These are the exact steps:
1. Open putty
2. ssh(port 22) to the jumpserverhost.domain.com(this is a linux server)
3. Enter user, and passcode(RSA token)
4. Ensure the putty session window is alive/open
5. Now open browser and open the Application console - https://localhost:port
NOTE: "We don't use the direct server IP, we use localhost ip-127.0.0.1 and the port on which the application GUI is running"
6. Enter Application GUI user/password and do your work on the Application
My requirement is to automate the above steps for login to my Application GUI Login. This is what I have done
a. Use a pywinauto script to automate the exact human steps from steps 1-4 above
Please check my answer to how to pass variable in pywinauto.Application().start(cmd_line='') for the script I am using for the task.
Even though my pywinauto script is working just fine and logging in to the jump server via putty, I am not able to connect to the application GUI, see image below.
But every time I login to the jump server manually using steps 1-4 above, the application GUI login page opens on https://127.0.0.1:. See image below
Why is it changing behavior via automated script ?
I have also checked many articles which suggest to automate ssh connections through paramiko , will it work in this case ? (considering the ssh connection to the jump server needs to be alive till the time I'm connected to the Application GUI )
My knowledge of how jump servers work is very shallow, please help.
So I suspect that the problem is that you use Putty to create an SSH tunnel connection to the server in question, while your pywinauto command might just open a normal SSH connection. You can check if there is an ssh tunnel configured under Connections > SSH > Tunnels in the Putty configuration for that server.
From what I read in your other question regarding pywinauto, it seems this is indeed the case as the command you issue there:
putty.exe -ssh uname#host.domain.com
Doesn't create a tunnel.
If you want to create an SSH Tunnel with the Putty command-line with pywinauto, you should do something like:
putty.exe -ssh uname#host.domain.com -L <server_port>:host.domain.com:<local_port>

PyCharm remote debugging about double SSH

Using PyCharm remote debugging is one of my favorite choices when doing deep learning jobs on Server platform. But recently I face a problem is that I have to first use SSH to login the platform then I will need another SSH to access the computing node. I may have to do this using my shell.
ssh myname#myip
ssh mynode
python myfile.py
Thus, usually when I use PyCharm. I can only do as the following:
ssh myname#myip
python myfile.py
My question is: how I can use PyCharm to double my SSH operation?
Generally, I use MobaXterm as the intermediate jump tools.
Choose Tunneling toolbar, you will get a dialog like this 1st step
Click the gear in settings box,and then edit the local port forwarding like this 2nd step
The final step, adding remote interpreter in pycharm:
3.1. Choose ssh interpreter
3.2. The host should be localhost, and port is the one mapped to your own PC. Then enter your username and follow the dialog.
Here is the figure of 3rd step

How to connect to ssh server from another server

I've been trying to connect to first an out-of-band management server (in my case, since I'm connecting to DELL-servers, an iDRAC) and through that connect to the main server itself. I've got it to work when I do it manually, using, in the (windows) terminal:
putty.exe -ssh 'username'#'iDRAC-IP'
followed by PuTTY window opening where I type in the password, followed by
connect
which connects to the server itself, and then I type in the username and password for the server, completing the process.
When I've been writing my script in python, I'm using paramiko, http://www.paramiko.org/, suggested here on stackoverflow, and following this example: https://www.ivankrizsan.se/2016/04/24/execute-shell-commands-over-ssh-using-python-and-paramiko/, and it works just splendid for the iDRAC (the first server I connect to). It also works when I type in
stdin, stdout, stderr = ssh_client.exec_command('connect')
because I am still in my first server (ssh_client) (I can tell this is working because when I try to connect to the server manually afterwards, it is occupied). But after that it stops working, since when doing 'connect' I am no longer in ssh_client, but in a different server.
So my question is - how do I connect to a server from another server (in this case being the out-of-band management server) and log in to this one?
You can use ssh tunnel to do so.
this post may resolve your problem:
PyCharm: Configuring multi-hop remote Interpreters via SSH

How to remote debug in PyCharm

The issue I'm facing right now:
I deploy Python code on a remote host via SSH
the scripts are passed some arguments and must be ran by a specific user
the PyCharm run/debug configuration that I create connects through SSH via a different user (can't connect with the user that actually runs the scripts)
I want to remote debug this code via PyCharm...I managed to do all configuration, I just get permission errors.
Are there any ways on how I can run/debug the scripts as a specific user (like sudo su - user)?
I've read about specifying some Python Interpeter options in PyCharm's remote/debug configuration, but didn't manage to get a working solution.
If you want an easy and more flexible way to get into the PyCharm debugger, rather than necessarily having a one-click "play" button in PyCharm, you can use the debug server functionality. I've used this in situations where running some Python code isn't as simple as running python ....
See the Remote debug with a Python Debug Server docs for more details, but here's a rough summary of how it works:
Upload & install remote debugging helper egg on your server (On OSX, these are found under /Applications/PyCharm.app/Contents/debug-eggs)
Setup remote debug server run configuration: click on the drop-down run configuration menu, select Edit configurations..., hit the + button, choose Python remote debug.
The details entered here (somewhat confusingly) tell the remote server running the Python script how to connect to your laptop's PyCharm instance.
set Local host name to your laptop's IP address
set port to any free port that you can use on your laptop (e.g. 8888)
Now follow the remaining instructions in that dialog box: copy-paste the import and pydevd.settrace(...) statements into your code, specifically where you want your code to "hit a breakpoint". This is basically the PyCharm equivalent of import pdb; pdb.set_trace(). Make sure the changed code is sync'ed to your server.
Hit the bug button (next to play; this starts the PyCharm debug server), and run your Python script just like you'd normally do, under whatever user, environment etc. When the breakpoint is hit, PyCharm should drop into debug mode.
I have this (finally) working with ssh RemoteForward open, like so:
ssh -R 5678:localhost:5678 user#<remotehost>
Then start the script in this ssh session. The python script host must connect to localhost:5678 and of course your local pycharm debugger must listen to 5678
(or whatever port you choose)

Paramiko Script for SSH and VNC

I am trying to write a script to use when connecting remotely to various computers in my office. We also use VNC to allow us to see the user desktops. I have been trying to find a script that would allow me to do this, but I have had no luck. Right now, we use the SSH command in Terminal (we all use Macs), which looks like the following:
ssh "hostname" -L 5901:127.0.0.1:5900
This then requires RSA fingerprint and user password. Username is never requested as it is the same as the user profile on the computer. 5901 can also be 5902, 5903, etc, depending on which display port is specified in our VNC client.
I would ultimately like to created a script that would prompt for hostname and display port, assuming username and password can be stored permanently in the script. If not, we would need prompts for those as well. Is this even possible?
I while ago had a similar use case so I put together this script:
http://code.activestate.com/recipes/576810-copy-files-over-ssh-using-paramiko/
To tunnel VNC over SSH you would need to forward port 5900 for connecting to the real xorg instance, e.g. via x11vnc, or port 5901 to connect to the first virtual xorg (e.g. via vncserver), 5902 to connect to the second xorg, etc.
I am not aware of paramiko being able to forward ports but there seems to be a pure Python module that does just that https://gist.github.com/1399529

Categories