4

I am writing telnet client for cisco router using apache.commons.net.telnet. But i have problem. Here is code sample:

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 and shows prompt for password entering. But is other cases it works incorrectly - hangs by reading from telnet input stream. 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!

DenisM
  • 223
  • 4
  • 13
  • Use `tcpdump` or other packet sniffer to find out why. It's impossible to tell from this level of code. – Greg Hewgill Mar 31 '12 at 19:47
  • Can you describe the problem more precisely? Are you saying you get no output at all? Or you get some output and then it hangs? Or what? – David Schwartz Mar 31 '12 at 20:43
  • I have tried to use wireshark. It shows, that my router sends telnet data (prompt for password entering), but my application do not response on this packets. After triple prompt was received (new prompt is sent if timeout-error occurs), my app prints all 3 prompt and then bad-password error. I don't understand when it happens. May be case is in telnet options? – DenisM Mar 31 '12 at 20:56
  • Does the `read` function *ever* return, but you call it again and it blocks? Or does it *never* return even once? – David Schwartz Apr 01 '12 at 01:13
  • Is some cases read function returns immediately, and one prompt are shown. In other cases this 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. – DenisM Apr 01 '12 at 05:19
  • In the case where you have a problem, is it that `read` never returns even once for that run? Or does it return, but you call `read` again? – David Schwartz Apr 02 '12 at 00:10
  • I call telnetClient.getInputStream().read(buff) only once. In problem case this function returns after long delay. It looks like it stores received data in internal buffer, then calls internal read-function again and again and returns all data only after password-error receiving. – DenisM Apr 02 '12 at 07:07
  • If i have no problem, telnet output is `"User Access Verification Password: "`. In problem case telnet output is `"User Access Verification Password: % Password: timeout expired! Password: % Password: timeout expired! Password: % Password: timeout expired! % Bad passwords"` – DenisM Apr 03 '12 at 05:37
  • I don't understand why it doesn't work. But i applied this approach http://stackoverflow.com/questions/1195809/looking-for-java-telnet-emulator with `readUntil` function, and it works. – DenisM Apr 03 '12 at 17:31

1 Answers1

1

I can reproduce this problem every time.

The problem can be worked-around by changing your read buffer size to 1 byte.

This accounts for why the readUntil() function from Looking for Java Telnet emulator works, at it simply calls read() for 1 byte.

That said, does this indicate a bug in org.apache.commons.net.telnet.TelnetClient?

Edit: Rolled back to an earlier version of Commons Net and the problem disappeared !

Community
  • 1
  • 1
Phaedrus
  • 487
  • 7
  • 15