I am writing telnet client for cisco router using apache.commons.net.telnet. But i have problem. Here is code sample:
Java Code:
static TelnetClient telnetClient = new TelnetClient();

public static void main(String[] args) throws IOException {
    setOptionHandlers();
    telnetClient.connect("192.168.127.100");
    read();
    telnetClient.disconnect();
}

private static void setOptionHandlers() throws IOException {
    ArrayList<TelnetOptionHandler> optionHandlers = 
        new ArrayList<TelnetOptionHandler>();
    optionHandlers.add(new TerminalTypeOptionHandler("VT100", false, false, true, false));
    optionHandlers.add(new EchoOptionHandler(true, false, true, false));
    optionHandlers.add(new SuppressGAOptionHandler(true, true, true, true));
    for (TelnetOptionHandler handler : optionHandlers) {
        try {
            telnetClient.addOptionHandler(handler);
        }
        catch (InvalidTelnetOptionException e) {
            System.err.println("Error registering option handler "
                    + handler.getClass().getSimpleName());
        }
    }
}

public static void write(byte[] data) throws IOException {
    telnetClient.getOutputStream().write(data);
    telnetClient.getOutputStream().flush();
}

public static void read() throws IOException {
    System.out.println("Read");
    byte[] buff = new byte[1024];
    int read;
    if((read = telnetClient.getInputStream().read(buff)) > 0) {
        System.out.println(new String(buff, 0, read));
    }
    System.out.println("read="+read);
}
In some cases it works correctly (read function returns immediately,) and shows prompt for password entering. But is other cases it works incorrectly - read function hangs some time (triple password time-out, i think) and then returns, and 3 prompts, each with 'Timeout exceeded' message, and bad-password error are shown. It looks like telnetClient.getInputStream().read(buff) stores received data in internal buffer, then calls internal read-function again and again and returns all data only after password-error receiving.
Run conditions are the same. Why do I get this situation? If anyone has tips for writing cisco telnet client, i'll be glad to hear them!