How to disable echo when sending a terminal command using apache-commons-net TelnetClient

前端 未结 6 1262
醉梦人生
醉梦人生 2020-12-11 03:10

So, I have this class that uses the org.apache.commons.net.telnet.TelnetClient class. It attempts to send commands and read the response.

public class Automa         


        
6条回答
  •  庸人自扰
    2020-12-11 03:41

    By default, TelnetClient indicates to the remote system that it is a "vt100" terminal. The [m is part of a VT100 escape sequence. When constructing the TelnetClient pass "dumb" as the first argument to indicate the terminal does not support escape sequences. So

    TelnetClient telnet = new TelnetClient("dumb");
    

    Update:

    It may be the shell prompt on the remote is set to display in color. If you run "echo $PS1" you'll see what the prompt is set to and it might be something like:

    \[\e]2;\u@\h : \w\007\]\[\e[39m\e[0;49m\][\[\e[1;34m\]\u\[\e[0;37m\]@\[\e[0;32m\]\h\[\e[0;36m\] \[\e[0;33m\]\w\[\e[39m\e[0;49m\]]$ 
    

    That displays on my terminal as "[user@host directory]$ " with the brackets and dollar sign in white, the user in blue, the host in green and the directory in yellow. All very nice for people, but bad for machines.

    As soon as you log in, send "PS1='$ '\n". That will set the prompt to exactly a dollar sign followed by a space without any extra sequences.

提交回复
热议问题