Banjić Medina
Banjić Medina

Reputation: 11

TelnetClient output - ANSI disable

I have seen similar question and answers on stackoverflow.com

Unfortunately, this is not working for me. I have the same code as the example given in previous questions like this, but the "dumb" terminal type (TelnetClient telnet = new TelnetClient("dumb") was he solution for others ) is not filtering ANSI chars so I get this:

Last login: Fri May 20 10:09:21 from 172.20.22.244

[01;33mteltest@vivadev[00m:[01;34m~[00m$ ls ls testing [01;33mteltest@vivadev[00m:[01;34m~[00m$ cd testing cd testing [01;33mteltest@vivadev[00m:[01;34m~/testing[00m$ ls

and I need a readable file. Is there any other solution known, as encoding outputStream, anything?

Thank you.

Upvotes: 0

Views: 197

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54583

Conventional applications pay attention to TERM, so that dumb (which does not use color) would do what you want. However, there are a number of hardcoded applications (no comment needed).

Some of those will suppress color if you redirect the output of your program to a file, e.g.

foo >bar

but many (probably the majority of the misbehaving programs) ignore even that. To work around those, you have to filter the results, either by a sed script or similar program, or by (for example) capturing the output of your command by redirecting or using script and then post-processing the result. For instance, you could do this with a script something like

#!/bin/sh
myscript=$(mktemp)
trap "sed -f $myscript typescript; rm -f $myscript typescript" EXIT INT QUIT HUP
cat >$myscript <<"EOF"
s/^[[[][<=>?]\{0,1\}[;0-9]*[@-~]//g
xample:
s/^[[]][^^[]*^[//g
s/^[[]][^^[]*^[\\//g
:loop
s/[^^[]^[\(.\)/\1/g
t loop
s/  *$//g
s/^.* //g
s/^[[^[]//g
/\1/g
EOF
script -c "$*" >/dev/null

which illustrates the approach. Most of the ^[ pairs in the example are literal ASCII escape characters, which you won't be able to select/paste. The original sed script is here: script2log

The point of the script is that it runs the command normally and then echoes the filter results.

Upvotes: 1

Related Questions