command-engine.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>Run shell commands from your instance &#8212; Searx Documentation (Searx-1.1.0.tex)</title>
  8. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  9. <link rel="stylesheet" type="text/css" href="../_static/searx.css" />
  10. <link rel="stylesheet" type="text/css" href="../_static/tabs.css" />
  11. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  12. <script src="../_static/jquery.js"></script>
  13. <script src="../_static/underscore.js"></script>
  14. <script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
  15. <script src="../_static/doctools.js"></script>
  16. <script src="../_static/sphinx_highlight.js"></script>
  17. <link rel="index" title="Index" href="../genindex.html" />
  18. <link rel="search" title="Search" href="../search.html" />
  19. <link rel="next" title="Search in indexers" href="indexer-engines.html" />
  20. <link rel="prev" title="How to create private engines" href="private-engines.html" />
  21. </head><body>
  22. <div class="related" role="navigation" aria-label="related navigation">
  23. <h3>Navigation</h3>
  24. <ul>
  25. <li class="right" style="margin-right: 10px">
  26. <a href="../genindex.html" title="General Index"
  27. accesskey="I">index</a></li>
  28. <li class="right" >
  29. <a href="../py-modindex.html" title="Python Module Index"
  30. >modules</a> |</li>
  31. <li class="right" >
  32. <a href="indexer-engines.html" title="Search in indexers"
  33. accesskey="N">next</a> |</li>
  34. <li class="right" >
  35. <a href="private-engines.html" title="How to create private engines"
  36. accesskey="P">previous</a> |</li>
  37. <li class="nav-item nav-item-0"><a href="../index.html">Searx Documentation (Searx-1.1.0.tex)</a> &#187;</li>
  38. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Administrator documentation</a> &#187;</li>
  39. <li class="nav-item nav-item-this"><a href="">Run shell commands from your instance</a></li>
  40. </ul>
  41. </div>
  42. <div class="document">
  43. <div class="documentwrapper">
  44. <div class="bodywrapper">
  45. <div class="body" role="main">
  46. <section id="run-shell-commands-from-your-instance">
  47. <h1>Run shell commands from your instance<a class="headerlink" href="#run-shell-commands-from-your-instance" title="Permalink to this heading">¶</a></h1>
  48. <p>Command line engines are custom engines that run commands in the shell of the
  49. host. In this article you can learn how to create a command engine and how to
  50. customize the result display.</p>
  51. <section id="the-command">
  52. <h2>The command<a class="headerlink" href="#the-command" title="Permalink to this heading">¶</a></h2>
  53. <p>When specifyng commands, you must make sure the commands are available on the
  54. searx host. Searx will not install anything for you. Also, make sure that the
  55. <code class="docutils literal notranslate"><span class="pre">searx</span></code> user on your host is allowed to run the selected command and has
  56. access to the required files.</p>
  57. </section>
  58. <section id="access-control">
  59. <h2>Access control<a class="headerlink" href="#access-control" title="Permalink to this heading">¶</a></h2>
  60. <p>Be careful when creating command engines if you are running a public
  61. instance. Do not expose any sensitive information. You can restrict access by
  62. configuring a list of access tokens under tokens in your <code class="docutils literal notranslate"><span class="pre">settings.yml</span></code>.</p>
  63. </section>
  64. <section id="available-settings">
  65. <h2>Available settings<a class="headerlink" href="#available-settings" title="Permalink to this heading">¶</a></h2>
  66. <ul class="simple">
  67. <li><p><code class="docutils literal notranslate"><span class="pre">command</span></code>: A comma separated list of the elements of the command. A special
  68. token <code class="docutils literal notranslate"><span class="pre">{{QUERY}}</span></code> tells searx where to put the search terms of the
  69. user. Example: <code class="docutils literal notranslate"><span class="pre">['ls',</span> <span class="pre">'-l',</span> <span class="pre">'-h',</span> <span class="pre">'{{QUERY}}']</span></code></p></li>
  70. <li><p><code class="docutils literal notranslate"><span class="pre">query_type</span></code>: The expected type of user search terms. Possible values:
  71. <code class="docutils literal notranslate"><span class="pre">path</span></code> and <code class="docutils literal notranslate"><span class="pre">enum</span></code>. <code class="docutils literal notranslate"><span class="pre">path</span></code> checks if the uesr provided path is inside the
  72. working directory. If not the query is not executed. <code class="docutils literal notranslate"><span class="pre">enum</span></code> is a list of
  73. allowed search terms. If the user submits something which is not included in
  74. the list, the query returns an error.</p></li>
  75. <li><p><code class="docutils literal notranslate"><span class="pre">delimiter</span></code>: A dict containing a delimiter char and the “titles” of each
  76. element in keys.</p></li>
  77. <li><p><code class="docutils literal notranslate"><span class="pre">parse_regex</span></code>: A dict containing the regular expressions for each result
  78. key.</p></li>
  79. <li><p><code class="docutils literal notranslate"><span class="pre">query_enum</span></code>: A list containing allowed search terms if <code class="docutils literal notranslate"><span class="pre">query_type</span></code> is
  80. set to <code class="docutils literal notranslate"><span class="pre">enum</span></code>.</p></li>
  81. <li><p><code class="docutils literal notranslate"><span class="pre">working_dir</span></code>: The directory where the command has to be executed. Default:
  82. <code class="docutils literal notranslate"><span class="pre">.</span></code></p></li>
  83. <li><p><code class="docutils literal notranslate"><span class="pre">result_separator</span></code>: The character that separates results. Default: <code class="docutils literal notranslate"><span class="pre">\n</span></code></p></li>
  84. </ul>
  85. </section>
  86. <section id="customize-the-result-template">
  87. <h2>Customize the result template<a class="headerlink" href="#customize-the-result-template" title="Permalink to this heading">¶</a></h2>
  88. <p>There is a default result template for displaying key-value pairs coming from
  89. command engines. If you want something more tailored to your result types, you
  90. can design your own template.</p>
  91. <p>Searx relies on <a class="reference external" href="https://jinja.palletsprojects.com/">Jinja2</a> for
  92. templating. If you are familiar with Jinja, you will not have any issues
  93. creating templates. You can access the result attributes with <code class="docutils literal notranslate"><span class="pre">{{</span>
  94. <span class="pre">result.attribute_name</span> <span class="pre">}}</span></code>.</p>
  95. <p>In the example below the result has two attributes: <code class="docutils literal notranslate"><span class="pre">header</span></code> and <code class="docutils literal notranslate"><span class="pre">content</span></code>.
  96. To customize their diplay, you need the following template (you must define
  97. these classes yourself):</p>
  98. <div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;result&quot;</span><span class="p">&gt;</span>
  99. <span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;result-header&quot;</span><span class="p">&gt;</span>
  100. {{ result.header }}
  101. <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  102. <span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;result-content&quot;</span><span class="p">&gt;</span>
  103. {{ result.content }}
  104. <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  105. <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  106. </pre></div>
  107. </div>
  108. <p>Then put your template under <code class="docutils literal notranslate"><span class="pre">searx/templates/{theme-name}/result_templates</span></code>
  109. named <code class="docutils literal notranslate"><span class="pre">your-template-name.html</span></code>. You can select your custom template with the
  110. option <code class="docutils literal notranslate"><span class="pre">result_template</span></code>.</p>
  111. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">your engine name</span><span class="w"></span>
  112. <span class="w"> </span><span class="nt">engine</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">command</span><span class="w"></span>
  113. <span class="w"> </span><span class="nt">result_template</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">your-template-name.html</span><span class="w"></span>
  114. </pre></div>
  115. </div>
  116. </section>
  117. <section id="examples">
  118. <h2>Examples<a class="headerlink" href="#examples" title="Permalink to this heading">¶</a></h2>
  119. <section id="find-files-by-name">
  120. <h3>Find files by name<a class="headerlink" href="#find-files-by-name" title="Permalink to this heading">¶</a></h3>
  121. <p>The first example is to find files on your searx host. It uses the command
  122. <cite>find</cite> available on most Linux distributions. It expects a path type query. The
  123. path in the search request must be inside the <code class="docutils literal notranslate"><span class="pre">working_dir</span></code>.</p>
  124. <p>The results are displayed with the default <cite>key-value.html</cite> template. A result
  125. is displayed in a single row table with the key “line”.</p>
  126. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">find</span><span class="w"></span>
  127. <span class="w"> </span><span class="nt">engine </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">command</span><span class="w"></span>
  128. <span class="w"> </span><span class="nt">command </span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="s">&#39;find&#39;</span><span class="p p-Indicator">,</span><span class="w"> </span><span class="s">&#39;.&#39;</span><span class="p p-Indicator">,</span><span class="w"> </span><span class="s">&#39;-name&#39;</span><span class="p p-Indicator">,</span><span class="w"> </span><span class="s">&#39;{{QUERY}}&#39;</span><span class="p p-Indicator">]</span><span class="w"></span>
  129. <span class="w"> </span><span class="nt">query_type </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">path</span><span class="w"></span>
  130. <span class="w"> </span><span class="nt">shortcut </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">fnd</span><span class="w"></span>
  131. <span class="w"> </span><span class="nt">tokens </span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[]</span><span class="w"></span>
  132. <span class="w"> </span><span class="nt">disabled </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">True</span><span class="w"></span>
  133. <span class="w"> </span><span class="nt">delimiter </span><span class="p">:</span><span class="w"></span>
  134. <span class="w"> </span><span class="nt">chars </span><span class="p">:</span><span class="w"> </span><span class="s">&#39;</span><span class="nv"> </span><span class="s">&#39;</span><span class="w"></span>
  135. <span class="w"> </span><span class="nt">keys </span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="s">&#39;line&#39;</span><span class="p p-Indicator">]</span><span class="w"></span>
  136. </pre></div>
  137. </div>
  138. </section>
  139. <section id="find-files-by-contents">
  140. <h3>Find files by contents<a class="headerlink" href="#find-files-by-contents" title="Permalink to this heading">¶</a></h3>
  141. <p>In the second example, we define an engine that searches in the contents of the
  142. files under the <code class="docutils literal notranslate"><span class="pre">working_dir</span></code>. The search type is not defined, so the user can
  143. input any string they want. To restrict the input, you can set the <code class="docutils literal notranslate"><span class="pre">query_type</span></code>
  144. to <code class="docutils literal notranslate"><span class="pre">enum</span></code> and only allow a set of search terms to protect
  145. yourself. Alternatively, make the engine private, so no one malevolent accesses
  146. the engine.</p>
  147. <div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="p p-Indicator">-</span><span class="w"> </span><span class="nt">name </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">regex search in files</span><span class="w"></span>
  148. <span class="w"> </span><span class="nt">engine </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">command</span><span class="w"></span>
  149. <span class="w"> </span><span class="nt">command </span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="s">&#39;grep&#39;</span><span class="p p-Indicator">,</span><span class="w"> </span><span class="s">&#39;{{QUERY}}&#39;</span><span class="p p-Indicator">]</span><span class="w"></span>
  150. <span class="w"> </span><span class="nt">shortcut </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">gr</span><span class="w"></span>
  151. <span class="w"> </span><span class="nt">tokens </span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[]</span><span class="w"></span>
  152. <span class="w"> </span><span class="nt">disabled </span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">True</span><span class="w"></span>
  153. <span class="w"> </span><span class="nt">delimiter </span><span class="p">:</span><span class="w"></span>
  154. <span class="w"> </span><span class="nt">chars </span><span class="p">:</span><span class="w"> </span><span class="s">&#39;</span><span class="nv"> </span><span class="s">&#39;</span><span class="w"></span>
  155. <span class="w"> </span><span class="nt">keys </span><span class="p">:</span><span class="w"> </span><span class="p p-Indicator">[</span><span class="s">&#39;line&#39;</span><span class="p p-Indicator">]</span><span class="w"></span>
  156. </pre></div>
  157. </div>
  158. </section>
  159. </section>
  160. </section>
  161. <div class="clearer"></div>
  162. </div>
  163. </div>
  164. </div>
  165. <span id="sidebar-top"></span>
  166. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  167. <div class="sphinxsidebarwrapper">
  168. <p class="logo"><a href="../index.html">
  169. <img class="logo" src="../_static/searx_logo_small.png" alt="Logo"/>
  170. </a></p>
  171. <h3>Project Links</h3>
  172. <ul>
  173. <li><a href="https://searx.github.io/searx/blog/index.html">Blog</a>
  174. <li><a href="https://github.com/searx/searx">Source</a>
  175. <li><a href="https://github.com/searx/searx/wiki">Wiki</a>
  176. <li><a href="https://twitter.com/Searx_engine">Twitter</a>
  177. <li><a href="https://github.com/searx/searx/issues">Issue Tracker</a>
  178. </ul><h3>Navigation</h3>
  179. <ul>
  180. <li><a href="../index.html">Overview</a>
  181. <ul>
  182. <li><a href="index.html">Administrator documentation</a>
  183. <ul>
  184. <li>Previous: <a href="private-engines.html" title="previous chapter">How to create private engines</a>
  185. <li>Next: <a href="indexer-engines.html" title="next chapter">Search in indexers</a></ul>
  186. </li>
  187. </ul>
  188. </li>
  189. </ul>
  190. <div id="searchbox" style="display: none" role="search">
  191. <h3 id="searchlabel">Quick search</h3>
  192. <div class="searchformwrapper">
  193. <form class="search" action="../search.html" method="get">
  194. <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
  195. <input type="submit" value="Go" />
  196. </form>
  197. </div>
  198. </div>
  199. <script>document.getElementById('searchbox').style.display = "block"</script>
  200. </div>
  201. </div>
  202. <div class="clearer"></div>
  203. </div>
  204. <div class="footer" role="contentinfo">
  205. &#169; Copyright 2015-2022, Adam Tauber, Noémi Ványi.
  206. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.3.0.
  207. </div>
  208. <script src="../_static/version_warning_offset.js"></script>
  209. </body>
  210. </html>