1

I have to run python code on remote python process.

Normally, what I would do is:

ssh admin@localhost -p 61234

which opens a interactive python console and I can execute python code directly.

>>> print('3')
3
>>> 

But I want to automate this and pass python code as parameter to ssh.

I tried following options:

ssh admin@localhost -v -p 61234 python logs.py

ssh admin@localhost -v -p 61234 nohup python logs.py

ssh admin@localhost -p 61234 < logs.py

cat logs.py | ssh admin@localhost -p 61234 python -

But all options give following error:

shell request failed on channel 0

logs.py:

#!/usr/bin/env python
# tried with and without first line.
print('3')

netstat -anp | grep 61234
tcp        0      0 127.0.0.1:61234         0.0.0.0:*               LISTEN      6/python2.7

Is there a way to do this?

Community
  • 1
  • 1
codingenious
  • 8,385
  • 12
  • 60
  • 90

2 Answers2

0

Pretty sure this is overkill, but who knows, maybe you need more than just a simple command in the future.

Paramiko package is what you're looking for. The project is full of demos which demonstrate how and in many different ways.

The function you'll find the most useful is paramiko.client.SSHClient.exec_command

Execute a command on the SSH server. A new Channel is opened and the requested command is executed. The command’s input and output streams are returned as Python file-like objects representing stdin, stdout, and stderr.

Demos Folder
In-depth Testing
Interactive.py is a fully interactive TTY (remote terminal control functions).

Noah M.
  • 310
  • 1
  • 8
0

PyCharm Professional Edition (Python IDE from JetBrains) has tools for remote development, including SSH remoting, and the ability to run a remote interpreter:

enter image description here

Richard Inglis
  • 5,888
  • 2
  • 33
  • 37