关于使用JSch连接sftp服务器引发的异常

2023-11-06

异常信息:

com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
	at com.jcraft.jsch.Session.connect(Session.java:565)
	at com.jcraft.jsch.Session.connect(Session.java:183)

首先需要了解ssh协议的原理:https://www.cnblogs.com/zmlctt/p/3946860.html

然后开启JSch的日志打印功能,不然你是无法定位哪里错了,否则只能抓包

JSch只提供日志接口,如何打印日志,需要自己实现,代码如下

public class SftpLogger implements com.jcraft.jsch.Logger {
	    
        static Hashtable<Integer, String> name = new Hashtable<Integer, String>();
        static {
            name.put(new Integer(DEBUG), "DEBUG: ");
            name.put(new Integer(INFO), "INFO: ");
            name.put(new Integer(WARN), "WARN: ");
            name.put(new Integer(ERROR), "ERROR: ");
            name.put(new Integer(FATAL), "FATAL: ");
        }

		@Override
		public boolean isEnabled(int level) {
			return true;
		}

		@Override
        public void log(int level, String message) {
		    //这里我们就用控制台红色字体输出
            System.err.print(name.get(new Integer(level)));
            System.err.println(message);
        }
		
	}

然后开启打印

public void login(String username, String password, String host, int port) {
	    JSch.setLogger(new SftpLogger()); //设置打印类
	    JSch jsch = new JSch();
		Session session = null;
		Channel channel = null;
		try {
			session = jsch.getSession(username,host,port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect();
			channel = session.openChannel("sftp");
			channel.connect();

			//do something

		} catch (JSchException e) {
			e.printStackTrace();
		} finally {
			if(channel != null){
				channel.disconnect();
			}
			if(session != null){
				session.disconnect();
			}
		}
	}

开启打印日志后

Connecting to 198.23.12.158 port 8022
Connection established
Remote version string: SSH-2.0-OpenSSH_7.9
Local version string: SSH-2.0-JSCH-0.1.54
CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
aes256-ctr is not available.
aes192-ctr is not available.
aes256-cbc is not available.
aes192-cbc is not available.
CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
diffie-hellman-group14-sha1 is not available.
CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
SSH_MSG_KEXINIT sent
SSH_MSG_KEXINIT received
kex: server: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256
kex: server: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
kex: server: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
kex: server: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
kex: server: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
kex: server: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
kex: server: none,zlib@openssh.com
kex: server: none,zlib@openssh.com
kex: server: 
kex: server: 
kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: none
kex: client: none
kex: client: 
kex: client: 
kex: server->client aes128-ctr hmac-sha1 none
kex: client->server aes128-ctr hmac-sha1 none
SSH_MSG_KEX_DH_GEX_REQUEST(1024<1024<1024) sent
expecting SSH_MSG_KEX_DH_GEX_GROUP
Disconnecting from 198.23.12.158 port 8022
com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
	at com.jcraft.jsch.Session.connect(Session.java:565)
	at com.jcraft.jsch.Session.connect(Session.java:183)

上述日志中,你会发现有is not available的字样,说明客户端不支持这些算法。为什么不支持?因为jdk环境不支持!查看JSch源码,你会发现有些算法是需要jdk8的

    config.put("diffie-hellman-group14-sha1", 
               "com.jcraft.jsch.DHG14");    // available since JDK8.
    config.put("diffie-hellman-group-exchange-sha256", 
               "com.jcraft.jsch.DHGEX256"); // available since JDK1.4.2.
                                            // On JDK8, 2048bits will be used.
    config.put("ecdsa-sha2-nistp256", "com.jcraft.jsch.jce.SignatureECDSA256");
    config.put("ecdsa-sha2-nistp384", "com.jcraft.jsch.jce.SignatureECDSA384");
    config.put("ecdsa-sha2-nistp521", "com.jcraft.jsch.jce.SignatureECDSA521");

另外也可以从日志中kex: server服务端支持的算法,kex: client客户端支持的算法可以看出他们有能够匹配的算法,所以修改使用jdk8运行应用程序,问题解决!成功后的日志输出为

Connecting to 198.23.12.158 port 8022
Connection established
Remote version string: SSH-2.0-OpenSSH_7.9
Local version string: SSH-2.0-JSCH-0.1.54
CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
SSH_MSG_KEXINIT sent
SSH_MSG_KEXINIT received
kex: server: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256
kex: server: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
kex: server: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
kex: server: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
kex: server: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
kex: server: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
kex: server: none,zlib@openssh.com
kex: server: none,zlib@openssh.com
kex: server: 
kex: server: 
kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: none
kex: client: none
kex: client: 
kex: client: 
kex: server->client aes128-ctr hmac-sha1 none
kex: client->server aes128-ctr hmac-sha1 none
SSH_MSG_KEXDH_INIT sent
expecting SSH_MSG_KEXDH_REPLY
ssh_rsa_verify: signature true
Permanently added '198.23.12.158' (RSA) to the list of known hosts.
SSH_MSG_NEWKEYS sent
SSH_MSG_NEWKEYS received
SSH_MSG_SERVICE_REQUEST sent
SSH_MSG_SERVICE_ACCEPT received
Authentications that can continue: publickey,keyboard-interactive,password
Next authentication method: publickey
Authentications that can continue: keyboard-interactive,password
Next authentication method: keyboard-interactive
Authentications that can continue: password
Next authentication method: password
Authentication succeeded (password).
Disconnecting from 198.23.12.158 port 8022

我不想升级jdk8,是否有其他解决办法?通过看日志可以知道服务端支持的算法有:

kex: server: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256

客户端支持的算法有(也可以通过JSch源码查看):

kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1

那么只需要在代码中这样设置,问题即可解决。把服务端优先支持且不需要jdk8支持的算法放在前面

//diffie-hellman-group1-sha1放前面
session.setConfig("kex", "diffie-hellman-group1-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1");

http://www.jcraft.com/jsch/ 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于使用JSch连接sftp服务器引发的异常 的相关文章

随机推荐

  • 多种方法查找窗口句柄

    一种 使用API函数FindWindowhw FindWindow nil PChar 窗口的标题名称 第二种 通过枚举所有窗口 查询特定条件的窗口function EnumWindowsProc Wnd HWND Param Intege
  • FATFS文件系统返回FR_DISK_ERROR错误的解决方案

    问题描述 我的团队一直在处理一个包含基于标准库的 SD 卡的项目 最近我们决定迁移到 HAL 并开始了 幸运的是 我们项目的所有部分都尽可能地更改为 HAL 它们运行良好 但我们不知道为什么 SD 卡不能正常运行 我们没有更改外设的配置时钟
  • 谷粒学院解析用户登录信息出现 Unexpected token o in JSON at position 1

    原因 JSON parse data data本身就是一个Object 不能解析 看看是不是在login vue中存储返回的登录信息时 需要转换为字符串然后再保存到cookie中JSON stringify this loginInfo 第
  • CVPR 2021

    点击下方卡片 关注 CVer 公众号 AI CV重磅干货 第一时间送达 本文作者 李翔 来源 知乎 已授权 https zhuanlan zhihu com p 313684358 论文 https arxiv org abs 2011 1
  • 二叉树前中后序遍历的迭代写法

    前序遍历 入栈之前访问根节点 public List
  • apache ii评分和死亡率_高大上的风险分层系统:APACHE评分到底是啥?

    APACHE的英文全称为Acute Physiology and Chronic Health Evaluation 中文译为急性生理与慢性健康评分 有个别文献也将APACHE的全文写为Acute Physiology Age and Ch
  • C++菱形继承问题

    多重继承 一个派生类继承了两个或两个以上的基类 如图 如果在多重继承中Class A 和Class B存在同名数据成员 则对Class C而言这个同名的数据成员容易产生二义性问题 这里的二义性是指无法直接通过变量名进行读取 需要通过域 成员
  • redis持久化机制,修改配置文件之后需要这么做才有用

    1 修改配置文件 2 修改完配置文件 想在启服务器的时候 服务区读取到配置文件需要 这么做 2 1 2 2 2 3 2 4 2 5 打开redis服务器也是需要这么操作 拖动server 和 config 才能去读到保存到硬盘的数据 转载于
  • stm32是小端模式还是大端模式

    STM32 是大端模式 在计算机体系结构中 有两种不同的方法来存储多字节数据类型 即大端模式和小端模式 在大端模式中 最高有效字节 即最左边的字节 存储在内存的低地址处 而最低有效字节 即最右边的字节 存储在内存的高地址处 相反 在小端模式
  • [python] 时间序列分析之ARIMA

    1 时间序列与时间序列分析 在生产和科学研究中 对某一个或者一组变量 x t x t 进行观察测量 将在一系列时刻 t1 t2 tn t 1 t 2 cdots t n 所得到的离散数字组成的序列集合 称之为时间序列 时间序列分析是根据系统
  • fill填充函数解析及用法示例

    fill填充函数解析及用法示例 fill x y color 其中x y是填充的范围 color是填充的颜色 1 对x y范围的获取 示例 所以可以得出x 0 1 1 0 y 0 0 1 1 示例代码如下 画一个填充图形 思路 首先需要得到
  • vue3.0通信方式之 Ref

    Ref通信方式 父传子 子传父 父传子
  • 鸿蒙石之鉴流程,鸿蒙石之鉴完全攻略!

    现在小肥皂给大家说说日常神器任务之鸿蒙石之鉴攻略及成就攻略 这是唯一一个起神器可以获得两个及以上五宝的神器 1 在长安传令天兵处领取任务 2 领取任务后来到傲来进行第一场战斗 封印法弟子是天宫会错乱封人 雷霆法弟子是天宫会雷霆万钧 五雷法弟
  • 栈的最小值

    请设计一个栈 除了常规栈支持的pop与push函数以外 还支持min函数 该函数返回栈元素中的最小值 执行push pop和min操作的时间复杂度必须为O 1 示例 MinStack minStack new MinStack minSta
  • utf8字符串转gb2312代码

    因iconv方法有些编译器不支持 则采用下面映射方法 完全代码参考 https download csdn net download weixin 55163060 84566848 unsigned short giGB2312 2124
  • 173.CI/CD(一):gitlab配置,jenkins的安装配置,jenkins实现基础的CI/CD,Sonarqube代码质量检测,Harbor镜像仓库

    目录 一 容器化持续集成的基础概念 1 敏捷开发 持续集成 持续交付 DevOps区别 2 为什么需要持续集成 3 如何设计持续集成流水线 4 什么是持续部署 1 概念 2 要素 3 常见自动化部署方法 4 如何测试部署的效果 5 项目进度
  • ACE日志系统之本机日志系统的多文件实现

    在文章 lt
  • Qt5入门系列之自关联槽函数与手动关联槽函数

    Qt5入门系列之自关联槽函数与手动关联槽函数 1 自关联槽函数 自关联函数适用于关系唯一且功能普通的的sender与槽函数的调用中 操作步骤 1 在 ui文件中选中sender右击 点击 转到槽 来到 cpp文件中 2 在自动生成的槽函数名
  • 6. JVM调优工具详解及调优实战

    JVM性能调优 1 前置启动程序 1 1 Jmap 1 1 1 Jmap查询内存信息 1 1 2 Jmap查询堆信息 1 1 3 jmap查询堆内存dump 1 2 Jstack 1 3 远程连接jvisualvm 1 4 jstack找出
  • 关于使用JSch连接sftp服务器引发的异常

    异常信息 com jcraft jsch JSchException Session connect java io IOException End of IO Stream Read at com jcraft jsch Session