Run Shell Command

Last modified by Vincent Massol on 2021/03/18 11:29

application_osx_terminalAllows users with programming permission to run a command on the bash shell on the server
TypeSnippet
Category
Developed by

xwiki:XWiki.cjdelisle

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

 Only works on a server with Linux. 

Security: None (no protection against cross site request forgery, this should be removed after use.)

This script can create processes which do not stop, it's up to you to then find and kill the processes.

{{groovy}}
if (xwiki.hasAccessLevel("programming", xcontext.getUser(), "XWiki.XWikiPreferences")) {
  String shellcmd = request.getParameter("command");
  println("{{html}}<form action='' method='POST'><input type='text' name='command' size=50 />");
  println("<input type='submit' value='Run' /></form>{{/html}}\n\n")
if (shellcmd != null ) {
    String[] cmd = ["sh", "-c", shellcmd];
    stdout = new java.io.BufferedReader(
    new java.io.InputStreamReader(Runtime.getRuntime().exec(cmd).getInputStream()));
    println("{{code language=none}}")
  while ((s = stdout.readLine()) != null) {
      println(s);
  }
    println("{{/code}}")
}
}
{{/groovy}}

Get Connected