views:

460

answers:

2

I have a Java program that runs on Linux and telnets into a remote server using org.apache.commons.net.telnet.TelnetClient and performs a few commands. The problem is that it hangs intermittently when it gets to an output display that asks the users to “press any key to continue…” The program hangs on this about 1 out of every 10 tims it runs and out of the 7 servers I run it on only 3 of the servers have problems. Also, when I run the same program on a windows box it works all the time.

I was wondering if anyone else has encountered a problem like this?

On a test server I can get it to hang every time to test with. I have tried to send in other commands that won’t cause it to hang but no luck. I have tried all the carrage return, line feed, adding a character and putting in a line feed. Nothing seems to make it client continue.

Forgot to mention that flushing the buffer what the first thing I thought of. I put the flush command anywhere I thought it might hlep.
I will also mention that when I run it and watch the output from the write line it does find the "press any key" and keeps going but hangs the terminal does not continue.

CODE WHERE I MAKE THE CALL:

  readUntil("X) Exit (no report)");
  write("C", false);
  out.flush();

  readUntil("continue....");

  // write this for all servers.
  write("", true);
  out.flush();

  readUntil("X) Exit");
  write("X", false);


/*
 * This method is used to read the command line until the pattern that was 
 * passed in is found.
 */
public String readUntil(String pattern) throws Exception {
 try {
  String tempString;
  char lastChar = pattern.charAt(pattern.length() - 1);
  StringBuffer sb = new StringBuffer();
  //boolean found = false;
  char ch = (char) in.read();
  while (true) 
  {
   // NOTE: Turn line below on to watch the program perform the telnet
   System.out.print(ch);

   sb.append(ch);
   tempString = sb.toString();
   if (ch == lastChar) {
    if (tempString.endsWith(pattern)) 
    {
     // log to file
     logFileWriter.write(tempString);
     logFileWriter.flush();
     return tempString;
    }
   }
   ch = (char) in.read();
  }
 }
 catch (Exception e) {
  e.printStackTrace();
  throw e;
 }
}

/*
 * writes the String passed in to the command line.
 * boolean userWriteln: true - use the return key after the command, false - just type the 
 * command with NO enter key
 */
public void write(String value, boolean useWriteln) 
{

 System.out.println("WRITTING '" + value + "'");

 try {
  if (useWriteln)
  {
   out.println(value);
  }
  else
  {
   out.print(value);
  }
  out.flush();
  System.out.println(value);
 }
 catch (Exception e) {
  e.printStackTrace();
 }
}

StackTrace: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:129) at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) at java.io.BufferedInputStream.read(BufferedInputStream.java:237) at java.io.FilterInputStream.read(FilterInputStream.java:66) at java.io.PushbackInputStream.read(PushbackInputStream.java:122) at org.apache.commons.net.io.FromNetASCIIInputStream.__read(FromNetASCIIInputStream.java:77) at org.apache.commons.net.io.FromNetASCIIInputStream.read(FromNetASCIIInputStream.java:175) at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) at java.io.BufferedInputStream.read(BufferedInputStream.java:237) at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:122) at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:564) at java.lang.Thread.run(Thread.java:619)

WHERE IT HANGS: english 1 6000 4462 26 % 13826 11056 20 %

Calls answered since Thu Jun  4, 2009  3:11 am: 41245

Press any key to continue....

+3  A: 

There might be several reasons:

  1. You're not flushing your output (the input of the remote command), so the "any key" is never sent.

  2. The program tries to send you some data and you never read your input (the output of the remote command). Note that you must do this in a second thread since the I/O usually happens "at the same time" and one side will block if you don't handle the other side timely enough.

  3. Maybe you experience a problem because the app turns the terminal into "RAW mode". But flushing your output should fix that :/

Aaron Digulla
I have looked at both these and I am flushing my buffer. (many times and in many places). I also looked for additional text but did not see any.
Ben
A: 

Have you considered alternatives to Apache Commons?

  1. If you only need to execute commands across a telnet session, consider using NIST's Expect.
  2. If you need a Java solution, try JExpect.
  3. Another open-source telnet client is also available here.
Dave Jarvis
I have looked into the other options. I wanted to do it in Java to go with the most supported solution. I think commons is the best of the available technologies.
Ben