2

I am using golang ssh package to build a ssh client, I set the terminal modes as below

// Set up terminal modes
modes := ssh.TerminalModes{
    ssh.ECHO:          0,     // disable echoing
    ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
    ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}

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

it works fine, but a tiny problem, when I use "ls" command, it returns the names with color code, like below

[root@localhost ~]# ls
ls
anaconda-ks.cfg    [0m[01;34mpeter[0m
[01;32mbuild.sh[0m           [01;34mredis64[0m

it makes me feel difficult to read the file names, so is there any method to remove the color code around the names? thanks

ddaddca
  • 69
  • 3
  • 2
    Have `ls` not output colors: `ls --color=never`? – Volker Oct 16 '15 at 10:33
  • Take a look at [`terminal.Terminal`](https://godoc.org/golang.org/x/crypto/ssh/terminal#Terminal)'s `Escape` field and [`vt100EscapeCodes`](https://github.com/golang/crypto/blob/master/ssh/terminal/terminal.go#L24). That might be a solution. – thwd Oct 16 '15 at 10:50
  • 3
    You're claiming to be an `xterm` (which supports colour). Perhaps pick a different terminal type? – James Henstridge Oct 16 '15 at 11:52
  • Whether color codes are sent by the server depend on two factors: the terminal ID set in `RequestPty` and how it's interpreted (or not) on the server. i.e. if you have, on the server, a shell init script that sets up `ls` to output colors regardless of `$TERM`, changing the terminal ID won't change a thing (oh, yes, `$TERM` on the server is initialized with the term ID that you pass in `RequestPty`). So unless you really want something interactive, I'd go for @Volker's solution. – wldsvc Oct 19 '15 at 00:42

1 Answers1

0

Use session.RequestPty with "vt220" terminal type

example: session.RequestPty("vt220", 80, 40, modes)

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Maybe/maybe not: [NewTerminal](https://godoc.org/golang.org/x/crypto/ssh/terminal#NewTerminal) does not appear to care what type is passed in, and looking for examples of use for `RequestPty` I find nothing useful. – Thomas Dickey Oct 16 '15 at 23:20
  • You don't need to use `NewTerminal` to execute commands using ssh. Example: https://play.golang.org/p/0q0Yu_8vpE – Gregory Man Oct 17 '15 at 09:35
  • 1
    That isn't useful to me. What would be useful would be showing how the "vt220" gets passed to the other side of the pty and is *used*. Lacking that, I cannot distinguish between your answer and others. – Thomas Dickey Oct 17 '15 at 10:51