0

I have a Python module (PyBind11 C++ Lib) that resides on an ECU. To use this library I need a SSH connection to the ECU to start a Python instance via the terminal.

~$ ssh root@IP
~$ python3

python 3.8.2 (default, Feb 25 2020, 10:39:28) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import myModule

>>> myModule.do_something()
...

This works perfectly in the manual application

Now if I want to run the whole thing completely via Python on the host: (Pseudo Code)

>>> from myLibs import ssh_session

>>> with ssh_session(IP, user, pw) as clt:
       clt.exec_command("python3")
       clt.exec_command("import myModule")
       clt.exec_command("myModule.do_something()")
       ...

Here the commands should be Python commands instead of simple terminal commands.

Anyone have any ideas on how to implement this?

Thanks and greetings

JuSt Alex
  • 1
  • 1

3 Answers3

0

What I have understood from you question is you want to run a python script on the host:

import myModule

myModule.do_something()

Probably scp the script and then running the script might suffice:

from myLibs import ssh_session

with ssh_session(IP, user, pw) as clt:
    clt.exec_command("scp <source_user>@<source_host>:<source_file_path> <destination_user>@<destination_host>:<destination_file_path>")
    clt.exec_command("python3 your_copied_script.py")
  • On the ECU target that I can use via the SSH connection, there is a Python library that I want to run from the host to send data to the host from the ECU. On the ECU there is no python script that can be executed from the host, but only the library that I can use to configure and send the data with the respective Python commands. Therefore it is important that I can use the python library on the ECU target via the SSH tunnel from the host. – JuSt Alex Jul 28 '22 at 11:30
  • Process flow: 1. Connect to the Target via SSH tunnel 2. Start the python interpreter via terminal 3. Import the Python library (Pybind11 C++) 4. Create a sample with data from the library 5. Send the sample via command from the target to the host (library command) 6. Exit the ssh session and check the received data on the host – JuSt Alex Jul 28 '22 at 11:45
0

1.) HOST (LINUX):

  • Connect ECU via ssh tunnel

2.) TARGET (ECU):

  • Run python interpreter
  • Import myModule
  • Set mySample
  • Send mySample to host

3.) HOST (LINUX)

  • Compare mySample data with expected data (origin)
  • Exit ssh session
JuSt Alex
  • 1
  • 1
0

Try creating a python file that contains what you want to run on the target and then call the python interpreter:

clt.exec_command("touch myscript.py")
clt.exec_command("echo \"import MyModule\" >> myscript.py")
clt.exec_command("echo \"MyModule.dostuff()\" >> myscript.py")
clt.exec_command("python3 myscript.py")

I also suggest making a function .exec_all(*args) to run multiple commands in a single function call, therefore making the whole thing above a little simpler:

clt.exec_all("touch myscript.py",
             "echo \"import MyModule\" >> myscript.py",
             "echo \"MyModule.dostuff()\" >> myscript.py",
             "python3 myscript.py")
Alex D
  • 16
  • 2