2

I am using jsch library for my project. Jsch provide two channel EXEC and SHELL.

I want to get linux environment variable i.e: echo $FILE_EDIITON and this is not possible with exec. but it is possible with SHELL.

Problem is that While calling readline() method, it showing different character at the starting of line in debug tooltip (you can see in image below by click on link). but when i print same line on console. it is not showing those different character in console. It is perfectly printed as i wanted.

This is what i see in debug tooltip in eclipse.

[H[J[applmgr@codezen ~]$

When print same line on console

[applmgr@codezen ~]$

and because of this my if condition is not satisfied isIgnoreLine() method

Hope you understand my question and English.

This is my sample code.

@Override
public void run() {
    try {
        InputStreamReader inputStreamReader = new InputStreamReader(this.inputStream, "UTF-8");
        System.out.println(inputStreamReader.getEncoding());
        BufferedReader dataIn = new BufferedReader(inputStreamReader);
        BufferedWriter dataOut = new BufferedWriter(new OutputStreamWriter(this.outputStream));
        String line = null;
        while (isRunning) {
            if (this.isCommandExecution) {
                dataOut.write(this.command + "\r\n");
                dataOut.flush();
                this.isCommandExecution = false;

                this.output = new StringBuffer();
                while ((line = dataIn.readLine()) != null) {
                    System.out.println(line);
                    if (this.command.trim().equals(SSHClient.LINUX_COMMAND_CLEAR)) {
                        if (!line.trim().isEmpty() && line.contains(SSHClient.LINUX_COMMAND_CLEAR)) {
                            String[] arr = line.trim().split(" ");
                            if (arr[arr.length - 1].trim().equals(SSHClient.LINUX_COMMAND_CLEAR)
                                    && line.trim().length() != SSHClient.LINUX_COMMAND_CLEAR.length()) {
                                String tempStr = "";
                                for (int i = 0; i < arr.length - 1; i++) {
                                    tempStr += arr[i] + " ";
                                }
                                if (!tempStr.trim().isEmpty()) {
                                    this.terminalPrompt = tempStr.trim();
                                }
                            }
                        }
                        if (this.terminalPrompt != null) {
                            System.out.println("Break...");
                            break;
                        }
                    } else {
                        if (!isIgnoreLine(line, this.command)) {
                            System.out.println("Output..." + line);
                            this.output.append(line);
                        }
                    }
                }
                this.isResponseReady = true;
            }
        }
        dataOut.close();
        dataIn.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public boolean isIgnoreLine(String line, String command) throws Exception {
    if (this.terminalPrompt == null) {
        throw new Exception("Terminal Prompt is not found");
    }
    if (this.terminalPrompt.equals(line.trim())) {
        return true;
    } 
    return false;
}

Please click here to see screenshot

Prashant
  • 23
  • 4
  • 1
    the `[G` etc are called [escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code) I think you will have to remove them by hand –  Jan 06 '17 at 12:22
  • see http://stackoverflow.com/questions/25189651/how-to-remove-ansi-control-chars-vt100-from-a-java-string for "how" –  Jan 06 '17 at 12:24
  • @RC. Thank you man. Its worked. – Prashant Jan 07 '17 at 07:03
  • Glad it did :) (marked as duplicate for future reader) –  Jan 07 '17 at 10:58

0 Answers0