java.io.IOException: com.jcraft.jsch.JSchException: channel is not opened. 解决方案

 

最近工作中遇到 channel is not opened 这个报错,

 

网上的资料基本上是如下三种方案:

1、设置超时时间:

session.setTimeout(60000);

这个方案没试直接否了。

2、调整 /etc/ssh/sshd_config 配置文件的 MaxSessions,原来是10,改成了20,并去掉了“#”符号,保存后重启。

没解决。

3、释放linux内存。

1、free -m
2、sync
3、echo 3 > /proc/sys/vm/drop_caches

没解决。

=========================================================================

最后调整 /etc/ssh/sshd_config 配置文件的 GSSAPIAuthentication 和 UsePAM 为no,解决。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Java中连接到Linux虚拟机并在其上执行命令,可以使用JSch库。下面是一个简单的例子: ```java import com.jcraft.jsch.*; public class SSHExample { public static void main(String[] args) { try { JSch jsch = new JSch(); String host = "your-ssh-host"; String user = "your-ssh-user"; String password = "your-ssh-password"; Session session = jsch.getSession(user, host, 22); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); String command = "ls -l"; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); // 读取命令的输出 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); channel.disconnect(); session.disconnect(); } catch (JSchException | IOException e) { e.printStackTrace(); } } } ``` 在上面的例子中,我们使用JSch库连接到SSH主机并执行命令。需要替换`your-ssh-host`,`your-ssh-user`和`your-ssh-password`为实际的SSH主机,用户名和密码。然后,我们指定要在虚拟机中执行的命令,并使用`ChannelExec`执行该命令。最后,我们读取命令的输出并打印到控制台上。 需要注意的是,JSch库需要在项目中包含相应的依赖项。可以在Maven或Gradle中添加JSch库的依赖项。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值