当前位置: 首页 > 知识库问答 >
问题:

JSch频道。断开连接以防止日志打印

戴树
2023-03-14

我正在使用JSch在远程机器上运行shell脚本,并使用JSch通道在日志文件中打印命令日志。问题是,当脚本结束时,我会创建一个通道。断开连接,断开连接后不久,系统将关闭。out停止打印到日志文件中。代码如下:

private int runShellScript(HashMap bundleDetails) {
        int exitStatus = -1;
        Channel channel = null;
        Session session = null;
        try {
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");

            String host = (String) bundleDetails.get("host");
            String userName = (String) bundleDetails.get("userName");
            String password = (String) bundleDetails.get("password");
            String bundleName = findFileName((String) bundleDetails.get("bundleName"));

            String sourceLocation = (String) bundleDetails.get("sourceLocation");
            String logFileName = findFileName((String) bundleDetails.get("logFileName"));

            String targetLocation = (String)bundleDetails.get("targetLocation");            

            String command1 = "sh "+(String) bundleDetails.get("targetIndexerLocation") + (String) bundleDetails.get("deployScript")+" "+
                                    targetLocation + bundleName + " " +
                                    targetLocation + logFileName;
            JSch ssh = new JSch();
            session = ssh.getSession(userName, host, 22);
            session.setConfig(config);
            session.setPassword(password);
            session.connect();

            channel = session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command1);
            channel.setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);
            InputStream in=channel.getInputStream();
            channel.connect();
            byte[] tmp=new byte[1024];
            while(true){
                //System.out.println("inside while second");
                  while(in.available()>0){
                    int i=in.read(tmp, 0, 1024);
                    if(i<0)break;
                    System.out.print("*****NEW ONE*****$$$$$**$$########"+new String(tmp, 0, i));
              }
                  if(channel.isClosed()){
                      exitStatus = channel.getExitStatus();
                     System.out.println("Before Disconnected Here exit-status: "+exitStatus);
                      channel.disconnect();
                    System.out.println("Disconnected Here exit-status: "+exitStatus);
                    break;
                  }
            }       
            //logger("runShellScript", "END");
            System.out.println("***** out of infinite loop");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Copy to remote location failed.... ");
        }finally{
            System.out.println("finally DISCONNECT channel and session");
            if(channel!=null && channel.isConnected()){
                channel.disconnect();
            }
            if(session!=null){
                session.disconnect();
            }           
            System.out.println("finally DISCONNECTED channel and session");

        }
        System.out.println("Before return exit-status: "+exitStatus);
        return exitStatus;
    }

日志文件中的行:

*****NEW ONE*****$$$$$**$$########Starting... 

之前断开这里退出-状态: 0

如果您在我上面粘贴的方法中看到,sysout打印的实际上就是“通道”上方的一个。断开'。下面的那个没有打印出来!所有功能正常,总体输出符合我的预期

系统出来println(“在此断开连接之前退出状态:“退出状态”)<频道。断开连接()
系统。出来println(“此处已断开连接退出状态:“退出状态”);

所有的功能都是正确的,整体输出是我所期望的。唯一的问题是日志冻结。我哪里做错了?

编辑
另外,我无法从我的finally块中看到syout!!

共有2个答案

丌官浩旷
2023-03-14

这是因为

((ChannelExec)channel).setErrStream(System.err);

当您断开通道连接时,连接的流也会断开连接。

因此,请在断开通道之前写下以下声明:

((ChannelExec)channel).setErrStream(null);
方谦
2023-03-14

很可能与这一行有关:

((ChannelExec)channel).setErrStream(System.err);

通过这样做,您已经将System.err流绑定到通道流。默认情况下,每个留档在通道断开时关闭流。你没有说你在哪个平台上运行,但是我认为大多数平台以某种方式连接System.err和System.out,所以Jsch很可能在System.out断开连接时关闭它。您可以尝试这样做以防止JSch关闭流:

((ChannelExec)channel).setErrStream(System.err, true);

Javadoc

即使这确实有效,我认为像这样ystem.err有点冒险。我认为更安全的设计是创建一个直接写入日志文件的流,而不是通过System.err.

 类似资料:
  • 问题内容: 如果我从未检查过Go通道的状态,可以永远保持打开状态(永远不关闭通道)可以吗?会导致内存泄漏吗?下列代码可以吗? 问题答案: 可以永远保持Go频道永远不关闭。当不再使用该通道时,将对其进行垃圾回收。 请注意,只有当接收器正在寻找关闭通道时,才需要关闭通道。关闭通道是通道上的控制信号,指示没有更多数据跟随。 设计问题:通道关闭

  • 2.7 频道日志查询 2.7.1 描述 通过调用该接口查询频道一个月内日志。默认每小时一个日志。 日志格式:ip - - 请求完成时间 “方法 URL 协议” 状态码 大小 来源 客户端 总处理时间 2.7.2 请求地址 地址: https://api.bokecs.com/channel/queryLog?domain={domain}&begin={begin}&end={end} 2.7.3

  • 我正在为我的应用程序使用spring boot,并且我正在使用默认的spring boot日志记录。 是否有任何配置来防止spring boot将日志消息打印到控制台?

  • 我试图连接到无法通过JSch管理的远程设备,尽管我可以使用具有相同凭据的WinSCP客户端进行连接。 该应用程序是一个基于J2EE的企业应用程序,在JBoss作为7.1.1运行,在Windows服务器上,我试图从Quartz作业连接SFTP服务器。下面是我用来验证SFTP登录的代码。 你能帮我找出这种行为的可能原因吗?

  • 在断开连接按钮上,我尝试了: relivant按钮代码的当前部分 } } 编辑:猜是因为varible会话在winconnect中,而不是在按钮中,所以找不到它? Edit2:尝试下面的建议后,单击断开连接就会锁定 Edit5:如果调用session.disconnect确实有效,则选中此选项,但由于某种原因,它会导致我的程序锁定

  • 断开mqtt连接,前提是必须已经通过Iot_id,Iot_pwd建立过一次mqtt连接。 请求方式: "|4|1|4|\r" 返回值: "|4|1|4|1|\r" 断开成功 "|4|1|4|2|\r" 断开失败 Arduino样例: softSerial.print("|4|1|4|\r");