diff options
| author | Shireesh Anjal <shireesh@gluster.com> | 2011-03-29 21:53:36 +0530 |
|---|---|---|
| committer | Shireesh Anjal <shireesh@gluster.com> | 2011-03-29 21:53:36 +0530 |
| commit | 5c39a47fdd3987bb5eee35f7f7397ce127c8919e (patch) | |
| tree | 90c46009d9c1ac4ff8c43dbec5be0f40cef37afa /src/com.gluster.storage.management.server | |
| parent | 5ed821be2c54c9fbbbcbdc77a40edf4cc5454617 (diff) | |
Remote execution of scripts on peer machines from management server
Diffstat (limited to 'src/com.gluster.storage.management.server')
| -rw-r--r-- | src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/ServerUtil.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/ServerUtil.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/ServerUtil.java index 1d237461..b07873f1 100644 --- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/ServerUtil.java +++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/ServerUtil.java @@ -20,6 +20,11 @@ */ package com.gluster.storage.management.server.utils; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.Socket; import java.util.ArrayList; import java.util.List; @@ -55,4 +60,41 @@ public class ServerUtil { String scriptPath = servletContext.getRealPath(SCRIPT_DIR) + CoreConstants.FILE_SEPARATOR + scriptName; return scriptPath; } + + /** + * Executes given command on given server + * + * @param runInForeground + * @param serverName + * @param commandWithArgs + * @return Response from remote execution of the command + */ + public String executeOnServer(boolean runInForeground, String serverName, String commandWithArgs) { + try { + InetAddress address = InetAddress.getByName(serverName); + Socket connection = new Socket(address, 50000); + + PrintWriter writer = new PrintWriter(connection.getOutputStream(), true); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + + writer.println(commandWithArgs); + writer.println(); // empty line means end of request + + StringBuffer output = new StringBuffer(); + String line; + while (!(line = reader.readLine()).trim().isEmpty()) { + output.append(line + CoreConstants.NEWLINE); + } + + connection.close(); + return output.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static void main(String args[]) { + System.out.println(new ServerUtil().executeOnServer(true, "localhost", "ls -lrt")); + } }
\ No newline at end of file |
