How to create ssh session by ssh foo@bar except not any color format? The only require is the setting should be declared at the ssh command and can't be setting at config file.
As an aside, the reason is I want get the ssh shell result to a Scala program, git repos more detail if you interested. So, the color codes, such as[03m(maybe others), should be removed. I can't expect remote host make some setting and let it no color.
UPDATE
about
JSchSSH library, there is some setting to explain:setPtyType("dumb"), the parameterdumbsetting the session withdumbemulation terminal similar withLinux,VT100etc. Thedumbemulation terminal is not any colors codes in result. Useecho $TERMwill printdumbafter setting this property.setPty(bool enable)default isfalsebut if I explicit settingsetPty(false)the result will be break. Unfortunately I haven't find a API to get the pty values at runtime and the library is lack of document even though it is Java de-facto standard SSH library.
why I want use ssh in a ssh?
- This feature is need because some key file maybe on remote host.So, I need login the remote host first and then login another host.
- login a terminal and use ssh connect to another host is a normal operation.
What's the Scala library output if it has some color codes?
steps:- use
JSchcreate a ssh session connection. - use
ls - connect to new shell
- use
cd /varon the new shell - use
lson the new shell.
- use
//use `JSch` create a ssh session connection.
val shell = new Shell(Auth("hostname", "username", 22, "pwd"))
//use `ls`
shell.exc("ls")
//connect to new shell
val newShell = shell.newShell(Auth("xxx.xxx.xxx.xxx", "root", 22, "pwd")).right.get
//use `cd /var` on the new shell
newShell.exc("cd /var")
//use `ls` on the new shell.
newShell.exc("ls")
newShell.exit()
shell.disconnect()
The command shell.exc("ls") output perfectly because I'm use dumb property to JSch.setPtyType(). It not color codes print.
The problem of the color codes occurred at newShell.exc("ls"), it prints:
[0m[01;34mbackups[0m [01;34mcache[0m [01;34mlib[0m [01;34mlocal[0m [01;36mlock[0m [01;34mlog[0m [01;34mmail[0m [01;34mopt[0m [01;36mrun[0m [01;34mspool[0m [30;42mtmp[0m
Obviously, it contains some color codes. val newShell is generated by a bare command ssh -t -o StrictHostKeyChecking=no foo@bar, so it different with JSch ways.
Currently, I just want dispose the color codes and keep the response information only contains text:
backups cache lib local lock log mail opt run spool tmp
Hope it will be clear the question. Thanks again.
UPDATE again
Sorry for my two mistake.
TERM=dumbis needn't at all becauseJSchhas settingdumband new ssh will fooling it.setPty(true)(or -T parameter) is affect, my test was fail because of another problem that theMAGIC_SPLITconstants should be start/end with\nrather then\r\nin this mode.
What's the changes should I do?
Use setPty(true) for all message protocol, which contains JSch and ssh with -T parameter, rather then dumb because it is more clear response.
What's the problem left?
Some remote host will response a error message stdin: is not a tty after success login the remote host that is matter because of echo $? will be fail. But it is off topic already.
About this topic
Back to the question how to use ssh session with no color formatting?, the answer is use -T parameter for ssh.
And finally, thanks a lot for @sourcejedi's patience.