diff --git a/pom.xml b/pom.xml index 18bcc1c..68b08b3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.example.sshd echo-sshd-server - 2.1.0 + 2.2.0 ECHO SSH SERVER Learning Apache Mina SSHD library diff --git a/src/main/java/com/example/sshd/service/EchoComponent.java b/src/main/java/com/example/sshd/service/EchoComponent.java index 65518ba..4cf7d41 100644 --- a/src/main/java/com/example/sshd/service/EchoComponent.java +++ b/src/main/java/com/example/sshd/service/EchoComponent.java @@ -31,6 +31,9 @@ public class EchoComponent extends AbstractComponent { @Autowired XmppComponentConfig xmppComponentConfig; + @Autowired + ReplyService replyService; + @Autowired @Qualifier("userAdminCache") private volatile Cache userAdminCache; @@ -111,7 +114,7 @@ public class EchoComponent extends AbstractComponent { doEcho(inMsg, "chgpasswd "); break; default: - doEcho(inMsg, null); + doEcho(inMsg, replyService.executeShellCommand(inMsg.getBody().trim())); break; } } diff --git a/src/main/java/com/example/sshd/service/ReplyService.java b/src/main/java/com/example/sshd/service/ReplyService.java index f74bd73..ac041a8 100644 --- a/src/main/java/com/example/sshd/service/ReplyService.java +++ b/src/main/java/com/example/sshd/service/ReplyService.java @@ -112,19 +112,7 @@ public class ReplyService { String reply = hashReplies.getProperty(o.get().getRight(), "").replace("\\r", "\r").replace("\\n", "\n") .replace("\\t", "\t"); if (reply.isEmpty()) { - logger.info("[{}] Execute cmd for real: {} ({})", cmdHash, command.trim(), o.get()); - ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); - try { - CommandLine cmdLine = CommandLine.parse(command.trim()); - DefaultExecutor executor = DefaultExecutor.builder().get(); - PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut); - executor.setStreamHandler(streamHandler); - int exitValue = executor.execute(cmdLine); - logger.info("[{}] Result: {} ({})", cmdHash, command.trim(), exitValue); - reply = new String(tempOut.toByteArray()).replace("\n", "\r\n"); - } catch (ExecuteException e) { - logger.info("[{}] Execute cmd failed: {} ({})", cmdHash, command.trim(), o.get(), e); - } + reply = executeShellCommand(command.trim()); out.write(String.format("\r\n%s\r\n%s", reply, prompt).getBytes()); } else { logger.info("[{}] Known pattern detected: {} ({})", cmdHash, command.trim(), o.get()); @@ -139,4 +127,22 @@ public class ReplyService { } return false; } + + public String executeShellCommand(String command) throws IOException { + String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase(); + logger.info("[{}] Execute cmd for real: {}", cmdHash, command.trim()); + ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); + try { + CommandLine cmdLine = CommandLine.parse(command.trim()); + DefaultExecutor executor = DefaultExecutor.builder().get(); + PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut); + executor.setStreamHandler(streamHandler); + int exitValue = executor.execute(cmdLine); + logger.info("[{}] Result: {} ({})", cmdHash, command.trim(), exitValue); + return new String(tempOut.toByteArray()).replace("\n", "\r\n"); + } catch (ExecuteException e) { + logger.info("[{}] Execute cmd failed: {}", cmdHash, command.trim(), e); + } + return null; + } }