Java – how to disable echo when sending terminal commands using Apache commons net telnetclient

So, I have this class using org apache. commons. net. telnet. Telnetclient class It attempts to send a command 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);
            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();
        }
    }
}

The trouble is that when I send a command, the response presupposes the previous callback For example, when I send the LS command, this is the response I read

[mls 
....

I tried to search in how to disable echo, but no one seems to have an answer So I decided to ask the community Who knows how to disable this echo?

edit

Looking at the source code of echooptionhandler puzzles me Why does the sub negotiation method only return null?

Solution

Interesting question Sum up my efforts: I didn't get normal work

You cannot write IAC do not echo directly to the data channel. This is done through commands and options like this example

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

You can sign up for telnet registerNotifHandler(new MyNotificationHandler()); Debug 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";
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>