Using matlab from local computer to connect and transfer files to remote computer
315 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Sailesh Sidhwani
on 28 Jul 2017
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 Comment
Siva Meduri
on 21 Jun 2024
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.
Ameer Hamza
on 22 Apr 2018
@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 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!