|
|
@ -11,6 +11,7 @@ import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
import org.apache.commons.exec.CommandLine;
|
|
|
|
import org.apache.commons.exec.CommandLine;
|
|
|
|
import org.apache.commons.exec.DefaultExecutor;
|
|
|
|
import org.apache.commons.exec.DefaultExecutor;
|
|
|
|
|
|
|
|
import org.apache.commons.exec.ExecuteException;
|
|
|
|
import org.apache.commons.exec.PumpStreamHandler;
|
|
|
|
import org.apache.commons.exec.PumpStreamHandler;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
@ -19,9 +20,9 @@ import org.apache.sshd.server.session.ServerSession;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
@Service
|
|
|
|
public class ReplyService {
|
|
|
|
public class ReplyService {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ReplyService.class);
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ReplyService.class);
|
|
|
@ -90,14 +91,18 @@ public class ReplyService {
|
|
|
|
.replace("\\t", "\t");
|
|
|
|
.replace("\\t", "\t");
|
|
|
|
if (reply.isEmpty()) {
|
|
|
|
if (reply.isEmpty()) {
|
|
|
|
logger.info("[{}] Execute cmd for real: {} ({})", cmdHash, command.trim(), o.get());
|
|
|
|
logger.info("[{}] Execute cmd for real: {} ({})", cmdHash, command.trim(), o.get());
|
|
|
|
|
|
|
|
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
try {
|
|
|
|
CommandLine cmdLine = CommandLine.parse(command.trim());
|
|
|
|
CommandLine cmdLine = CommandLine.parse(command.trim());
|
|
|
|
DefaultExecutor executor = DefaultExecutor.builder().get();
|
|
|
|
DefaultExecutor executor = DefaultExecutor.builder().get();
|
|
|
|
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut);
|
|
|
|
PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut);
|
|
|
|
executor.setStreamHandler(streamHandler);
|
|
|
|
executor.setStreamHandler(streamHandler);
|
|
|
|
int exitValue = executor.execute(cmdLine);
|
|
|
|
int exitValue = executor.execute(cmdLine);
|
|
|
|
logger.info("[{}] Result: {} ({})", cmdHash, command.trim(), exitValue);
|
|
|
|
logger.info("[{}] Result: {} ({})", cmdHash, command.trim(), exitValue);
|
|
|
|
reply = new String(tempOut.toByteArray()).replace("\n", "\r\n");
|
|
|
|
reply = new String(tempOut.toByteArray()).replace("\n", "\r\n");
|
|
|
|
|
|
|
|
} catch (ExecuteException e) {
|
|
|
|
|
|
|
|
logger.info("[{}] Execute cmd failed: {} ({})", cmdHash, command.trim(), o.get(), e);
|
|
|
|
|
|
|
|
}
|
|
|
|
out.write(String.format("\r\n%s\r\n%s", reply, prompt).getBytes());
|
|
|
|
out.write(String.format("\r\n%s\r\n%s", reply, prompt).getBytes());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
logger.info("[{}] Known pattern detected: {} ({})", cmdHash, command.trim(), o.get());
|
|
|
|
logger.info("[{}] Known pattern detected: {} ({})", cmdHash, command.trim(), o.get());
|
|
|
|