Code covered by the BSD License  

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

4.6875
4.7 | 16 ratings Rate this file 90 Downloads (last 30 days) File Size: 600 KB File ID: #27999 Version: 1.7

SSH From Matlab (updated) + SFTP/SCP

by

David Freedman (view profile)

 

23 Jun 2010 (Updated )

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 and is now included in the zip file.

Acknowledgements

Ssh From Matlab and Scp/Sftp From Matlab inspired this file.

This file inspired Ssh, Sftp/Scp From Matlab With Public Key Authentication and Ssh/Sftp/Scp For Matlab (V2).

MATLAB release MATLAB 7.9 (R2009b)
Other requirements Ganymed SSH-2 for Java library INCLUDED IN ZIP FILE
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (84)
25 Aug 2015 David Freedman

David Freedman (view profile)

Hi Nathan, You say that you can use pysftp, but is that for sftp? See if sftpfrommatlab() works. The error is foreign to me, but I would guess that it is probably happening because the server is not configured for ssh2. (Caused by: java.io.IOException: The server denied the request.)

Comment only
24 Aug 2015 Nathan Williams

i'm getting the following error when I execute a simple command like:
command_output = ssh2_simple_command(HOSTNAME,USERNAME,PASSWORD,'ls -la *ninjas*')

Any idea why? Thanks so much David for putting this code out there. I've yet to be able to get this to work from matlab although python's pysftp library works fine.

------------------------------------:

Error using ssh2_main (line 320)
Java exception occurred:
java.io.IOException: The execute request failed.

at
ch.ethz.ssh2.channel.ChannelManager.requestExecCommand(ChannelManager.java:707)

at ch.ethz.ssh2.Session.execCommand(Session.java:248)

Caused by: java.io.IOException: The server denied the request.

at
ch.ethz.ssh2.channel.ChannelManager.waitForChannelSuccessOrFailure(ChannelManager.java:191)

at
ch.ethz.ssh2.channel.ChannelManager.requestExecCommand(ChannelManager.java:703)

... 1 more

Error in ssh2 (line 84)
ssh2_struct = ssh2_main(ssh2_struct);

Error in ssh2_command (line 31)
ssh2_struct = ssh2(ssh2_struct);

Error in ssh2_simple_command (line 40)
[ssh2_struct, command_result] = ssh2_command(ssh2_struct,
command, enableprint);

------------------------------------

Comment only
07 Aug 2015 David Freedman

David Freedman (view profile)

peter, every connection is like a new shell. You must develop your application with this is mind.

Comment only
07 Aug 2015 Peter

Peter (view profile)

Works not quite as expected... maybe I'm doing something wrong.

Try to change from the home directory to a subdirectory and then issue a command to access something in that subdirectory. Won't work because for some reason it switches back to the home directory.

Seems like it's starting a new session with every sshfrommatlabissue() command.

Is someone experiencing a similar behaviour?

Comment only
08 Jan 2015 Jonathan  
20 Aug 2013 David Freedman

David Freedman (view profile)

Hi James, it appears cleondris has stopped hosting the ganymed-library. I have submitted an updated to Mathworks that will include the .zip file with the matlab files so it will no longer need to be downloaded.

It's been a great run cleondris.

Comment only
20 Aug 2013 James

James (view profile)

Perfectly useful tool.

This morning sshfrommatlabinstall(1) is returning the following error:

??? Error using ==> urlwrite at 140
Error downloading URL. Your network connection may be down or your proxy settings improperly
configured.

Error in ==> sshfrommatlabinstall at 40
urlwrite('http://www.cleondris.ch/ssh2/ganymed-ssh2-build250.zip','ganymed-ssh2-build250.zip');

Also, http://www.cleondris.ch/ssh2/ganymed-ssh2-build250.zip generates an error when trying to access it from a brouser.

Thanks again for this great tool!!

James

18 Jul 2013 Adam

Adam (view profile)

great!

08 May 2013 David Freedman

David Freedman (view profile)

Hi Nicholas,

If you can NFS mount the remote computer, that would probably be ideal.

Otherwise, you can continue to use this function, just download the files to your computer to use matlab functions to do your analysis.

Comment only
08 May 2013 Nicholas

Hi, I'm enjoying playing around with this function but I can't get it to do what I want. I was wondering if maybe I should be using an alternative. I've developed a series of scripts to analyse some model outputs and the way the script works is by looking at which directory is the newest and then taking information from the filenames to determine automatically quite a lot of information about the runs I've done. However, I'm now doing some runs on a computer other than my own that I only have remote access to, and, which, doesn't have matlab.

So, is there anyway I can get matlab to see the directories on the remote machine as if it was a local directory so that I can use functions such as dir, textscan and cell2mat to extract the model information from the remote directories?

Any assistance would be greatly appreciated.

Nick

Comment only
07 Mar 2013 David Freedman

David Freedman (view profile)

Hi Matt,

Unfortunately, I'm not familiar with byobu. This link implies you need to run byobu with the "-t" option as the ssh2 package does not provide a tty terminal.

http://stackoverflow.com/questions/474967/automatically-or-more-easily-reconnect-to-a-screen-session-after-network-inter

Does this solve your problem?

Comment only
07 Mar 2013 Matthew

After SSHing into the remote computer, I am trying to run the byobu command so I can run the remote in root. I use the command below:
[conn, res] = sshfrommatlabissue(conn,'byobu') and the result returns ']0;roboard@a-laptop - byobuMust be connected to a terminal.'. I was hoping you could give me some insight as how to run byobu so I can run the remote in root.

Thanks,
Matt

07 Feb 2013 David Freedman

David Freedman (view profile)

Hi Walter, If I understand the problem, you want to look at the result of your commands. I'll remind you that these functions cannot support interactive modes on the command line. But, for getting the response of your command, you can look at the cell array "result" from the command

[channel, result] = sshfrommatlabissue(channel,command).

Thanks.

Comment only
06 Feb 2013 Walter

Walter (view profile)

Hi, great submission!
I use this package to run simple commands on a condor cluster. But there appear to be certain commands which when run using an external SSH tool, print commands to the prompt screen

In particular: condor submitters -username

When I try to run that command from SSH_MATLAB, obviously, it will not give me any feedback, or command response.

Is there a setting somewhere I can adjust, that would allow for me to see the same feedback at my MATLAB Command Window, that I see when I run condor submitters from the SSH GUI?

12 Dec 2012 Roel H

Roel H (view profile)

Hi David,

Thanks for your quick reply!
I was already afraid that this would be the answer, because both hacks you suggest are not possible in my network configuration. Before I found this package, I tried public key identification/scripting but it didn't work. Also, I don't think the server I need to connect through runs matlab, so I'll have to figure out another way or just do it manually.

Greets, Roel

12 Dec 2012 David Freedman

David Freedman (view profile)

Hi Roel,

It is difficult, but I don't think impossible to connect through two machines.

If you're able to run Matlab on the first machine, you should be able to use this package to ssh into the second machine.

Otherwise, you can use public key authentication on the first machine and a script to automatically connect and execute your function.

This hacks are needed because the SSH package for matlab can only execute commands and cannot run interactively.

Comment only
11 Dec 2012 Roel H

Roel H (view profile)

Hi David,

Thanks for writing this awesome package!

I have one question, though.
I need to ssh to a server first and then from the server ssh to the remote machine I want to use. Then, on the remote machine I need to start up my matlab simulations.
My (linux)system environment does not let me automate ssh connections (expect scripts, public keys or similar constructs), so I am using sshfrommatlab to handle the connections.
Therefore my question: Is it possible to make this double ssh connection using the package You have provided?

Thanks very much in advance!
Roel Helsen

Comment only
05 Sep 2012 Carlos

Carlos (view profile)

This files look great! However, when I try to use sshfrommatlab, I get,

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: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 (line 53)
isAuthenticated = channel.authenticateWithPassword(userName,password);

And I am sure that my username, server and password are ok... :(

Comment only
24 Aug 2012 Alex

Alex (view profile)

I needed the -t flag (pseudo-tty) when executing certain commands. Simple modification to sshfrommatlabissue.m made this work:

%
% Send the commands
%
result = {''};
channel2 = channel.openSession();

channel2.requestPTY('vt100', 80, 24, 640, 480, []);

channel2.execCommand(command);

...

26 Jun 2012 Marissa

When I try to execute the scptomatlab function, I get an error message associated with this portion of code:

import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;

which says

??? Error: File: scptomatlab.m Line: 23 Column: 8
Arguments to IMPORT must either end with ".*"
or else specify a fully qualified class name: "ch.ethz.ssh2.SCPClient" fails
this test.

If I add .* to the end of each one and then execute, this error appears:

??? Error using ==> scptomatlab at 45
Error: SCPTOMATLAB could not connect to the remote machine

followed by the host name I’m trying to connect to. Do you know what is going wrong? This code has worked for my coworker, so I know that the hostname must be correct.

Rebecca

Comment only
18 Jun 2012 Murat Belge

For BeagleBoard users out there who is trying to connect to their board from within MATLAB using SSH. The BeagleBoard support package in R2012a has built-in support for SSH connection. The BeagleBoard support package uses "plink.exe" and "pscp.exe" utilities to talk to beagleboard. There is a demo called "Executing Remote Linux Commands" that goes over the API.

To install BeagleBoard downloadable support package, type in "targetinstaller" on the MATLAB command line.

Comment only
14 Jun 2012 Trentt James

Thank you very much David. This helped me out a bunch!

Comment only
13 Jun 2012 David Freedman

David Freedman (view profile)

Hi Trent, this is likely the same problem that Ronald (and maybe Dominic) had in Oct 2010. Modified C/P below.

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 ganymed-ssh2 FAQ at http://www.cleondris.ch/opensource/ssh2/FAQ.html for more info.

Comment only
13 Jun 2012 Trentt James

Hi David. If this helps, the user is trying to connect to a BeagleBoard xM. I am ssh in as root so we should have root privelages. Through other methods of ssh (like putty), I have been able to connect to the remot host and send these commands as well so that should be verified.

Initially, when connected, I sent these commands (which worked correctly):

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

and then I tried to run a program, but I was given a null return and send back to root's home directory. When I first started to run a command, besides trying to shutdown/restart, I tried to run a program with this following command:

[conn,res] = sshfrommatlabissue(conn,'./Program');disp(res);

Once again, I was given a null return. Then I tried to restart with the "shutdown -r now" which I have always done to restart, and a "shutdown -h now" to shutdown. After multiple tries, I was unable to restart the computer. I really appreciate this help!

Comment only
13 Jun 2012 Trentt James

Also, we are ssh in as root so we should have root privelages. Sorry for this late post.

Comment only
12 Jun 2012 David Freedman

David Freedman (view profile)

Hi Trennt, Are you sure the user is authorized to shutdown the computer? Have you verified this command "shutdown -r now" works from the same computer using an ssh2 client? Also, please make sure you have a made a connection object correctly. Have you tried running "hostname" to if you have made a good connection? I expect if you can verify this, you should be able to restart the machine.

I always thought the command was shutdown -r 0

Comment only
12 Jun 2012 Trentt James

Hi!

I am trying to issue a standard command to restart the remote host by using the command " sshfrommatlabissue(connection,'shutdown -r now') ". However, this is not restarting the remote host and I am receving a null return. I would greatly appreciate any help on this, or if this type on command line is not implemented. Thank you!

Comment only
06 Jun 2012 David Freedman

David Freedman (view profile)

Hi Sébastien, you should check out the v2 of this file SSH/SFTP/SCP For Matlab (v2)at the top. It has public key support for sftp and scp functions.

Comment only
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...

Comment only
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

Comment only
03 Mar 2012 Alex

Alex (view profile)

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

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

Comment only
22 Dec 2011 Hamad

Hamad (view profile)

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

Comment only
16 Dec 2011 Sophie

Sophie (view profile)

Got it sorted, thanks! Works fine now.

Comment only
15 Dec 2011 Sophie

Sophie (view profile)

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

Comment only
15 Dec 2011 David Freedman

David Freedman (view profile)

Hi Sophie,

Have you tried running
sshfrommatlabinstall(1)
once
and then

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

Comment only
15 Dec 2011 Sophie

Sophie (view profile)

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?

Comment only
28 Nov 2011 David Freedman

David Freedman (view profile)

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"

Comment only
28 Nov 2011 Dominic

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

Comment only
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!

Comment only
12 Nov 2011 Nathan Orloff  
11 Nov 2011 Nathan Orloff

Is there a way to change the port?

Comment only
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

Comment only
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

Comment only
26 Sep 2011 David Freedman

David Freedman (view profile)

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.

Comment only
25 Sep 2011 Ernest

Ernest (view profile)

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?

Comment only
25 Sep 2011 Ernest

Ernest (view profile)

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.

Comment only
20 Sep 2011 Richard Crozier

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

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!

Comment only
30 Jun 2011 David Freedman

David Freedman (view profile)

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

Comment only
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;

Comment only
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?

Comment only
02 Jun 2011 Etienne Coetzee

Thanks David. Works a treat.

02 Jun 2011 David Freedman

David Freedman (view profile)

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

Comment only
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.

Comment only
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.

Comment only
01 Jun 2011 David Freedman

David Freedman (view profile)

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.

Comment only
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.

Comment only
01 Jun 2011 David Freedman

David Freedman (view profile)

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.

Comment only
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.

Comment only
18 May 2011 David Freedman

David Freedman (view profile)

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"

Comment only
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

Comment only
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);

Comment only
20 Dec 2010 Erika

Erika (view profile)

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

Comment only
15 Dec 2010 Clark Gee

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

Comment only
14 Dec 2010 David Freedman

David Freedman (view profile)

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.

Comment only
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

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

Comment only
10 Nov 2010 Dirk

Dirk (view profile)

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

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?

Comment only
13 Oct 2010 David Freedman

David Freedman (view profile)

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.

Comment only
13 Oct 2010 Dan

Dan (view profile)

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.

Comment only
23 Sep 2010 Yasel Couce

Excellent tool... Thanks!

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

11 Sep 2010 David Freedman

David Freedman (view profile)

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

Comment only
11 Sep 2010 David Freedman

David Freedman (view profile)

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.

Comment only
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

19 Aug 2010 Ronald

Ronald (view profile)

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.

Comment only
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).

Comment only
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! )

Comment only
19 Jul 2010 Min Kim

Thanks a million!

Comment only
17 Jul 2010 David Freedman

David Freedman (view profile)

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.

Comment only
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!

Comment only
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!

Comment only
Updates
29 Jun 2010 1.3

Added SFTP function. Also removed extraneous files.

17 Jul 2010 1.4

Added SCP functionality

01 Jun 2011 1.5

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

02 Jun 2011 1.6

File updated.

20 Aug 2013 1.7

Includes Ganymed library (no longer accessible from (http://www.cleondris.ch/)

Contact us