Disable colors or strip colors from string

1,069 views
Skip to first unread message

Srinath

unread,
Mar 22, 2016, 3:28:11 PM3/22/16
to golan...@googlegroups.com
Hi,

In the context of ssh client in go, (https://godoc.org/golang.org/x/crypto/ssh) I am looking for 

1) some way to disable colors when using session.RequestPty 
2) if #1 is not possible, I need some way to strip all colors(they are formatted as ANSI escape codes) from the string obtained from stdoutpipe of such session.

Thanks,
Srinath

Konstantin Khomoutov

unread,
Mar 22, 2016, 3:44:44 PM3/22/16
to Srinath, golan...@googlegroups.com
On Tue, 22 Mar 2016 19:27:23 +0000
Srinath <g.s.sr...@gmail.com> wrote:

> In the context of ssh client in go, (
> https://godoc.org/golang.org/x/crypto/ssh) I am looking for
>
> 1) some way to disable colors when using session.RequestPty

In theory, there should be a way to communicate the type of your
terminal device to the remote. If you set it to something not
supporting colors ("dumb" is a good candidate) the programs running
remotely should not attempt to use colors.

Note that the remote system must have the definition file for this
terminal available. (If this is a GNU/Linux based system you can
verify this by running `infocmp dumb` there).

> 2) if #1 is not possible, I need some way to strip all colors(they are
> formatted as ANSI escape codes) from the string obtained from
> stdoutpipe of such session.

That's simple [1]: you look for a sequence of two runes "\033["
and skip everything until, and including, the nearest rune "m".

1. https://github.com/mgutz/ansi/blob/master/ansi.go#L26

John McKown

unread,
Mar 22, 2016, 4:39:44 PM3/22/16
to Srinath, golan...@googlegroups.com
​I did a quick try on the following, but it is just a straight SSH from my Linux/Intel system to my Raspberry Pi 3, running Raspbian. Instead of just entering: ssh rpi3, I entered: TERM=dumb ssh rpi3. This set the terminal to "dumb" which BASH realizes does not have any coloring capability and so it doesn't color anything. Looking here: https://godoc.org/golang.org/x/crypto/ssh#Session.RequestPty The example shows something like:

// Request pseudo terminal
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
    log.Fatalf("request for pseudo terminal failed: %s", err)
}

=== change to ===

// Request pseudo terminal
if err := session.RequestPty("dumb", 80, 40, modes); err != nil {
    log.Fatalf("request for pseudo terminal failed: %s", err)
}

Unfortunately, I am ignorant of how to use Go + ssh + public keys. I've given it a try, but have (so far) failed to get anything working.

--
How many surrealists does it take to screw in a lightbulb? One to hold the giraffe and one to fill the bathtub with brightly colored power tools.

Maranatha! <><
John McKown

Konstantin Khomoutov

unread,
Mar 23, 2016, 6:46:51 AM3/23/16
to Srinath, golan...@googlegroups.com
On Tue, 22 Mar 2016 19:27:23 +0000
Srinath <g.s.sr...@gmail.com> wrote:

...and while we're at it, are you sure you need PTY at all?
Pseudo terminal is only needed if you intend to spawn in the remote
session a program which actively expects to be connected to a terminal
device, and such programs are in most cases are interactive -- such as
full-screen text editors (like nano, Vim, Emacs etc) or, say, chat
clients like irssi or mcabber and so on. If you merely want to spawn a
program which prints something to its standard output streams, and you
want to grab that output, not only you do not need to allocate a PTY to
run that program, but doing that might actually complicate matters.

To explain it in more simple words, suppose you need to run `git log`
on a remote machine and grab its output. If you allocate a PTY for
that command to run, it will detect it's connected to a terminal and
enable colouring of its output -- assuming it's interacting with a
human operator. What's worse, it will even spawn the so-called
"pager" (usually `less` or `more`) to pipe its output through it.
Conversely, if it will detect it's not connected to a termnial, it will
go to a "no-frills" mode and merely output plain uncoloured data into
its stdout; no pager will be run, of course.

TL;DR
Please make sure you do really need a PTY to run your program.
Reply all
Reply to author
Forward
0 new messages