简体   繁体   中英

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

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 AutomatedTelnetClient
{
    private TelnetClient telnet = new TelnetClient();
    private InputStream in;
    private PrintStream out;
    private String prompt = "$";

    public AutomatedTelnetClient(String server, String user, String password)
    {
        try
        {
            EchoOptionHandler echoopt = new EchoOptionHandler(false, false, false, false);
            telnet.addOptionHandler(echoopt);

            // Connect to the specified server
            telnet.connect(server, 23);

            // Get input and output stream references
            in = telnet.getInputStream();
            out = new PrintStream(telnet.getOutputStream());

            // Log the user on
            readUntil("login: ");
            write(user);
            readUntil("Password: ");
            write(password);

            // Advance to a prompt
            readUntil(prompt + " ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void su(String password)
    {
        try
        {
            write("su");
            readUntil("Password: ");
            write(password);
            prompt = "#";
            readUntil(prompt + " ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public String readUntil(String pattern)
    {
        try
        {
            char lastChar = pattern.charAt(pattern.length() - 1);
            StringBuffer sb = new StringBuffer();
            boolean found = false;
            char ch = (char) in.read();
            while (true)
            {
                System.out.print(ch);
                sb.append(ch);
                if (ch == lastChar)
                {
                    if (sb.toString().endsWith(pattern))
                    {
                        return sb.toString();
                    }
                }
                ch = (char) in.read();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public void write(String value)
    {
        try
        {
            out.println(value);
            out.flush();
            System.out.println(value);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public String sendCommand(String command)
    {
        try
        {
            write(command);
            return readUntil(prompt + " ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public void disconnect()
    {
        try
        {
            telnet.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        try
        {
            AutomatedTelnetClient telnet = new AutomatedTelnetClient(
                    "...", "...", "...");

            System.out.println("Got Connection...");

            System.out.println("run command");
            telnet.sendCommand("ls ");
            telnet.sendCommand("cd netConf");
            telnet.sendCommand("ls ");
            telnet.sendCommand("cd lanSetup");
            telnet.sendCommand("ls ");
            telnet.sendCommand("cd dhcpd");
            telnet.sendCommand("show interface 2");

            telnet.disconnect();
            System.out.println("DONE");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Trouble is, when I send a command, the response is prefaced with an echo of sorts. For instance, when I send the ls command, this is the response I read

[mls 
....

I've tried searching on how to disable echo , but no one seems to have the answer. So, I've decided to ask this community. Does anyone know how to disable this echo?

EDIT

Looking at the source code of EchoOptionHandler is confusing me. Why are the sub-negotiation methods only returning null ?

Interesting problem. To summarize my effort: I didn't get it working properly But here are some of my findings:

You cannot write IAC DON'T ECHO directly to the data channel, this is done with commands and options like this example

int[] msg = {TelnetCommand.DONT,TelnetOption.ECHO};
telnet.sendSubnegotiation(msg);

You can register telnet.registerNotifHandler(new MyNotificationHandler()); to debug the commands during connection setup

public class MyNotificationHandler implements TelnetNotificationHandler
{
  @Override
  public void receivedNegotiation(int negotiation_code, int option_code)
  {
    System.out.println("--->"+get(negotiation_code)+" "
                             +TelnetOption.getOption(option_code));
  }

  private String get(int i)
  {
    if(i==TelnetNotificationHandler.RECEIVED_DO){return "RECEIVED_DO";}
    else if(i==TelnetNotificationHandler.RECEIVED_DONT){return "RECEIVED_DONT";}
    else if(i==TelnetNotificationHandler.RECEIVED_WILL){return "RECEIVED_WILL";}
    else if(i==TelnetNotificationHandler.RECEIVED_WONT){return "RECEIVED_WONT";}
    else if(i==TelnetNotificationHandler.RECEIVED_COMMAND)
                                                    {return "RECEIVED_COMMAND";}
    return "UNKNOWN";
    }
}

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.

Have you checked about expect4J?

URL - http://code.google.com/p/expect4j

This is actually a java implementation for expect library.

Apache is buggy. EchoOptionHandler(false, false, false, false) is not working. In one case new TelnetClient("dumb") helped me. Another server obeyed only stty -echo command.

We have this problem also in our use of the java TelnetClient. It is inconsistent whether the telnet server returns the sent command in its responses.

What we are doing currently to make sure the command is not included in the response data is sending the stty command to the telnet session to turn off terminal echoing. This works for telnet servers running on linux and if the command is entered in the bash shell:

stty -echo

The telnet echo command is a simple command does not require sub-negotiation. Try:

telnet.sendCommand((byte)(TelnetCommand.DONT | TelnetOption.ECHO));

It works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM