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.
JSchSSH library, there is some setting to explain:
setPtyType("dumb"), the parameter
dumbsetting the session with
dumbemulation terminal similar with
dumbemulation terminal is not any colors codes in result. Use
echo $TERMwill print
dumbafter setting this property.
setPty(bool enable)default is
falsebut if I explicit setting
setPty(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?
JSchcreate a ssh session connection.
- connect to new shell
cd /varon the new shell
lson the new shell.
//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()
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
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.
Sorry for my two mistake.
TERM=dumbis needn't at all because
dumband new ssh will fooling it.
setPty(true)(or -T parameter) is affect, my test was fail because of another problem that the
MAGIC_SPLITconstants should be start/end with
\r\nin this mode.
What's the changes should I do?
setPty(true) for all message protocol, which contains
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
And finally, thanks a lot for @sourcejedi's patience.