I am trying to run a Python script remotely on my RPi from another python script on my computer.

For now, I have created a local network on my RPi, that my computer can connect to.

I tried to run a subprocess connecting through ssh from my computer:

import subprocess
proc = subprocess.call(["ssh","pi@","python3 /home/pi/desktop/python_server/python_server.py"],stdout=subprocess.PIPE)
while True:
   line = proc.stdout.readline()
   if not line:
   print("test:", line.rstrip())

But the script doesn't start and I dont get any data back.

How can I connect remotely to the RPi from my computer and start the Python script?

If possible it would be best if this to be done without manually connecting to a local network first. But I dont know if this is possible?


The process got stuck as it was waiting for me to enter the password to the PI.

How can I avoid entering a password or can Python automatically enter a password to access ssh?

NOTE: I would like to access through ssh from any device, so a ssh key between a single computer and the RPi is not an option.

  • It is hard to see how this is a Pi problem, especially when the ssh command would't do anything on the remote - did you test FIRST before you tried to script it? – Milliways Dec 2 '20 at 11:16
  • 1
    Hi. I was not aware that the ssh command wouldn't do anything. I thought this was the way to go, so thats what i tested. If there is another way I am open for suggestions. I find it hard as a beginner programmer to find guides and tutorials on this subject. – Frederik Petri Dec 2 '20 at 11:59
  • Does this answer your question? Should I generate SSH keys for accessing the Raspberry Pi? – Dmitry Grigoryev Dec 3 '20 at 12:24
  • It doesn't really, as I would like any computer to access ssh without performing the sharing of ssh keys first as I state in the "NOTE" in my question. But thanks for the answer – Frederik Petri Dec 3 '20 at 12:32
  • Why can't you share the SSH keys? There are solutions which forego all security (e.g. telnet), but I really don't see how copying a file to several computers is a problem – Dmitry Grigoryev Dec 3 '20 at 12:36

Debug that command from a terminal first, before wrapping it into a Python script.

Chances are, ssh is stuck waiting for you to type the password in order to login.

There's an official Pi guide explaining how to setup passwordless SSH access on a Pi using private/public SSH keys. You can share the same SSH key across several installations.

  • Thanks. I updated the question – Frederik Petri Dec 2 '20 at 12:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.