V1.4.1 Unlike normal shell, echo part of a command must be evaluated.

master
Ng Yat Yan 1 month ago
parent 7f99e9e2f6
commit 494ea4c1bb

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.sshd</groupId>
<artifactId>echo-sshd-server</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
<name>ECHO SSH SERVER</name>
<description>Learning Apache Mina SSHD library</description>
<parent>
@ -75,6 +75,11 @@
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

@ -9,6 +9,7 @@ import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
@ -102,7 +103,7 @@ public class EchoShell implements Command, Runnable, SessionAware {
int s = r.read();
if (s == 13 || s == 10) {
boolean containsExit = Arrays.asList(command.split(";")).stream().map(cmd -> {
boolean containsExit = Arrays.asList(StringUtils.split(command, ";|&")).stream().map(cmd -> {
boolean wantsExit = false;
try {
wantsExit = replyUtil.replyToCommand(cmd.trim(), out, prompt, session);

@ -17,5 +17,4 @@ public class EchoShellFactory implements Factory<Command> {
return (Command) applicationContext.getBean("echoShell");
}
}

@ -2,6 +2,7 @@ package com.example.sshd.core;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -23,7 +24,7 @@ public class OnetimeCommand extends EchoShell {
@Override
public void run() {
try {
Arrays.asList(command.split(";")).stream().forEach(cmd -> {
Arrays.asList(StringUtils.split(command, ";|&")).stream().forEach(cmd -> {
try {
replyUtil.replyToCommand(cmd.trim(), out, "", session);
out.flush();

@ -0,0 +1,19 @@
package com.example.sshd.test;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
public class SplitTest {
@Test
public void test() {
String command = "echo hello | lscpu && uname ; whoami";
String[] splited = StringUtils.split(command,";|&");
System.out.println(Arrays.asList(splited));
Assert.assertTrue(splited.length == 4);
}
}
Loading…
Cancel
Save