user1463479
user1463479

Reputation: 63

using python commands within paramiko

I've been using Paramiko today to work with a Python SSH connection, and it is useful.

However one thing I'd really like to be able to do over the SSH is to utilise some Pythonic sugar. As far as I can tell I can only use the inbuilt Paramiko functions, and if I want to anything using Python on the remote side I would need to use a script which I have placed on there, and call it.

Is there a way I can send Python commands over the SSH connection rather than having to make do only with the limitations of the Paramiko SSH connection? Since I am running the SSH connection through Paramiko within a Python script, it would only seem right that I could, but I can't see a way to do so.

Upvotes: 0

Views: 755

Answers (2)

kichik
kichik

Reputation: 34704

RPyC could be what you're looking for. It gives you access to another machine's Python environment directly from the local script.

>>> import rpyc
>>> conn = rpyc.classic.connect("someremotehost.com")
>>> conn.modules.sys.path
['D:\\projects\\rpyc\\servers', 'd:\\projects', .....]

To establish a connection over SSL or SSH, see:

http://rpyc.sourceforge.net/docs/secure-connection.html#ssl

Upvotes: 1

Rostyslav Dzinko
Rostyslav Dzinko

Reputation: 40755

Well, that is what SSH created for - to be a secure shell, and the commands are executed on the remote machine (you can think of it as if you were sitting at a remote computer itself, and that either doesn't mean you can execute Python commands in a shell, though you're physically interact with a machine).

You can't send Python commands simply because Python do not have commands, it executes Python scripts.

So everything you can do is a "thing" that will make next steps:

  1. Wrap a piece of Python code into file.
  2. scp it to the remote machine.
  3. Execute it there.
  4. Remove the script (or cache it for further execution).

Basically shell commands are remote machine's programs themselves, so you can think of those scripts like shell extensions (python programs with command-line parameters, e.g.).

Upvotes: 0

Related Questions