You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Sayor Basu <sa...@gmail.com> on 2012/05/10 19:51:09 UTC

[NET] Windows Telnet Session restarts when sending commands

Hi!

I am creating Telnet sessions to Windows as well as Linux servers to mount
a shared drive from my box, install a product using a single command line
and then check if all the services are running to verify by either tasklist
or ps and then looking for all the services from the returned response.
The Linux servers dont seem to have any problems, however on the Windows
servers I see that the session is restarted randomly - I get to see the
"Welcome to Microsoft Telnet Server." after writing a command to the
session. After this happens, even trying to send any more commands do not
help as I am not able to get the result of the command from the session.

I am using a sendCommand method which in turn does a write() and
readUntil() as in the code at the end.

So after the write, I get the Telnet welcome and when I am reading till the
system prompt e.g. ">" on Windows I do not get a response.
I have checked that the server does not reach the maximum no. of sessions
allowed or not reaching timeout and I have tried resending the command, but
that also does not produce any output.

Any suggestions on what I could try to get over this problem? Please let me
know if I can provide more information.

Thanks!


Code used for the writing a string to Telnet session command prompt and
reading from the prompt:


public void write(String value, Logger logger) throws Exception {
        try {
            out.println(value);
            out.flush();
            System.out.println(value);
        } catch (Exception ex) {
            logger.println("Error while trying to write " + value +
".Details: " + Helper.getStackTraceAsString(ex));
            throw new Exception(ex);
        }
    }

public String readUntil(String pattern, Logger logger) throws Exception {
        StringBuffer sb = new StringBuffer();
        try {
            char ch = (char) in.read();
            int read = in.read();
            while (read != -1) {
                ch = (char) read;
                System.out.print(ch);
                sb.append(ch);
                if (sb.toString().endsWith(pattern)) {
                    return sb.toString();
                }
                read = in.read();
            }
        } catch (Exception ex) {
            logger.println("Error while trying to read until " + pattern +
".Details: " + Helper.getStackTraceAsString(ex));
            throw new Exception(ex);
        }
        throw new Exception("Telnet response doesn't end with '" + pattern
+ "' pattern. Response text : /n" + sb.toString());
    }