0

I am writing a python script to do things on a remote computer. Actually the python script runs on my computer and the remote computer is a robot running ubuntu.

The command that I am issuing starts the robot software and doesn't exit (except if you kill it with ^c). I would like the command to be issued, be able to display the next 20 seconds of output (or until it goes "quiet") and then have my script actually complete.

I've been trying a combination of subprocess.run (and variants) and ssh options as well as ending the command with an ampersand. None of it works.

Here's what I have so far (you can see some experimental alternatives commented out and in). Everything I've tried either blocks at the ssh (waiting for subprocess to finish which it won't) or just runs through and I don't even see the prompt for the ssh password.

def ssh(self, command_line):
        ssh_cmd = ["ssh", "pi@" + self.cfg["BRU_MASTER_IP"], command_line]
        print(ssh_cmd)
        print("1")
        #cmd1 = subprocess.Popen(" ".join(ssh_cmd), shell=True, stdout=subprocess.PIPE)
        completed_process = subprocess.run(" ".join(ssh_cmd), shell=True, capture_output=True, text=True)
        #completed_process = subprocess.Popen(" ".join(ssh_cmd), shell=True, stdout=subprocess.PIPE)
        print("2")
        #completed_process.communicate()
        print("2a")
        #cmd1_out = completed_process.stdout.read()
        print(completed_process.stdout.read())
        print("3")
2
  • Can you edit your question and paste the code you have written so far? Commented Jan 1, 2022 at 14:51
  • 2
    An idea. If from a local shell, I would do like ssh … 'shell code' where shell code is shell code that runs 1>log 2>&1 nohup start_robot_software &, then sleep 20, then cat log and then exits (so the local ssh exits). I cannot help you in Python, therefore just a comment. Note if start_robot_software generates output indefinitely then log will grow and grow. Commented Jan 1, 2022 at 16:52

1 Answer 1

0

I can assist with Linux/Unix suggestion, weaker at python.

Assuming you have an endless long running command:

Lets say ping $HOSTNAME

And you want to run ping $HOSTNAME on a remote machine with ssh:

 ssh user@remoteMachine 'ping $HOSTNAME'

But you want to timeout ping $HOSTNAME on a remote machine after 15 secs:

 timeout 15 ssh user@remoteMachine 'ping $HOSTNAME'

Eventaully after testing that your script is working and respecting the timeout command.

Finally your Linux command is ready, and your are ready for python scripting

Create your python script with arguments and parameters to send the above command.

Mind the quotes: ' has different behavior from " (try it out).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.