is it possible to send data from the matlab to run in the terminal ??

hello i hope everybody is fine i have a question i usually do the following in the terminal and i want to do it using matlab the following is: i log in into another computer and i am asked for the password to i enter it i then change the directory of the new computer and i run a command . the terminal commands are :
1)ssh -X xxxx1234@xxxx.yyy.yyy-yyyyyyyyyyy.yy ( to login into my account on the other computer)
2)xxxxxxxxxxx (for my password)
3)cd palm/currentversion
4)mrun -d example neutral -b -h xxxxx -K parallel -X 48 -T 24 -t 1800 -q xxxx.p -r "d3# ts# pr# xy#"
5)i need to send y character for the simulation to run
6)exit
what i need to do is to automate the procedure so that I can run this command through a script in Matlab this is what i was able of doing till now to log in but it failed
if true
sayyes_file = [tempname() 'xxxxxxxx'];
fid = fopen(sayyes_file, 'wt');
fprintf(fid, 'password');
fclose(fid);
cmd_pattern = ('"ssh -X xxxx1234@xxxx.yyy.yyy-yyyyyyyyyyy.yy" < "%s"');
cmd = sprintf(cmd_pattern, sayyes_file);
[status] = system(cmd)
end
this part only the part to login but it does not want to work i donot know what i am not doing correctly thank you sooo sooo much grateful for your help

 Accepted Answer

Using system for doing ssh inside a script is not a good idea. You can use this file exchange submission to establish an ssh connection to a machine from MATLAB. You can refer to detailed examples included insides the referred file exchange submission. For your case, you can run

ssh2_simple_command(hostname, username, password, 'cd palm/currentversion; ...
mrun -d example neutral -b -h xxxxx -K parallel -X 48 -T 24 -t 1800 -q xxxx.p -r "d3# ts# pr# xy#"');

this will run the command, print the terminal result and close the connection. If you want to main the connection you will need advanced setup as given in example file.

7 Comments

thank you so much for your answer i have a question how can i send a 'y' character in the above command ? i need to send yes or y after running the mrun command

This can be done by using standard UNIX system utility yes and pipelines . For more details, you can refer here. In short, you can write your command like this.

yes y | mrun -d example neutral -b -h xxxxx -K parallel -X 48 -T 24 -t 1800 -q xxxx.p -r "d3# ts# pr# xy#"
so this is the command I write, I am able to login successfully but the problem is it is not running the run command is not running if there is an error it usually appears in the terminal is it possible to make results appear in Matlab to know the error? or am I doing something wrong in the command down could you please check it ?? what i normally do in the terminal I log in change directory to cd palm/current_version then I do the mrun command then the terminal asks for confirmation by pressing yes or no
could you please check if i am writing the correct commands ? ignoring ofcourse the xxxx
ssh2_simple_command('xxxxxxxx', 'xxxxxxxx', 'xxxxxxxxxxx', ... 'cd "cd palm/current_version;yes y | mrun -d doubleturbine_afy8 -b -h lcxxxx -K parallel -X48 -T 24 -t 1000 -q xxxx.p -r " d3# ts# pr# xy#"');

Sorry! I just realized, there was a typing mistake in the original command I pasted. I have edited it now. For reference, 4th argument is the string of shell commands you want to execute from MATLAB.

Also if you want to get the terminal output, you will need to see the examples provided with the package you downloaded from file exchange. For reference, I am also pasting the correct command here,

ssh2_simple_command('xxxxxxxx', 'xxxxxxxx', 'xxxxxxxxxxx', ... 
'cd palm/current_version;yes y | mrun -d doubleturbine_afy8 -b -h lcxxxx -K parallel -X48 -T 24 -t 1000 -q xxxx.p -r " d3# ts# pr# xy#"');
I am very grateful for your help and guidance
thank you so much
yours
Mostafa haggag from egypt
"Using system for doing ssh inside a script is not a good idea" .... can you explain why?

Sign in to comment.

More Answers (3)

Will sshpass help?
Execute ssh with password authentication via windows command prompt
The sshpass utility is meant for exactly this. First, install sshpass by typing this command:
sudo apt-get install sshpass
Then prepend your ssh/scp command with
sshpass -p '<password>' <ssh/scp command>
This program is easiest to install when using Linux.
User should consider using SSH's more secure public key authentication (with the ssh command) instead.
This works on a Linux machine.
First install sshpass as shown above.
Create a shell file containing one command or an entire set of commands on the remote machine.
On the local machine, run the following:
sshpass -p "<password>" ssh user@remotehost 'sh shell_filename.sh'
or
sshpass -p "<password>" ssh user@ipaddress 'sh shell_filename.sh'
Now the trick is how to get this to work with the MATLAB 'system' command?
Here's a command that works on the MATLAB Command Window.
In this example, I am commanding a remote Raspberry Pi at 192.168.86.1 to collect data from an RTL-SDR.
% call the shell command to start a new rtlsdr collection
system("sshpass -p 'raspberry' ssh pi@192.168.86.1 'sh rtl.sh'");
Yeah, I'm still using the default username and password.
You can also use the ./ operator.
% call the shell command to start a new rtlsdr collection
system("sshpass -p 'raspberry' ssh pi@192.168.86.1 './rtl.sh'");
To be more secure, you are advised to change the password of your Raspberry Pi.
The shell file on the remote Raspberry Pi is named rtl.sh
(The shell file must be made executable.)
#!/bin/bash
# run rtl_sdr (full path must be provided)
echo "----------run RTL-SDR program"
/home/pi/rtl-sdr/build/src/rtl_sdr -f 107.7e6 -n 1e6 -s 2.048e6 "iq_data_"$(date +"%Y%m%d_%H%M00.dat")
To make a file executable
chmod +x filename.sh

Categories

Find more on Communications Toolbox 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!