Code covered by the BSD License  

Highlights from
SSH From Matlab (updated) + SFTP/SCP

4.44444

4.4 | 9 ratings Rate this file 57 Downloads (last 30 days) File Size: 10.3 KB File ID: #27999

SSH From Matlab (updated) + SFTP/SCP

by David Freedman

 

23 Jun 2010 (Updated 02 Jun 2011)

Run commands on a remote machine from a Matlab session, via a secure shell.

| Watch this File

File Information
Description

If you need to access a remote machine from your Matlab session (for near-real time data transfer etc...) this set of functions allows you to programmatically send a single command and obtain the return values as a cell array.

Added SFTP function that is compatible with this library. Changed to use Matlab's binary read operations. Tested and achieved over 1.2MB sustained transfer rate.

SCP functionality has been included to transfer files back locally.

Now supports public key authentication.

This requires the open-source Ganymed SSH-2 for Java that is freely available online.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
SSH From Matlab, SCP/SFTP from Matlab
This submission has inspired the following:
SSH, SFTP/SCP from Matlab with Public Key Authentication, SSH/SFTP/SCP For Matlab (v2)

MATLAB release MATLAB 7.9 (2009b)
Other requirements Ganymed SSH-2 for Java library http://www.cleondris.ch/opensource/ssh2/
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (56)
28 Jun 2010 Samuel Hurley

Looks like this could be very useful, especially for initiating parallel processing jobs on a cluster of servers.

One quick suggestion, try to be careful to not include the MaxOS hidden files (__MACOSX, .DS_Store, etc) in the zip file. Kinda annoying to go thru and clean all of those out. Thanks!

13 Jul 2010 Min Kim

Thank you very much! Very useful.

One question: is it possible for sftpfrommatlab.m to download file (remote to local) instead of uploading (local to remote)? So far I could not do this. Thanks!

17 Jul 2010 David Freedman

Hi, I removed the hidden MacOS files. Also to transfer files back locally, I added the SCP function. I use this to transfer files back locally.

19 Jul 2010 Min Kim

Thanks a million!

18 Aug 2010 Min Kim

I really appreciate the version, and I did learn a lot about using the java package to control SSH. Again, thank you very much,

I have another question though. I've been trying to use this file to connect to a host, and then trying to ssh into another remote (only connected to the aforementioned host). The problem is that whenever the server's response is a question (e.g. RSA key question, or password), the sshfrommatlabissue does not grab the question (it's just an empty string), and there's no way to issue an answer.

So for example, if I connect to a server using "sshfrommatlab", and then use "sshfrommatlabissue(conn, 'ssh 155.226.32.136')"
, I only get back an empty string as result, and I can't answer the password challenge. Would there be any way to accomplish this?

(I've looked at the code, and it seems like "sshfrommatlabissue" always close session...maybe that's the reason? I would really appreciate any help! )

18 Aug 2010 Min Kim

FYI, I played around a little bit, and figured out that whenever I try to ssh to another remote, I get stderr that says

Pseudo-terminal will not be allocated because stdin is not a terminal.
Permission denied, please try again.
Permission denied (publickey,password,keyboard-interactive).

19 Aug 2010 Ronald

This works great for running defaul commands, but I noticed that I am missing all of my custom environment variables when creating an SSH session from MATLAB. Do you know why some of the environment variables are being dropped?

Note: I am able to use PuTTY on the same machine and get the full list of environment variables.

31 Aug 2010 Aurelien Queffurust

Very nice tool. Tested successfully with R2010a after adding ganymed-ssh2-build251beta1.jar in my java path .
Once I entered the hostname , login and password of the Linux remote machine using sshfrommatlab.m I was able to run basic commands like "ls" or "pwd" from my MATLAB windows machine .

Now I am trying to find a way to run a standalone ( GUI executable created with MATLAB Compiler) remotely via ssh . Could you tell me what are the exact limitations of sshfrommatlabissue.m?

Thanks !

Aurélien

11 Sep 2010 David Freedman

Ronald, the issue stems from the fact you are not getting a bash (or tcsh) shell, and are therefore not getting any of the typical variables. This was a problem for me too, and I had to create a file that "exports" the variables, and after I log in, I include a "source" command before I issue any statements. It's kinda a pain, but that's the way it has to be. You may want to check the FAQ I'm posting in the next reply.

11 Sep 2010 David Freedman

Min, I would recommend you look into using public/private keys because otherwise I don't know how you can possibly enter the password with ganymed-ssh2. You may want to look into the suggestions posted on the following link, Problem 2: Can't enter SSH password. http://pentestmonkey.net/blog/ssh-with-no-tty/

Ronald, the issue stems from the fact you are not getting a bash (or tcsh) shell, and are therefore not getting any of the typical variables. This was a problem for me too, and I had to create a file that "exports" the variables, and after I log in, I include a "source" command before I issue any statements. It's kinda a pain, but that's the way it has to be. You may want to check the FAQ I'm posting in the next reply.

Aurelien, Thanks for the rating, I'm still learning the limitations myself. They are basically the same limitations of ganymed-ssh2. You should check out the FAQ at http://www.cleondris.ch/opensource/ssh2/FAQ.html

21 Sep 2010 Philip Mewes

Hi,
very nice tool. I need to specify a proxy server (socks 5) and a port number to connect to a ssh server because I'm inside a company network. Putty e.g. give you the possibility to specify a proxy.
Matlab itselfs only allows to configure proxys for Internet-connection, but not for ssh

Philip

23 Sep 2010 Yasel Couce

Excellent tool... Thanks!

13 Oct 2010 Dan

Hi: I am having some trouble. I am able to get the connection and issue an 'ls' command with the result being a listing of the contents of my home directory, however, when trying to 'cd' to any other directory I get an empty result and when I issue a subsequent 'ls' command I still get home contents. For example:

[conn,res] = sshfrommatlabissue(conn, 'ls'); disp(res);
[conn,res] = sshfrommatlabissue(conn, 'cd ..'); disp(res);
[conn,res] = sshfrommatlabissue(conn, 'ls'); disp(res);

This results in the same listing before and after the cd command.

I am running off a Mac OSX Matlab 2009b version. Thanks.

13 Oct 2010 David Freedman

Hi Dan, unfortunately you can't use the command in that way. If you want to accomplish the current task I recommend you do

[conn,res] = sshfrommatlabissue(conn, 'cd ..; ls'); disp(res);

cd won't return anything (unless there's an error) so you should get the response from the ls command.

22 Oct 2010 Tomas S

Hi, I have some trouble with using sshfrommatlab.m. I am not able to make the connection with remote host in my local network. When I enter "channel = Connection(hostName)" result is "channel = ch.ethz.ssh2.Connection@b243d7" - I think this is OK. But executing of next command "channel.connect()" gives error:

??? Java exception occurred:
java.io.IOException: There was a problem while connecting to PETER-NTB:22

at ch.ethz.ssh2.Connection.connect(Connection.java:753)

at ch.ethz.ssh2.Connection.connect(Connection.java:543)

Caused by: java.net.ConnectException: Connection timed out: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(Unknown Source)

at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

at java.net.PlainSocketImpl.connect(Unknown Source)

at java.net.SocksSocketImpl.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at ch.ethz.ssh2.transport.TransportManager.establishConnection(TransportManager.java:342)

at ch.ethz.ssh2.transport.TransportManager.initialize(TransportManager.java:450)

at ch.ethz.ssh2.Connection.connect(Connection.java:697)

... 1 more

I am sorry, it is so long log... but after two-day thinking and trying I still cannot find how to make connection. I am sure that hostname, username and password are right. I tried use IP address (private 192.168.1.4) instead of hostname but result was the same. Is here anyone who could help me?

10 Nov 2010 Dirk

Hi,
I've the same problem as Tomas S. Perhaps it is a Matlab 2010a Problem. Because it works fine for me with Matlab 2006b.
Dirk

11 Nov 2010 Jonathan

Hi,

I am able to use SFTP functionality with password authentication, but not with public key authentication. Can some one confirm for me that this is indeed a limitation of the code, and not an error on my part? If so, are there plans to add this functionality?

Thanks for the great tool!
Yoni

14 Dec 2010 Clark Gee

Hi, I am experiencing the same problem as "Tomas S".
I have tried different jar files:
1) trilead-ssh2-build213.jar
2) ganymed-ssh2-build250.jar
Added each one (at a time) to classpath.txt and restarted matlab7b each time a change was made.
Situation: I am trying to connect to a NASA ftp site that I can connect to via WS-FTP, but would like to use Matlab. I have a site, username, and password that work using WS-FTP.
Configuration: Windows XP Pro(32 bit), MatLab7b

If I call sshfrommatlab directly using the user,site,pass fields correctly, I get a response:
??? Error using ==> sshfrommatlab at 48
Error: SSHFROMMATLAB could not find the SSH2 java package

Diving into the sshfrommatlab.m file ...

I then entered commands manually. Here is what I get:
>>import ch.ethz.ssh2.*
>>channel = Connection(hostName)

channel = ch.ethz.ssh2.Connection@1ac8e09

>>channel.connect()
??? Java exception occurred:
java.io.IOException: There was a problem while connecting to oceans.gsfc.nasa.gov:22

at ch.ethz.ssh2.Connection.connect(Connection.java:753)

at ch.ethz.ssh2.Connection.connect(Connection.java:543)

Caused by: java.net.ConnectException: Connection timed out: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(Unknown Source)

at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

at java.net.PlainSocketImpl.connect(Unknown Source)

at java.net.SocksSocketImpl.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at ch.ethz.ssh2.transport.TransportManager.establishConnection(TransportManager.java:342)

at ch.ethz.ssh2.transport.TransportManager.initialize(TransportManager.java:450)

at ch.ethz.ssh2.Connection.connect(Connection.java:697)

... 1 more

Sorry the message is so long...
I realize that there must be some simple thing I have not set, but have been unable to recognize it, yet. Any help here would be most appreciated.
Cheers,
Clark

14 Dec 2010 David Freedman

First Thomas, I have no idea what's causing the problem. You should verify that you can ssh to this local machine.

There is no private/public key support at the moment, but that is a limitation of the matlab code, not the SSH library.

Finally, Clark, this is an SSH/SFTP tool and it appears to me that you're trying to SSH to the host. There is not FTP support in this version. This is either built into Matlab, or passive mode support is provided by another matlab central upload.

15 Dec 2010 Clark Gee

David, thanks for the quick reply.
I will go look for passive mode FTP.
Clark

20 Dec 2010 Erika

Jonathan, to use sshfrommatlab.m with a private/public key you can try this one:

remove/comment the following lines:
% isAuthenticated = channel.authenticateWithPassword(userName,password);
 % if(~isAuthenticated)
  % error...
   % (['Error: SSHFROMMATLAB could not authenticate the',...
   % ' SSH connection...']);
 % end

and write instead:

id_dsa = sprintf('<your_dsa_or_rsa_key>')
  isAuthenticated = channel.authenticateWithDSA(userName,id_dsa, password)

For me it works.
Erika

20 Mar 2011 Tam Chu

I tried to run sshfrommatlab and ran into the following problem about "authentication method Password not supported by server at this stage".

But I was able to login with password through command line. Not sure what the problem is. Has anyone else experienced the same problem?

Tam

??? Java exception occurred:
java.io.IOException: Password authentication failed.
at ch.ethz.ssh2.auth.AuthenticationManager.authenticatePassword(AuthenticationManager.java:328)
at ch.ethz.ssh2.Connection.authenticateWithPassword(Connection.java:311)
Caused by: java.io.IOException: Authentication method password not supported by the server at this
stage.
at ch.ethz.ssh2.auth.AuthenticationManager.authenticatePassword(AuthenticationManager.java:298)
... 1 more

Error in ==> sshfrommatlab at 53
  isAuthenticated = channel.authenticateWithPassword(userName,password);
 

18 May 2011 Etienne

Hi David

The tool is working very well, but now I am trying to run a Korn shell script to no avail. I noticed the comments above about exporting the environment variables etc. Can you please explain how you do this?

Thanks

Etienne

18 May 2011 David Freedman

Hi Etienne, I'm not very familiar with the Korn Shell, but I believe you can source the startup variables by connecting and running,

. .profile
or
. .kshrc

first, i.e., in your command string enter

". .profile; commandtorun"

01 Jun 2011 Etienne

Hi David

I managed to get the commands to work, but woudl like to be able to launch a batch command from SSH. I noticed that you can use a -f option in ssh. Is this functionality available in your routines. I have tried the nohup option and the & operator at the end of the command, to no avail.

01 Jun 2011 David Freedman

Hi Etienne, I don't quite understand your question. -f appears to put the ssh command in the background. There is no similar command when using Ganymed SSH-2 for Java.

You should be able to launch a batch command from SSH by issuing the correct command, i.e. ./command.sh. You should be able to put this script in the background.

01 Jun 2011 Etienne Coetzee

Hi David

I am launching a batch job on a unix machine. When I launch it from this program it hangs up because there are so many jobs I am sending. I would rather like to launch the job in the background and then check periodically whether it has finished. I tried all sorts of syntax, but it just doesn't seem to work. I will keep on trying.

01 Jun 2011 David Freedman

OK, I added a function that doesn't wait for a response. Plus I added Jonathan's request and Erica's suggestion. File should update shortly.

02 Jun 2011 Etienne Coetzee

Thanks. I will wait for the new additions and then give you a 5 star rating. Your programs have been incredibly useful.

02 Jun 2011 Etienne

Hi David. I noticed that you made the update, but I am not sure where you refer to the function that you are talking about.

02 Jun 2011 David Freedman

oops, the file was not updated correctly, you should find it shortly. The file you're looking for is sshfrommatlabissue_dontwait.m

02 Jun 2011 Etienne Coetzee

Thanks David. Works a treat.

28 Jun 2011 Koenraad

I was using scptomatlab to copy multiple files.

 localFolder= c:\temp\
 remotefilename = /tmp/*

where /tmp/ on the remote machine has multiple files (more than 100)

but only one file is copied to my c:\temp

Could it be that the tool does not support to copy multiple files?

29 Jun 2011 Koenraad

I found a work-around:

command ='cd /tmp; ls';
[channel, result] = sshfrommatlabissue(channel,command);
localFolder = 'c:\temp\'
for i = 1:size(result)
    remotefilename = ['/tmp/' cell2mat(result(i))];
     scptomatlab(userName,hostName,password,localFolder,remotefilename);
end;

30 Jun 2011 David Freedman

Hi Koenraad, unfortunately, the SCP library doesn't support wildcards. I would ...
nevermind, your workaround is what I would suggest. Nice work!

06 Jul 2011 Mithila Nagendra

Hi David,

I found your tool very useful and it literally saved me weeks of work... thanks a lot for sharing it!

I used your tool awhile back (folder name "sshfrommatlab_12") - back then I had to add the public key authentication for SFTP/SCP to your package for my use, but never got around to uploading the changes. I noticed that this feature is now available in the new package, but only for running commands remotely through SSH. I have a version of your SFTP/SCP functions that support public key authentication. I will upload my files, thought I'll let you know about it.

Thanks!

20 Sep 2011 Richard Crozier

Great tool thanks, it work with the beta 251 build which I accidentally downloaded too.

25 Sep 2011 Ernest

I also have the following error...

>> sftpfrommatlab('user','host','pswd','c:\GPS_POS_1.dat','/dir/dir/dir/')
??? Java exception occurred:
ch.ethz.ssh2.SFTPException: Failure (SSH_FX_FAILURE: An error occurred, but no specific error code exists to
describe the failure.)

at ch.ethz.ssh2.SFTPv3Client.openFile(SFTPv3Client.java:1198)

at ch.ethz.ssh2.SFTPv3Client.createFile(SFTPv3Client.java:1071)

at ch.ethz.ssh2.SFTPv3Client.createFile(SFTPv3Client.java:1052)

Error in ==> sftpfrommatlab at 67
localf=sftpcl.createFile(remotefilename);

I hope you could shed some lite regarding this.

25 Sep 2011 Ernest

Hi David, I am having a problem using the
sftpfrommatlab. I used the following commands the I get the errors bellow:
>> h = sshfrommatlab('user','host','pswd');
>> sftpfrommatlab('user','host','pswd','C:\GPS_POS_1.dat','user@user-dir/dir/dir/')

??? Java exception occurred:
ch.ethz.ssh2.SFTPException: No such file (SSH_FX_NO_SUCH_FILE: A reference was made to a file which does not
exist.)

at ch.ethz.ssh2.SFTPv3Client.openFile(SFTPv3Client.java:1198)

at ch.ethz.ssh2.SFTPv3Client.createFile(SFTPv3Client.java:1071)

at ch.ethz.ssh2.SFTPv3Client.createFile(SFTPv3Client.java:1052)

Error in ==> sftpfrommatlab at 67
localf=sftpcl.createFile(remotefilename);

Do you have any idea whatcould be the source of such error?

26 Sep 2011 David Freedman

Hi Ernest. I think I see the issue with the two previous posts.
1) You cannot use user@user-dir/dir/dir/. The user information is provided in the first input variable.
2) I think you need to provide the entire path for the remote file, i.e. /dir/dir/dir/GPS_POS_1.dat

Hope that helps.

25 Oct 2011 Richard Crozier

Because of the nested try catch in sshfrommatlab.m, the error 'SSHFROMMATLAB could not find the SSH2 java package' is always thrown, even if the actual error is that the connection was unsuccessful. This could be fixed with something like:

%
% Build the connection using the JSch package
%
  try
    import ch.ethz.ssh2.*;
    try
      channel = Connection(hostName);
      channel.connect();
    catch
      error('SSHFRMMATLAB:noconnection', ...
            ['Error: SSHFROMMATLAB could not connect to the remote machine %s ...'],...
            hostName);
    end
  catch
      
      LE = lasterror;
      
      if isequal(LE.identifier, 'SSHFRMMATLAB:noconnection')
          rethrow(LE);
      else
          error('Error: SSHFROMMATLAB could not find the SSH2 java package');
      end
      
  end

10 Nov 2011 Nathan Orloff

Hi David,

I think I might also be confused about what this program actually does can you explain your answer to Clark? Maybe I misunderstand the function. I want to remotely connect to a server and put a file in a folder on the host from a computer running matlab.

Thanks,

Nate

11 Nov 2011 Nathan Orloff

Is there a way to change the port?

12 Nov 2011 Nathan Orloff  
28 Nov 2011 Dominic

I tried to execute script using sshfrommatlabissue(channel,''./script -parameter); but it doesn't work.
What's the problem here?

Thank you!

28 Nov 2011 Dominic

I meant sshfrommatlabissue(channel,'./script -parameter')

28 Nov 2011 David Freedman

Hi Dmoninic, I cannot tell what's wrong from your above statement. Have you verified that your connection works with a simple "ls"? Also, ssh2 does not open a bash or csh, meaning any parameters you expect (.bash_profile) must be setup interdependently. i.e. "source ~/.bash_profile; ./script-parameter"

15 Dec 2011 Sophie

I keep getting an error using sshfrommatlab:

Error using sshfrommatlab (line 48)
Error: SSHFROMMATLAB could not find the SSH2 java package

This is using MatlabR2011b and using either the ganymed build 250 or 251beta. I have tested with a couple of remote hosts that I have no trouble ssh-ing into.

Any tips on obvious places I might be going wrong?

15 Dec 2011 David Freedman

Hi Sophie,

Have you tried running
sshfrommatlabinstall(1)
once
and then

sshfrommatlabinstall
every time you restart matlab, but before you run sshfrommatlab
?

15 Dec 2011 Sophie

Hi David,
Thanks for your super quick response. Yes I've tried that. When I tried the suggestion above based on the nested try catch, the error changed to a connection error. Although I have no problem with connecting simply with unix('ssh HOST').

Thanks again. Sophie

16 Dec 2011 Sophie

Got it sorted, thanks! Works fine now.

22 Dec 2011 Hamad

Hi David , this is really an interesting library. I am struggling to start it.
Can you anybody tell me how to use it? I mean in all the m files we have to change the username and password and hostname?
1. only in the arguments or also in the body.
2. what is the actual sequence for execution?

i am engineering student but i have to copy files to remote sever and then to start the simulations.

I would be waiting for your kind responses

regards,
Hamad

23 Feb 2012 Jurek Dziewierz

on trying to use the sftp, i get:

 sftpcl = SFTPv3Client(channel)
Java exception occurred:
java.io.IOException: Illegal sftp packet len: 757951599

at ch.ethz.ssh2.SFTPv3Client.receiveMessage(SFTPv3Client.java:245)

at ch.ethz.ssh2.SFTPv3Client.init(SFTPv3Client.java:861)

at ch.ethz.ssh2.SFTPv3Client.<init>(SFTPv3Client.java:108)

at ch.ethz.ssh2.SFTPv3Client.<init>(SFTPv3Client.java:119)

please help . . . .

03 Mar 2012 Alex

Jurek, it looks like you are trying to upload a 722 MB file, that could be your issue.

01 Jun 2012 Sébastien Laberge

I am trying to connect to an external server to call parallel simulations directly from a matlab GUI, and then fetch the results back to my computer. The sshfrommatlab package works well to make an ssh connexion to another personal computer, but I got the following error when trying to connect to the server:

 Error using ch.ethz.ssh2.Connection/authenticateWithPassword
Java exception occurred:
java.io.IOException: Password authentication failed.
at ch.ethz.ssh2.auth.AuthenticationManager.authenticatePassword(AuthenticationManager.java:318)
at ch.ethz.ssh2.Connection.authenticateWithPassword(Connection.java:311)
Caused by: java.io.IOException: Authentication method password not supported by the server at this stage.
at ch.ethz.ssh2.auth.AuthenticationManager.authenticatePassword(AuthenticationManager.java:288)
... 1 more

Error in sshfrommatlab (line 53)
  isAuthenticated = channel.authenticateWithPassword(userName,password);

I don't understand. I am sure I use the correct username, host name and password. I usually don't get this error when I try to connect via the computer's common line using the ssh command and a password. I am currently working on mac OS X and the server uses linux (SUSE).

Thank you for your help

01 Jun 2012 Sébastien Laberge

I just found a solution. I tried to connect with sshfrommatlab_publickey_file and it worked. That's bizarre. It looks like when I use the ganymed java package instead of the system command line, the server only accepts to be accessed with a ssh-key and refuses the old password method.
But it looks like I will have to make my own m files to use transfer files because the sftpfrommatlab and scptomatlab functions use passwords to connect...

Please login to add a comment or rating.
Updates
29 Jun 2010

Added SFTP function. Also removed extraneous files.

17 Jul 2010

Added SCP functionality

01 Jun 2011

Now supports public key authentication. Also one can send commands without waiting for a response. Plus, an updated sshfrommatlabinstall.

02 Jun 2011

File updated.

Tag Activity for this File
Tag Applied By Date/Time
communications David Freedman 24 Jun 2010 10:03:46
data export David Freedman 24 Jun 2010 10:03:46
import David Freedman 24 Jun 2010 10:03:46
ssh David Freedman 24 Jun 2010 10:03:46
sftp David Freedman 30 Jun 2010 13:34:57
thank you very much for this very useful one question is it poss Min Kim 13 Jul 2010 17:42:10
scp David Freedman 19 Jul 2010 10:34:32
scp Mark Matthews 17 May 2011 08:41:33
scp Durgesh 20 Mar 2012 08:53:39

Contact us at files@mathworks.com