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

74 views (last 30 days)
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

Ameer Hamza
Ameer Hamza on 22 Apr 2018
Edited: Ameer Hamza on 22 Apr 2018

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
mostafa haggag
mostafa haggag on 22 Apr 2018
Edited: mostafa haggag on 22 Apr 2018
I am very grateful for your help and guidance
thank you so much
yours
Mostafa haggag from egypt
HiWave
HiWave on 14 Jan 2022
"Using system for doing ssh inside a script is not a good idea" .... can you explain why?

Sign in to comment.

More Answers (3)

Marcus Lundmark
Marcus Lundmark on 18 Mar 2019
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.

Marcus Lundmark
Marcus Lundmark on 18 Mar 2019
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?

Marcus Lundmark
Marcus Lundmark on 18 Mar 2019
Edited: Marcus Lundmark on 21 Mar 2019
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

Community Treasure Hunt

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

Start Hunting!