0

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.

1

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.).

4 Comments

so is my best course of action to place a script on the remote machine and call the script? i can't think of any other ways of doing what i need to do.
@user1463479, basically yes, that's possibly the best way if you want to write scripts in Python.
thank you for being so clear. its what i thought anyway but i needed it to be confirmed.
@user1463479 It is not correct to say 'Python do not have commands'. If you run 'python --help', you will see the '-c' option. It allows "program passed in as string". It is OK for simple program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.