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

[摘要:办理: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.3
	at 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>
时间: 2025-01-07 13:22:00

解决:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3的相关文章

java.io.IOException: Server returned HTTP response code: 411 for URL

今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTTP response code: 411 for URL 百度之后得到:在调用时,添加如下两行代码即可,今行文以记之: /*解决411*/ httpConnection.setRequestProperty("Content-Length","0"); DataOutp

soket.io 客户端连接出现 response code: 400 的解决办法

书中提到使用 npm install socket.io 安装 node.js 的 soket.io 模块 默认安装的为最新版本  可以使用 npm list 查看当前装了哪些模块 如果出现 SIOClientImpl::handshakeResponse() called handshake completedresponse code: 400SIOClientImpl::handshake() failederror buffer: HelloWorld::onError received

retrying failed action with response code: 403 错误解决

[2019-06-10T06:52:51,610][INFO ][logstash.outputs.elasticsearch] retrying failed action with response code: 403 ({"type"=>"cluster_block_exception", "reason"=>"blocked by: [FORBIDDEN/12/index read-only / allow dele

AFNetworking报错:(415 Domain=com.alamofire.error.serialization.response Code=-1011 &quot;Request failed: unsupported media type (415)&quot;)

问题? 今天在与后台调接口的时候,遇到一个问题,使用AFNetworking报错,具体如下: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse:

Error Domain=com.alamofire.error.serialization.response Code=-1016 &quot;Request failed: unacceptabl

在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方 这里分享一下爬坑经历,忘读者不能速爬坑! 在发送请求后,NSURLSessionDataTask一直报错 Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" AFURLResponseSerializat

Error Domain=com.alamofire.error.serialization.response Code=-1016 &quot;Request failed: unacceptable content-type: text/html&quot; 的问题原因及解决方案

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html",此问题的原因就是使用的第三方框架AFNetworking 接口返回值类型不确定,由于服务器人员习惯于使用html文件所以将json文件也这么写了,导致没法解析 在模型类或者网络工具类中添加这句代码就能完美解决上述问题,建议不要直接修改AFNetw

使用Volley来写一个List列表(Valley可以解决很大一部分android请求server的问题)

先上效果图: 先写一个Volley的请求的类: public void fetchData() { String url = "http://2.novelread.sinaapp.com/framework-sae/index.php"; // String body = ""; // try { // mEntity = new StringEntity(body); // } catch (UnsupportedEncodingException e1) {

[原]Error Domain=com.alamofire.error.serialization.response Code=-1016 &quot;Request failed: unacceptable con

转载请注明出处:http://blog.csdn.net/dengbin9009/article/details/43485617 在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方 这里分享一下爬坑经历,忘读者不能速爬坑! 在发送请求后,NSURLSessionDataTask一直报错 Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed:

Caused by: java.io.EOFException: Can not read response from server.

1.错误描述 The last packet successfully received from the server was 76,997 milliseconds ago. The last packet sent successfully to the server was 78,995 milliseconds ago. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.ref