mysql-proxy tutorial - executing shell commands

Developed In: Lua — Contributed by: Giuseppe Maxia

This script uses MySQL Proxy to execute shell commands from a MySQL client, and to display its result as any other query

MySQL Proxy is a simple program that sits between your client and MySQL server that can monitor, analyze or transform their communication. See http://forge.mysql.com/wiki/MySQL_Proxy for more information.


Giuseppe Maxia
Lua
  1. --[[
  2.  
  3.   Copyright (C) 2007 MySQL AB
  4.  
  5.   This program is free software; you can redistribute it and/or modify
  6.   it under the terms of the GNU General Public License as published by
  7.   the Free Software Foundation; version 2 of the License.
  8.  
  9.   This program is distributed in the hope that it will be useful,
  10.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12.   GNU General Public License for more details.
  13.  
  14.   You should have received a copy of the GNU General Public License
  15.   along with this program; if not, write to the Free Software
  16.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17.  
  18. --]]
  19.  
  20. ---
  21. -- Uses MySQL-Proxy to return output from shell commands
  22. --
  23. -- Using this script, you can request commands like
  24. --
  25. -- SHELL ls -l /usr/local/mysql/data
  26. -- SHELL grep packet /etc/my.cnf
  27. --
  28. -- and get the result in tabular format, from any MySQL client
  29. --
  30. -- ====================================================
  31. -- WARNING
  32. -- ====================================================
  33. -- This feature introduces a security risk
  34. -- Use it only for administration, Not in applications
  35. -- open to general users.
  36. -- ====================================================
  37. --
  38. -- Written by Giuseppe Maxia, based on examples provided
  39. -- by Jan Knesckhe
  40. --
  41. -- see http://forge.mysql.com/snippets/view.php?id=78
  42. -- for details of how to return a result set
  43. --
  44. function read_query( packet )
  45. if string.byte(packet) == proxy.COM_QUERY then
  46. local query = string.sub(packet, 2)
  47.  
  48. -- parsing the query, and checking if it requests a SHELL command
  49. --
  50. -- currently supported:
  51. --
  52. -- SHELL shell command
  53. --
  54. local com_type,command = string.match(query, "^%s*(%w+)%s+(%S.*)" )
  55. --
  56. -- file used to capture the shell command output
  57. --
  58. local result_file = '/tmp/lua.txt'
  59.  
  60. if (string.upper(com_type) == 'SHELL') then
  61.  
  62. print("we got a SHELL query: " .. command)
  63.  
  64. -- executing the shell command
  65. shell_command = string.format('%s > %s 2>&1', command, result_file)
  66. os_result = os.execute(shell_command)
  67. if os_result == 0 then
  68. proxy.response.type = proxy.MYSQLD_PACKET_OK
  69. --
  70. -- collecting the result
  71. --
  72. line_no = 1
  73. shell_output_lines = {}
  74. for line in io.lines(result_file) do
  75. shell_output_lines[line_no] = {line}
  76. line_no = line_no + 1
  77. end
  78.  
  79. --
  80. -- assembling the result set
  81. --
  82. proxy.response.resultset = {
  83. fields = {
  84. {
  85. type = proxy.MYSQL_TYPE_STRING,
  86. name = command,
  87. },
  88. },
  89. rows = shell_output_lines
  90. }
  91. return proxy.PROXY_SEND_RESULT
  92. else
  93. --
  94. -- assembling the error message
  95. --
  96. proxy.response.type = proxy.MYSQLD_PACKET_ERR
  97. err_msg = " * "
  98. for line in io.lines(result_file) do
  99. err_msg = err_msg .. line
  100. end
  101. out_msg = string.format( "Shell error: (%d) %s", os_result , err_msg)
  102. proxy.response.errmsg = out_msg
  103. return proxy.PROXY_SEND_RESULT
  104. end
  105.  
  106. else
  107. print("we got a normal query: " .. query)
  108. end
  109. end
  110. end

Current Tags

You must be logged in to tag this tool

  1. <a href="http://www.rent-car.net">European Rent a Car Association
  2. </a>
,European Rent a Car Association

Votes

  • Rated 4.50 out of 5
Rated 4.50 out of 5 with 2 votes cast.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment