解決:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3

來源:互聯網
上載者:User

標籤:net   exce   match   private   應用   伺服器   content   stat   form   

[摘要:辦理:org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3   當應用org.apache.commons.net.ftp.]

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

當使用org.apache.commons.net.ftp.FTPClient通過協議SSH2進行SFTP串連時報如上錯誤,原因是它不支援這種方式的串連(使用FTPSClient的SSL也是不行的)。

範例程式碼:

package com.jerval.test;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class FtpFileList {    private static final Logger LOG = LoggerFactory.getLogger(FtpFileList.class);    public static void main(String[] args) {        printList();    }    private static void printList() {        List<String> list = listFileNames("fca-vm-rds-prod1", "applog", "aaa", "/webapp/myrds1/lib");        for (String fileName:list) {            System.out.println(fileName);        }    }    private static List<String> listFileNames(String host, String user, String password, String dir) {        List<String> list = new ArrayList<String>();        FTPClient ftpClient = new FTPClient();        try {            ftpClient.connect(host, 22);            int reply = ftpClient.getReplyCode();            if (FTPReply.isPositiveCompletion(reply)) {                ftpClient.login(user, password);                // ftpClient.changeWorkingDirectory(dir);                String[] names = ftpClient.listNames(dir);                list.addAll(Arrays.asList(names));            }        } catch (IOException e) {            LOG.error("ERROR!", e);        } finally {            close(ftpClient);        }        return list;    }    private static void close(FTPClient ftpClient) {        if (null != ftpClient && ftpClient.isConnected()) {            try {                ftpClient.logout();// 退出FTP伺服器            } catch (IOException e) {                e.printStackTrace();                System.out.println("退出FTP伺服器異常!");                System.out.println(e.getMessage());            } finally {                try {                    ftpClient.disconnect();// 關閉FTP伺服器的串連                    System.out.println("退出並關閉FTP伺服器的串連");                } catch (IOException e) {                    e.printStackTrace();                    System.out.println("關閉FTP伺服器的串連異常!");                    System.out.println(e.getMessage());                }            }        }    }}

錯誤資訊:

3    [main] ERROR com.jerval.test.FtpFileList  - ERROR!org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:333)at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:396)at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:796)at org.apache.commons.net.SocketClient.connect(SocketClient.java:172)at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)at com.jerval.test.FtpFileList.listFileNames(FtpFileList.java:32)at com.jerval.test.FtpFileList.printList(FtpFileList.java:22)at com.jerval.test.FtpFileList.main(FtpFileList.java:18)org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Reply: Protocol mismatch.

解決方案:

使用com.jcraft.jsch.JSch提供的SSH解決方案。代碼如下:

package org.jerval.test.ftp;import java.util.ArrayList;import java.util.List;import java.util.Properties;import java.util.Vector;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.ChannelSftp.LsEntry;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;public class FtpsFileList {    private static final Logger LOG = LoggerFactory.getLogger(FtpsFileList.class);    public static void main(String[] args) {        listFileNames("fca-vm-rds-prod1.xxx.org", 22, "applog", "xxx", "/webapp/myrds1/lib");    }    private static List<String> listFileNames(String host, int port, String username, final String password, String dir) {        List<String> list = new ArrayList<String>();        ChannelSftp sftp = null;        Channel channel = null;        Session sshSession = null;        try {            JSch jsch = new JSch();            jsch.getSession(username, host, port);            sshSession = jsch.getSession(username, host, port);            sshSession.setPassword(password);            Properties sshConfig = new Properties();            sshConfig.put("StrictHostKeyChecking", "no");            sshSession.setConfig(sshConfig);            sshSession.connect();            LOG.debug("Session connected!");            channel = sshSession.openChannel("sftp");            channel.connect();            LOG.debug("Channel connected!");            sftp = (ChannelSftp) channel;            Vector<?> vector = sftp.ls(dir);            for (Object item:vector) {                LsEntry entry = (LsEntry) item;                System.out.println(entry.getFilename());            }        } catch (Exception e) {            e.printStackTrace();        } finally {            closeChannel(sftp);            closeChannel(channel);            closeSession(sshSession);        }        return list;    }    private static void closeChannel(Channel channel) {        if (channel != null) {            if (channel.isConnected()) {                channel.disconnect();            }        }    }    private static void closeSession(Session session) {        if (session != null) {            if (session.isConnected()) {                session.disconnect();            }        }    }}

Maven依賴:

    <dependency>      <groupId>com.jcraft</groupId>      <artifactId>jsch</artifactId>      <version>0.1.49</version>    </dependency>

解決:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.