Исходный код вики Run Shell Command
Редактировал(а) Dmitriy Rybakov 05.08.2025, 13:07
Скрыть последних авторов
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{groovy}} |
| 2 | import org.xwiki.security.authorization.Right; | ||
| 3 | import org.apache.velocity.tools.generic.EscapeTool; | ||
| 4 | |||
| 5 | if (services.security.authorization.hasAccess(Right.PROGRAM)) { | ||
| 6 | def escapeTool = new EscapeTool(); | ||
| 7 | def placeHolder = escapeTool.xml(services.localization.render('admintools.server.tools.shellCommand.placeholder')); | ||
| 8 | def buttonLabel = services.localization.render('admintools.server.tools.shellCommand.run'); | ||
| 9 | def shellCmd = request.getParameter("command"); | ||
| 10 | def htmlForm = """ | ||
| 11 | <form class="form-horizontal" action="" method="POST"> | ||
| 12 | <input type="hidden" name="form_token" value="${services.csrf.token}" /> | ||
| 13 | <div class="form-group"> | ||
| 14 | <div class="col-sm-10"> | ||
| 15 | <input type="text" name="command" value="${shellCmd != null ? escapeTool.xml(shellCmd) : ''}" class="form-control col-md-12" style="width: 100%;" placeholder="${placeHolder}"> | ||
| 16 | </div> | ||
| 17 | <div class="col-sm-2"> | ||
| 18 | <button type="submit" class="btn btn-primary" style="margin:0;">${buttonLabel}</button> | ||
| 19 | </div> | ||
| 20 | </div> | ||
| 21 | </form>"""; | ||
| 22 | println("{{warning}}${services.localization.render('admintools.server.tools.shellCommand.warning')}{{/warning}}\n\n"); | ||
| 23 | println("{{html}}${htmlForm}{{/html}}\n\n"); | ||
| 24 | def tokenValid = services.csrf.isTokenValid(request.getParameter('form_token')); | ||
| 25 | if (shellCmd != null) { | ||
| 26 | if (!tokenValid) { | ||
| 27 | println("{{error}}${services.localization.render('admintools.server.tools.shellCommand.invalidToken')}{{/error}}\n\n"); | ||
| 28 | } else { | ||
| 29 | String[] cmd = ["sh", "-c", shellCmd]; | ||
| 30 | stdout = new java.io.BufferedReader(new java.io.InputStreamReader(Runtime.getRuntime().exec(cmd).getInputStream())); | ||
| 31 | println("{{code language=none}}") | ||
| 32 | while ((s = stdout.readLine()) != null) { | ||
| 33 | println(s); | ||
| 34 | } | ||
| 35 | println("{{/code}}") | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | {{/groovy}} |