【问题标题】:JSCH: Is there a way to retrieve the prompt?JSCH:有没有办法检索提示?
【发布时间】:2013-01-16 15:16:23
【问题描述】:

我正在使用 JSCH 通过 SSH 访问主机,然后启动一些命令。

主机未使用“通用”操作系统,因为它是 BNG:http://en.wikipedia.org/wiki/Border_Network_Gateway

我需要检索提示,因为它包含有用的信息,因为它可能类似于 [local]MYBNG#[not_local]MYBNG# 我需要应用一些逻辑来知道提示中是否有 localnot_local

ExecChannel 中检索InputStream 不起作用,因为您只能看到刚刚启动的命令的输出...

【问题讨论】:

    标签: java ssh prompt jsch


    【解决方案1】:

    在 JSCH 的示例页面上有一个登录 SSH 服务器并检索 shell 提示符的示例:

    Example is here

    应该这样做的代码具体是:

    Channel channel=session.openChannel("shell");
    channel.setOutputStream(System.out);
    

    这些示例具有用于特定终端仿真的 cmets,这取决于 BNG,可能需要。

    【讨论】:

      【解决方案2】:

      你可以直接回车:

      outputStream.write("\n");
      outputStream.flush();
      

      读取输出:

      String res = "";
      byte array[];
      int num;
      do {
          if ((num = inputStream.available()) > 0) {
              array = new byte[num];
              num = inputStream.read(array);
              String ret = new String(array, 0, num);
              res += ret;
      
              if (inputStream.available() == 0)
                  break;                      
          }
      } while (true);
      

      并验证它是否包含一些提示:

      res.toString().contains("[local]MYBNG#");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-10
        • 2013-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        相关资源
        最近更新 更多