remove ANSI control codes

149 views
Skip to first unread message

tcof...@gmail.com

unread,
Nov 28, 2016, 7:03:48 PM11/28/16
to Yet another Expect for Java
using the following code.

Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoOutput(System.out)
.withEchoInput(System.err)
.withInputFilters(removeColors(), removeNonPrintable())


coupled with jsch to connect to an HP procurve switch. "removeNonPrintable()" doesn't appear to be working. I see the following output in debug.

###############################################################################
[?7l [3;23r [?6l [1;1H
[?25l [1;1HProCurve J9087A Switch 2610-24-PWR
Software revision R.11.89

Copyright (C) 1991-2011 Hewlett-Packard Co. All Rights Reserved.

RESTRICTED RIGHTS LEGEND

Use, duplication, or disclosure by the Government is subject to restrictions
as set forth in subdivision (b) (3) (ii) of the Rights in Technical Data and
Computer Software clause at 52.227-7013.

HEWLETT-PACKARD COMPANY, 3000 Hanover St., Palo Alto, CA 94303

Press any key to continue [?25h [24;27H [?7l [3;23r [?6l [24;27H [?25h [24;27H [?6l [1;24r [?7l [2J [24;27H [1;24r [24;27H [2J [?7l [1;24r [?6l [24;1H [1;24r [24;1H [24;1H [2K [24;1H [?25h [24;1H [24;1sw1>

when viewed in putty the above loks like this

ProCurve J9087A Switch 2610-24-PWR
Software revision R.11.89

Copyright (C) 1991-2011 Hewlett-Packard Co. All Rights Reserved.

RESTRICTED RIGHTS LEGEND

Use, duplication, or disclosure by the Government is subject to restrictions
as set forth in subdivision (b) (3) (ii) of the Rights in Technical Data and
Computer Software clause at 52.227-7013.

HEWLETT-PACKARD COMPANY, 3000 Hanover St., Palo Alto, CA 94303











Press any key to continue
###############################################################################


I tried to use

.withInputFilters(removeColors(), removeNonPrintable(),replaceInString("[\\p{Cntrl}]", ""))

but that doesn't seem to work either. I suspect I need different regex to strip out the ANSI garbage but am not able to come up with a working solution.

Any suggestions greatly appreciated.

Alexey Gavrilov

unread,
Dec 2, 2016, 4:58:07 PM12/2/16
to Yet another Expect for Java


Begin forwarded message:

From: Alexey Gavrilov <Alexey1....@gmail.com>
Subject: Re: remove ANSI control codes
Date: 2 December 2016 at 22:47:58 GMT+1

Hi,

Yes, looks the standard filters don’t work in  this HP case.

I recommend you to try building your own filter based on the Filters#COLORS_REGEXP_STRING regular expression. See the  Filters.java and FiltersTest.java for more details.

I may help you with regular expression if can send me the byte representation of the output.

Regards,
Alexey

--
You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


tcof...@gmail.com

unread,
Dec 2, 2016, 5:52:01 PM12/2/16
to Yet another Expect for Java, Alexey1....@gmail.com
Alexey,

Not sure what you mean by byte representation. Isn't that what I have listed above for example between "Press any key to continue ... sw1>" ? The output seems variable from the HP switch.

Seems the ANSI codes between items can be a couple to many. They seem to all start with an escape character followed by [. The next character/s can be a ? or a one or two digit number. if its a number its followed by a semi colon ; a one to two digit number with an optional letter following.

for the regex using COLORS_PATTERN "\\x1b\\[" does the \\x1b represent the escape?

Alexey Gavrilov

unread,
Dec 3, 2016, 7:20:38 AM12/3/16
to tcof...@gmail.com, Yet another Expect for Java
Not sure what you mean by byte representation. Isn't that what I have listed above for example between "Press any key to continue ... sw1>" ? The output seems variable from the HP switch. 
I meant to capture the output in the byte format because it is not clear what ‘?’ char stands for in string like this: 
"Press any key to continue [?25h [24;27H [?7l [3;23r [?6l [24;27H [?25h [24;27H [?6l [1;24r [?7l [2J [24;27H [1;24r [24;27H [2J [?7l [1;24r [?6l [24;1H [1;24r [24;1H [24;1H [2K [24;1H [?25h [24;1H [24;1sw1>"

You can add an ExpectBuilder#withEchoOutput which would write directly into the file, then you can see the bytes/characters which you actually receiving.



Seems the ANSI codes between items can be a couple to many. They seem to all start with an escape character followed by [. The next character/s can be a ? or a one or two digit number. if its a number its followed by a semi colon ; a one to two digit number with an optional letter following.

I’m not sure the ‘?’ is actually question mark. Please see above.


for the regex using COLORS_PATTERN "\\x1b\\[" does the \\x1b represent the escape?
Correct.
Reply all
Reply to author
Forward
0 new messages