SSH Command to call a remote (Raspberry Pi) Python script using Matlab

33 次查看(过去 30 天)
Hello, I've been trying to run and get the output of a remote Python file that is on a Raspberry Pi.
The original file captures signals, but I just created a simple python file to see if it can work.
The python file is:
L = 1 + 5
print(L)
The Matlab file looks like this:
clc
close all
clear
% Define the SSH command to run the Python script on the Raspberry Pi
sshCommand = 'ssh id@ipaddress "python3.11 /home/mcc128/Desktop/RaspTest"';
% Execute the SSH command and capture the output
[status, cmdout] = system(sshCommand);
(I used the correct id and ip address. Omitted them for the sake of anonymity.)
When I run the file, matlab just gets stuck running the execution line forever. I don't get any error messages, just that it loads forever. Since it is a relatively simple task, it shouldn't take this long to run it. I think there's a mistake somewhere (in the code or that this function is just not supported etc.), but I can't figure it out. If there's a better way of doing it, then please do let me know. Any help would be appreciated. Thank you.

采纳的回答

Piyush Kumar
Piyush Kumar 2024-6-18
One possible reason for the command to hang is that the SSH client is waiting for a password or passphrase for the SSH key. Have you set up the SSH keys for passwordless login?
You can do this by generating an SSH key pair and adding the public key to the ~/.ssh/authorized_keys file on your Raspberry Pi. Test this by running the SSH command directly in a terminal or command prompt to ensure it does not prompt for a password.
To get in detailed information, refer to the section "Configure SSH without a password" of this documentation.
Some other troubleshooting steps you can try are -
  • Specify the full path to the Python executable in the SSH command
  • Verify the script's path and use chmod +x /path/to/script.py to ensure it's executable. Also, ensure that the user you're SSHing as has the necessary permissions to access and execute the script.
  • Run the SSH command directly from a terminal or command line interface (outside of MATLAB) to see if any error messages are displayed or if it executes as expected => If the SSH command works as expected outside of MATLAB, but not within, the issue might be with how MATLAB handles system commands. If it doesn't work outside MATLAB either, the problem is with the SSH setup or the Python environment.
  • Try specifying absolute paths for both ssh and python3.11 in your sshCommand string.
  • Simplify the command to identify where the problem might be. For example, start by just running hostname or ls on the remote machine.
sshCommand = 'ssh id@ipaddress "hostname"';
[status, cmdout] = system(sshCommand);
disp(cmdout);
Hope this helps!
  3 个评论
Navina Rajoo
Navina Rajoo 2024-6-20
Now that the simple file works, I have trouble with running the real file that captures signal/data. I use the daqhats module (installed on the raspberry pi) to run the python file for the signal acquisition, but when I call the file using matlab, i get the following error (that the daqhat module is not installed):
Status: 1
Output:
Traceback (most recent call last):
File "/home/mcc128/daqhats/my_project/daqhats_utils.py", line 5, in <module>
from daqhats import hat_list, HatError, AnalogInputMode, \
ModuleNotFoundError: No module named 'daqhats'
Would you be able to help with that? Should I provide the complete python code too?
Thank you!
Piyush Kumar
Piyush Kumar 2024-6-20
Hi,
I am assuming you followed the installation steps mentioned here.
First do ssh and check if daqhat is installed successfully or not -
sshCommand = 'ssh id@ipaddress daqhats_version';
Other troubleshooting steps you may try -
1. Explicitly specify the full path to the Python executable in your SSH command to ensure that the correct Python version is used:
sshCommand = 'ssh id@ipaddress "/full/path/to/python3.11 /home/mcc128/Desktop/RaspTest"';
Replace /full/path/to/python3.11 with the actual full path to the Python executable you intend to use (e.g., /usr/bin/python3.11).
2. The issue might be related to environment variables not being set properly when the SSH session is initiated from MATLAB. Environment variables that are crucial for Python to locate installed modules might not be loaded in non-interactive SSH sessions.
Source Profile: You can explicitly source the profile or the script that sets up the environment variables before executing your Python script:
sshCommand = 'ssh id@ipaddress "source /home/pi/.profile && python3.11 /home/mcc128/Desktop/RaspTest"';
3. Ensure that the user you're SSHing as has the necessary permissions to access the Python executable, the daqhats module, and the script itself.
4. If you have multiple python versions installed, the daqhats library might be installed as part of a Python 2 environment but is needed in a Python 3 environment currently. If daqhats needs to be available for Python 3, follow the installation process targeting Python 3. Since the library might not be available via pip, you'd typically follow the manual installation process provided by the daqhats GitHub repository. This process involves cloning the repository and running the installation script with Python 3 support selected when prompted.
If these steps do not help, share the script, python version installed, command used to install the module. Let me know if you have installed python3 as a virtual environment.

请先登录,再进行评论。

更多回答(1 个)

Animesh
Animesh 2024-6-18
Hi Navina,
As per your provided description and files, I feel the potential causes for such issue can be:
  1. Path Issues: The script or the Python executable might not have the appropriate permissions, or there could be a typo in the path to the Python file. To resolve thisVerify the path to your Python script (/home/mcc128/Desktop/RaspTest) is correct and that the file has executable permissions. You can set executable permissions using “chmod +x /path/to/your/script.py” on the Raspberry Pi. Also, ensure that the user you're SSHing as (id in your placeholder) has the necessary permissions to access and execute the script.
  2. Network Issues:If there's a network issue or the IP address or hostname of the Raspberry Pi is incorrect, the SSH command might attempt to connect indefinitely. To resolve this: Double-check the Raspberry Pi's IP address and ensure that your network allows SSH connections to it. Also, ensure that the Raspberry Pi's SSH server is running and accessible.
  3. if everything else seems correct, consider testing the SSH command outside MATLAB (e.g., in a terminal or command prompt) to ensure it works as expected. To test SSH Command Outside MATLAB: Open a terminal or command prompt on your machine and run the SSH command directly,
ssh id@ipaddress "python3.11 /home/mcc128/Desktop/RaspTest"
Replace id and ipaddress with your actual username and IP. This can help isolate whether the issue is with the SSH setup or how MATLAB executes the command.
By reviewing each of these potential issues, you should be able to identify and resolve the problem causing MATLAB to hang when trying to execute your Python script on the Raspberry Pi remotely.
I hope this helps!
Animesh
  1 个评论
Navina Rajoo
Navina Rajoo 2024-6-20
Hi Animesh, thank you for your response. The problem was that the SSH connection was password protected and matlab was waiting for the user to enter a password, but nothing ever enters it. It worked when I set up authentication to allow password-less SSH connection!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by