Using matlab from local computer to connect and transfer files to remote computer

381 ビュー (過去 30 日間)
Sourabh Rao
Sourabh Rao 2017 年 7 月 26 日
回答済み: Ameer Hamza 2018 年 4 月 22 日
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

回答 (2 件)

Sailesh Sidhwani
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:

Ameer Hamza
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.

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by