Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

commons-net is compatible with ssh-2.0 protocol

i have tried create a project with library commons.net for send via ftp some files. But i created a connection with my server i have received this error.

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3

i have followed this article for create my connection, and with official examples i've controlled article.

my java code here:

  private void connect(String host, String user, String pwd) {
        try{
        ftp = new FTPSClient(false);
        //ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;

        ftp.connect(host,22);//error is here
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new Exception("Exception in connecting to FTP Server");
        }
        ftp.login(user, pwd);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

I do not understand where I went wrong.

like image 242
Nopesound Avatar asked Dec 19 '13 09:12

Nopesound


3 Answers

The FTPS protocol does not run over SSH. What you need is SFTP. For this you could look at the Jsch library

    JSch jsch = new JSch();
    Session session = jsch.getSession( user, host, port );
    session.setConfig( "PreferredAuthentications", "password" );
    session.setPassword( pass );
    session.connect( FTP_TIMEOUT );
    Channel channel = session.openChannel( "sftp" );
    ChannelSftp sftp = ( ChannelSftp ) channel;
    sftp.connect( FTP_TIMEOUT );
like image 159
Nick Wilson Avatar answered Oct 17 '22 05:10

Nick Wilson


SFTP (file transfer running as an SSH stream over an SSH connection) is not the same as FTPS (FTP using SSL/TLS).

like image 34
chrylis -cautiouslyoptimistic- Avatar answered Oct 17 '22 05:10

chrylis -cautiouslyoptimistic-


import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPConnectAndLoginDemo {

public static void main(String[] args) throws Exception {

    String user = "username";
    String host = "hostadress";
    int port = 22;
    String pass = "password";
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, port);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(pass);
    session.connect();
    System.out.println("Connection established.");
    System.out.println("Creating SFTP Channel.");
}

try this instead hope this can help.The Session.setConfig ommits the hashcode check and allows user to connect to host.

like image 2
Bharat Dhal Avatar answered Oct 17 '22 05:10

Bharat Dhal