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 {

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 {
        catch (InvalidTelnetOptionException e) {
            System.err.println("Error registering option handler "
                    + handler.getClass().getSimpleName());

public static void write(byte[] data) throws IOException {

public static void read() throws IOException {
    byte[] buff = new byte[1024];
    int read;
    if((read = telnetClient.getInputStream().read(buff)) > 0) {
        System.out.println(new String(buff, 0, 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!