0

I was trying to run console command in Kali linux, but the output are just weird when i pipe it to the JTextPane. When I diplay it on the output console of Netbean it was fine.

Command that I'm trying to run: wifite -e Experiment -c 1

enter image description here

code:

    public cracker(JTextPane aOutputPane)
      {
       super();
        mOutputPane = aOutputPane;
  }
  @Override
  protected String doInBackground() throws Exception
  {
    Process p = null;
    try
    {
        String Channel=CNinput.getText();
        String WName=WN.getText();
      p = Runtime.getRuntime().exec("wifite -e "+WName+" -c "+Channel);
    }
    catch (IOException ex)
    {
      Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    String output = "";
    try
    {
      while ((line = buf.readLine()) != null)
      {
        publish(line);
        output += line + "\n";
      }
    }
    cat

ch (IOException ex)
    {
      Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
    }
    try
    {
      p.waitFor();
    }
    catch (InterruptedException ex)
    {
      Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
  }
  @Override
  protected void process(java.util.List<String> aChunks)
  {
    mOutputPane.setText(null);
    final String intermediateOutput = aChunks.stream().collect(Collectors.joining("\n"));
    final String existingText = mOutputPane.getText();
    final String newText = existingText + "\n" + intermediateOutput;
    mOutputPane.setText(newText);

  }

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

2

The characters are ANSI escape codes, intended to control the appearance of the terminal output generated by wifite. Among your options,

  • Elide the character sequences as they arrive in your implementation of doInBackground(); they all start with the ESC character.

  • Examine the codes and recapitulate the corresponding style in your JTextPane, as shown in the StyledDocument seen here.

  • Use a library such as the NetBeans console or Jansi, cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • After some googling with the thing you mentioned i find it useful, especially Jansi, is it possible to implement it to the .setText and how? I only find limited references on the system.out to AnsiConsole.systemInstall();. – Joco Tan Guan Min Mar 07 '17 at 20:07
  • I'm not sure that `Jansi` supports this usage directly, but it might be worth looking at how it does parsing. I've never needed more than a handful of `SimpleAttributeSet` instances in this context. – trashgod Mar 07 '17 at 23:47
  • How can I use SimpleAttributeSet with Jtextpanel, meaning like how would I allow the console pipe output to pipe formatted text into the jtextpanel? – Joco Tan Guan Min Mar 08 '17 at 07:19
  • You'd have to parse the code and apply the corresponding `SimpleAttributeSet`; sorry, I don't know a simpler approach. Because `JTextPane ` can display (limited) HTML, you might be able to leverage `HtmlAnsiOutputStream`, but I haven't tried it. – trashgod Mar 08 '17 at 12:50