]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java
Improve SSH
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / RemoteExec.java
index d07e6ab6cf4273924f87ab1ecf9e18088c9e648c..39305fcc4760e9c3e55cb45c06cf2e68f91dab18 100644 (file)
@@ -48,6 +48,8 @@ public class RemoteExec extends AbstractJschTask {
        private Resource stdOut = null;
        private ExecutionResources executionResources;
 
+       private String user;
+
        /**
         * If set, stdout is written to it as a list of lines. Cleared before each
         * run.
@@ -96,9 +98,17 @@ public class RemoteExec extends AbstractJschTask {
                }
 
                if (forceShell) {
-                       if (commandToUse.indexOf(';') >= 0
-                                       || commandToUse.indexOf('\n') >= 0) {
-                               StringTokenizer st = new StringTokenizer(commandToUse, ";\n");
+                       // for the time being do not interpret both \n and ;
+                       // priority to \n
+                       // until we know how to parse ; within ""
+                       if (commandToUse.indexOf('\n') >= 0) {
+                               StringTokenizer st = new StringTokenizer(commandToUse, "\n");
+                               while (st.hasMoreTokens()) {
+                                       String cmd = st.nextToken();
+                                       commandsToUse.add(cmd);
+                               }
+                       } else if (commandToUse.indexOf(';') >= 0) {
+                               StringTokenizer st = new StringTokenizer(commandToUse, ";");
                                while (st.hasMoreTokens()) {
                                        String cmd = st.nextToken();
                                        commandsToUse.add(cmd);
@@ -109,6 +119,19 @@ public class RemoteExec extends AbstractJschTask {
                        commandToUse = null;
                }
 
+               // run as user
+               if (user != null) {
+                       if (commandsToUse.size() > 0) {
+                               commandsToUse.add(0, "su - " + user);
+                               commandsToUse.add("exit");
+                       } else {
+                               if (command.indexOf('\"') >= 0)
+                                       throw new SlcException(
+                                                       "Don't know how to su a command with \", use shell instead.");
+                               commandToUse = "su - " + user + " -c \"" + command + "\"";
+                       }
+               }
+
                // execute command(s)
                if (commandToUse != null) {
                        if (commandsToUse.size() != 0)
@@ -400,4 +423,8 @@ public class RemoteExec extends AbstractJschTask {
                this.executionResources = executionResources;
        }
 
+       public void setUser(String user) {
+               this.user = user;
+       }
+
 }