java – 如何使用apache-commons-net TelnetClient发送终端命令时如何禁用echo

所以,我有这个类使用 org.apache.commons.net.telnet.TelnetClient类.它尝试发送命令并读取响应.
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();
        }
    }
}

麻烦的是,当我发送命令时,响应以前面的回调为前提.例如,当我发送ls命令时,这是我读的响应

[mls 
....

我在how to disable echo尝试搜索,但没有人似乎有答案.所以,我决定问这个社区.有谁知道如何禁用这个回声?

编辑

看看EchoOptionHandler的源代码让我感到困惑.为什么子协商方法只返回n​​ull?

解决方法

有趣的问题.总结我的努力:我没有得到正常工作
但这里有一些我的发现:

您不能将IAC DO NOT ECHO直接写入数据通道,这是通过像这个例子的命令和选项来完成的

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

你可以注册telnet.registerNotifHandler(new MyNotificationHandler());在连接设置期间调试命令

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";
    }
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...