从JSch中的命令输出中删除shell东西(如提示) [英] Removing shell stuff (like prompts) from command output in JSch

查看:422
本文介绍了从JSch中的命令输出中删除shell东西(如提示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将SSH连接到节点,发送输入并检索输出。输入一行后,该行将打印到控制台,然后是空白行,然后输出打印两次。输入后我不希望输入打印到控制台,也不希望输出空白行,也不希望输出第二次输出。以下是我的代码

I have successfully SSH'ed into a node, sent the input, and retrieved the output. After inputting a line, the line is printed to the console, followed by a blank line, and then the output prints twice. I don't want the input to print to the console after it is entered, nor the blank line, nor the output printed a second time. Below is the code I have

public void runSession() {
    try {
        Channel channel = session.openChannel("shell");
        channel.setInputStream(System.in, true);
        channel.setOutputStream(System.out, true);
        channel.connect(defaultChannelTimeout);

        while (channel.getExitStatus() == -1) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }

        channel.disconnect();
    } catch(JSchException jschEx) {
        System.out.println("JSch exception during I/O");
        System.out.println(jschEx.getMessage());
    }
}

以下是运行时控制台的样子

Here is what the console looks like when running


user:domain @ node:/ a / b / c> cd ..

user:domain@node:/a/b/c> cd ..

cd ..

user:domain @ node:/ a / b>
user:domain @ node:/ a / b>

user:domain@node:/a/b> user:domain@node:/a/b>

如您所见,存在问题:


  • cd ..单独打印到控制台的一行

  • cd ..后面出现一个空白行。

  • user:domain @ node:/ a / b>行打印两次。

有谁知道如何删除这3项从显示在控制台?期望的输出是

Does anyone know how I can remove these 3 items from being displayed in the console? Desired output is


user:domain @ node:/ a / b / c> cd ..

user:domain@node:/a/b/c> cd..

user:domain @ node:/ a / b>

user:domain@node:/a/b>


推荐答案

这些都是shell频道的后果。

These are all consequences of the "shell" channel.

你用人类用户喜欢的所有花哨的东西进行交互式会话。

You run an interactive session with all the fancy stuff that human users like.

shell频道不适用于自动化。

The "shell" channel is not intended for automation.

您可以通过调用 channel.setPty删除其中一些频道。在 channel.connect()之前

You may remove some of these by calling channel.setPty(false) before channel.connect().

虽然你最好使用exec频道。

Though you better use the "exec" channel.

这可能有效:

( echo username & echo password & echo hostname ) | command






相关问题:


Related questions:

  • Remove of unwanted characters when I am using JSch to run command
  • Is there a simple way to get rid of junk values that come when you SSH using Python's Paramiko library and fetch output from CLI of a remote machine?

这篇关于从JSch中的命令输出中删除shell东西(如提示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆