command-engine.rst.txt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. =====================================
  2. Run shell commands from your instance
  3. =====================================
  4. Command line engines are custom engines that run commands in the shell of the
  5. host. In this article you can learn how to create a command engine and how to
  6. customize the result display.
  7. The command
  8. ===========
  9. When specifyng commands, you must make sure the commands are available on the
  10. searx host. Searx will not install anything for you. Also, make sure that the
  11. ``searx`` user on your host is allowed to run the selected command and has
  12. access to the required files.
  13. Access control
  14. ==============
  15. Be careful when creating command engines if you are running a public
  16. instance. Do not expose any sensitive information. You can restrict access by
  17. configuring a list of access tokens under tokens in your ``settings.yml``.
  18. Available settings
  19. ==================
  20. * ``command``: A comma separated list of the elements of the command. A special
  21. token ``{{QUERY}}`` tells searx where to put the search terms of the
  22. user. Example: ``['ls', '-l', '-h', '{{QUERY}}']``
  23. * ``query_type``: The expected type of user search terms. Possible values:
  24. ``path`` and ``enum``. ``path`` checks if the uesr provided path is inside the
  25. working directory. If not the query is not executed. ``enum`` is a list of
  26. allowed search terms. If the user submits something which is not included in
  27. the list, the query returns an error.
  28. * ``delimiter``: A dict containing a delimiter char and the "titles" of each
  29. element in keys.
  30. * ``parse_regex``: A dict containing the regular expressions for each result
  31. key.
  32. * ``query_enum``: A list containing allowed search terms if ``query_type`` is
  33. set to ``enum``.
  34. * ``working_dir``: The directory where the command has to be executed. Default:
  35. ``.``
  36. * ``result_separator``: The character that separates results. Default: ``\n``
  37. Customize the result template
  38. =============================
  39. There is a default result template for displaying key-value pairs coming from
  40. command engines. If you want something more tailored to your result types, you
  41. can design your own template.
  42. Searx relies on `Jinja2 <https://jinja.palletsprojects.com/>`_ for
  43. templating. If you are familiar with Jinja, you will not have any issues
  44. creating templates. You can access the result attributes with ``{{
  45. result.attribute_name }}``.
  46. In the example below the result has two attributes: ``header`` and ``content``.
  47. To customize their diplay, you need the following template (you must define
  48. these classes yourself):
  49. .. code:: html
  50. <div class="result">
  51. <div class="result-header">
  52. {{ result.header }}
  53. </div>
  54. <div class="result-content">
  55. {{ result.content }}
  56. </div>
  57. </div>
  58. Then put your template under ``searx/templates/{theme-name}/result_templates``
  59. named ``your-template-name.html``. You can select your custom template with the
  60. option ``result_template``.
  61. .. code:: yaml
  62. - name: your engine name
  63. engine: command
  64. result_template: your-template-name.html
  65. Examples
  66. ========
  67. Find files by name
  68. ------------------
  69. The first example is to find files on your searx host. It uses the command
  70. `find` available on most Linux distributions. It expects a path type query. The
  71. path in the search request must be inside the ``working_dir``.
  72. The results are displayed with the default `key-value.html` template. A result
  73. is displayed in a single row table with the key "line".
  74. .. code:: yaml
  75. - name : find
  76. engine : command
  77. command : ['find', '.', '-name', '{{QUERY}}']
  78. query_type : path
  79. shortcut : fnd
  80. tokens : []
  81. disabled : True
  82. delimiter :
  83. chars : ' '
  84. keys : ['line']
  85. Find files by contents
  86. -----------------------
  87. In the second example, we define an engine that searches in the contents of the
  88. files under the ``working_dir``. The search type is not defined, so the user can
  89. input any string they want. To restrict the input, you can set the ``query_type``
  90. to ``enum`` and only allow a set of search terms to protect
  91. yourself. Alternatively, make the engine private, so no one malevolent accesses
  92. the engine.
  93. .. code:: yaml
  94. - name : regex search in files
  95. engine : command
  96. command : ['grep', '{{QUERY}}']
  97. shortcut : gr
  98. tokens : []
  99. disabled : True
  100. delimiter :
  101. chars : ' '
  102. keys : ['line']