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>

@ -27,5 +27,5 @@ public class Boot {
System.setProperty("logging.config", configDirectory + "/log4j2.xml");
}
SpringApplication.run(Boot.class, args);
}
}
}

@ -21,7 +21,7 @@ public class AppConfig {
public Map<String, Session> remoteSessionMapping() {
return Collections.synchronizedMap(new HashMap<>());
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public Map<String, String> ipInfoMapping() {

@ -41,7 +41,7 @@ public class SshConfig {
@Value("${ssh-server.hash-replies.location}")
private String hashReplies;
@Value("${ssh-server.regex-mapping.location}")
private String regexMapping;
@ -63,7 +63,7 @@ public class SshConfig {
});
sshd.setShellFactory(applicationContext.getBean(EchoShellFactory.class));
sshd.setCommandFactory(command -> applicationContext.getBean(OnetimeCommand.class, command));
sshd.start();
sshd.getSessionFactory().addListener(applicationContext.getBean(EchoSessionListener.class));
return sshd;
@ -78,7 +78,7 @@ public class SshConfig {
prop.load(stream);
return prop;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public Properties regexMapping() throws IOException {

@ -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;
@ -73,7 +74,7 @@ public class EchoShell implements Command, Runnable, SessionAware {
protected String remoteIpAddress() {
String remoteIpAddress = "";
if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) {
InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress();
remoteIpAddress = remoteAddress.getAddress().getHostAddress();
@ -82,7 +83,7 @@ public class EchoShell implements Command, Runnable, SessionAware {
}
return remoteIpAddress;
}
@Override
public void destroy() {
thread.interrupt();
@ -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