Using matlab from local computer to connect and transfer files to remote computer
234 次查看(过去 30 天)
显示 更早的评论
Hello all,
I am trying to write a script to login to a remote computer, transfer the local files, and run some programs on the remote computer. I am using a linux system and I am able to login to the remote computer (also linux) using the unix('ssh -Y username@hostname') matlab command, but once I login, the commands written in the editor after this do not execute. For example, I would like to execute the print working directory (pwd) command after logging in to the remote computer.
Script I have written in the editor for the login and pwd command:
unix('ssh -Y username@hostname');
unix('pwd');
What the matlab command window shows me:
>> script
username@hostname's Password: *enter password in command window*
username@hostname-login2:~>
Now the unix('pwd') command does not seem to execute in the username@hostname-login2:~>
To execute the pwd command, I would have to go to the command window and type it. My question is how do I write the script in the matlab editor so that the pwd command is executed from the editor itself when running the script?
Let me know if you need additional details. Thanks in advance for your help.
Regards,
Sou
0 个评论
回答(2 个)
Sailesh Sidhwani
2017-7-28
The "system" function lets you execute terminal commands on your local machine from MATLAB and can, therefore, be used in a MATLAB script to execute terminal commands. To run these commads on a remote machine, you would first log onto that machine using the terminal command "ssh," called using the "system" function.
The following code snippet illustrates how you would establish a remote connection, execute terminal commands, and close the connection using MATLAB:
% Open SSH connection to test machine
command = 'ssh username@remotemachine';
[status, cmdout] = system(command);
% That will not work if server and client are not set up for password-less login
% Execute commands here
command = 'ls';
[status, cmdout] = system(command);
% 'status' is 0 if the command was successfully executed
% ~0 otherwise
% 'cmdout' is the output of executing the command
% Close connection once tests are over
[status, cmdout] = system('exit');
Note that using SSH within MATLAB requires password-less login. If you try to connect as usual (using a password), you will not be able to enter the password through MATLAB and MATLAB will stay in a busy stay as it is waiting for the command to be completed. The command cannot be completed because it requires a password which you cannot enter.
If you check doc help for system command, it tells you that "This function is interchangeable with the DOS and UNIX functions. They all have the same effect."
help system
You can read more details about the "system" function in the online documentation:
1 个评论
Siva Meduri
2024-6-21
I am not able to run this command . It does not execute system(command). I am able to do ssh without password from shell. Please suggest how to use SSH with matlab. ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1719826/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1719826/image.png)
Ameer Hamza
2018-4-22
@Sailesh answer works well for password-less login. In a general scenario, you can use File exchange submission for ssh login with a password. You can refer to detailed examples included in the package. You can also refer to this example.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!