'How Can I Run Commands in a Python Environment Over SSH from a Python Script?

I am currently working with Create2 Roombas. They are connected through serial to USB connections to Raspberry Pis that I control from my laptop through SSH.

I can successfully run a local python script that creates an SSH connection to both of the Pis and runs a different script already on the Pis simultaneously. However, I would like to create Python shells on the Pis through that same SSH connection and feed live commands to it.

I use paramiko to connect the laptop to the Pis and pycreate2 to connect the Pis to the Roombas.

This is an example of a script that works correctly:

import piInterfaceTEST as piI
import time


ssh = piI.piCon()
time.sleep(1)
if not ssh:
    print('Error. Exiting.')
    exit()
piI.piSend('python3 test.py', 0)
time.sleep(3)
piI.piDiscon()

The local script on each Pi has commands to control its Roomba. I currently have a script to automate the transfer of the files to the Pis.

However, I would like to have a script that uses something like this:

piI.piSend('python3 -i', 0)
piI.piSend('from pycreate2 import Create2\n', 0)
piI.piSend('roomba = Create2("/dev/ttyUSB1")\n', 0)
piI.piSend('roomba.start()\n', 0)
piI.piSend('roomba.safe()\n', 0)
piI.piSend('roomba.drive_direct(-200, -200)\n', 0)
time.sleep(2)
piI.piSend('roomba.drive_stop()\n', 0)
time.sleep(1)
piI.piSend('roomba.seek_dock()\n', 0)
piI.piSend('roomba.close()\n', 0)
piI.piSend('quit()\n', 0)
time.sleep(3)
piI.piDiscon()

I also tried sending it all in one command, like this:

piI.Send('python3\nfrom pycreate2 import Create2\nroomba = Create2("/dev/ttyUSB1")\nroomba.start()\nroomba.safe()\nroomba.drive_direct(-200, -200)\ntime.sleep(2)\nroomba.drive_stop()\ntime.sleep(1)\nroomba.seek_dock()\nroomba.close()\nquit()\n', 0)

My goal is to remove the need to have a file put on the Raspberry Pis and make control of the Roombas fully remote.

The second code here does not work, and I am wondering if I will need to use a different SSH library or if there is an easier way to pass the commands through to the Python shell.

With a manual SSH connection in terminal, I can accomplish this:

pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pycreate2 import Create2
>>> roomba = Create2("/dev/ttyUSB1")
----------------------------------------
 Create opened serial connection
   port: /dev/ttyUSB1
   datarate: 115200 bps
----------------------------------------
>>> roomba.start()
>>> roomba.safe()
>>> roomba.drive_direct(-200, -200)
>>> roomba.drive_stop()
>>> roomba.seek_dock()
>>> quit()
Closing port /dev/ttyUSB1 @ 115200

I can do this manually, but I would like to do this with a script.

What is the formatting to allow paramiko (if possible) to accomplish this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source