final String obfuscatedCmd = cmd.toCommandLine(os, true);
logger.info("Starting command [{}] on [{}]", obfuscatedCmd, this);
try {
final TelnetClient tc = new TelnetClient();
tc.setConnectTimeout(connectionTimeoutMillis);
tc.addOptionHandler(new WindowSizeOptionHandler(299, 25, true, false, true, false));
logger.info("Connecting to telnet://{}@{}", username, address);
tc.connect(address, port);
final InputStream stdout = tc.getInputStream();
final OutputStream stdin = tc.getOutputStream();
final PipedInputStream callersStdout = new PipedInputStream();
final PipedOutputStream toCallersStdout = new PipedOutputStream(callersStdout);
final ByteArrayOutputStream outputBuf = new ByteArrayOutputStream();
final int[] exitValue = new int[1];
exitValue[0] = -1;
final Thread outputReaderThread = new Thread("Telnet output reader") {
@Override
public void run() {
try {
receive(stdout, outputBuf, toCallersStdout, "ogin:");
send(stdin, username);
receive(stdout, outputBuf, toCallersStdout, "assword:");
send(stdin, password);
receive(stdout, outputBuf, toCallersStdout, ">", "ogon failure");
send(stdin, "PROMPT " + DETECTABLE_WINDOWS_PROMPT);
// We must wait for the prompt twice; the first time is an echo of the PROMPT command,
// the second is the actual prompt
receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);
receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);
if (workingDirectory != null) {
send(stdin, "CD /D " + workingDirectory.getPath());
receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);
}
send(stdin, cmd.toCommandLine(getHostOperatingSystem(), false));
receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT);
send(stdin, "ECHO \"" + ERRORLEVEL_PREAMBLE + "%errorlevel%" + ERRORLEVEL_POSTAMBLE);
receive(stdout, outputBuf, toCallersStdout, ERRORLEVEL_POSTAMBLE);
receive(stdout, outputBuf, toCallersStdout, ERRORLEVEL_POSTAMBLE);
String outputBufStr = outputBuf.toString();
int preamblePos = outputBufStr.indexOf(ERRORLEVEL_PREAMBLE);
int postamblePos = outputBufStr.indexOf(ERRORLEVEL_POSTAMBLE);
if (preamblePos >= 0 && postamblePos >= 0) {
String errorlevelString = outputBufStr.substring(preamblePos + ERRORLEVEL_PREAMBLE.length(), postamblePos);
logger.debug("Errorlevel string found: {}", errorlevelString);
try {
synchronized (exitValue) {
exitValue[0] = Integer.parseInt(errorlevelString);
}
} catch (NumberFormatException exc) {
logger.error("Cannot parse errorlevel in Windows output: " + outputBuf);
}
} else {
logger.error("Cannot find errorlevel in Windows output: " + outputBuf);
}
} catch (IOException exc) {
throw new RuntimeIOException(format("Cannot start command [%s] on [%s]", obfuscatedCmd, CifsTelnetConnection.this), exc);
} finally {
closeQuietly(toCallersStdout);
}
}
};
outputReaderThread.setDaemon(true);
outputReaderThread.start();
return new OverthereProcess() {
@Override
public synchronized OutputStream getStdin() {
return stdin;
}
@Override
public synchronized InputStream getStdout() {
return callersStdout;
}
@Override
public synchronized InputStream getStderr() {
return new ByteArrayInputStream(new byte[0]);
}
@Override
public synchronized int waitFor() {
if (!tc.isConnected()) {
return exitValue[0];
}
try {
try {
outputReaderThread.join();
} finally {
disconnect();
}
return exitValue[0];
} catch (InterruptedException exc) {
throw new RuntimeIOException(format("Cannot start command [%s] on [%s]", obfuscatedCmd, CifsTelnetConnection.this), exc);
}
}
@Override
public synchronized void destroy() {
if (!tc.isConnected()) {
return;
}
disconnect();
}
private synchronized void disconnect() {
try {
tc.disconnect();
logger.info("Disconnected from {}", CifsTelnetConnection.this);
closeQuietly(toCallersStdout);
} catch (IOException exc) {
throw new RuntimeIOException(format("Cannot disconnect from %s", CifsTelnetConnection.this), exc);
}
}
@Override
public synchronized int exitValue() {
if (tc.isConnected()) {
throw new IllegalThreadStateException(format("Process for command [%s] on %s is still running", obfuscatedCmd, CifsTelnetConnection.this));
}
synchronized (exitValue) {
return exitValue[0];