我有一个在 Linux 上运行的 Java 程序,并使用org.apache.commons.net.telnet.TelnetClient
并执行一些命令远程登录到远程服务器。问题是当它到达一个要求用户“按任意键继续......”的输出显示时,它会间歇性挂起只有 3 台服务器有问题。此外,当我在 Windows 机器上运行相同的程序时,它一直都在工作。
我想知道是否有其他人遇到过这样的问题?
在测试服务器上,每次测试时我都可以让它挂起。我试图发送不会导致它挂起但没有运气的其他命令。我已经尝试了所有的回车、换行、添加字符和换行。似乎没有什么可以让它客户端继续。
忘了提到刷新缓冲区是我想到的第一件事。我将flush命令放在我认为可能有用的任何地方。
我还会提到,当我运行它并观察写入行的输出时,它确实找到了“按任意键”并继续运行,但挂起终端不会继续。
我打电话的代码:
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:在 java.io.BufferedInputStream.fill(BufferedInputStream.java) 的 java.net.SocketInputStream.read(SocketInputStream.java:129) 的 java.net.SocketInputStream.socketRead0(Native Method) 读取超时:218) 在 java.io.BufferedInputStream.read(BufferedInputStream.java:237) 在 java.io.FilterInputStream.read(FilterInputStream.java:66) 在 java.io.PushbackInputStream.read(PushbackInputStream.java:122) 在 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) 在 java.io.BufferedInputStream.read(BufferedInputStream.java:237) 在 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)
挂在哪里:英语 1 6000 4462 26 % 13826 11056 20 %
Calls answered since Thu Jun 4, 2009 3:11 am: 41245
按任意键继续....