]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
scripts: kresd-query.lua (new)
authorMarek Vavrusa <marek@vavrusa.com>
Fri, 22 Jan 2016 07:48:58 +0000 (23:48 -0800)
committerMarek Vavrusa <marek@vavrusa.com>
Fri, 22 Jan 2016 07:48:58 +0000 (23:48 -0800)
this is a boilerplate for a CLI utility to resolve
names and execute script on query response
in another words, "a jq for resolver answers"

this is a scaffolding for alternative tools like
'host' or a plug-in part for scripting around it.

it basically starts a kresd instance, but doesn't
bind to any interface or read configuration,
then a query + callback is sent to kresd standard
input, and it quits after the execution

scripts/kresd-query.lua [new file with mode: 0755]

diff --git a/scripts/kresd-query.lua b/scripts/kresd-query.lua
new file mode 100755 (executable)
index 0000000..d4e57d7
--- /dev/null
@@ -0,0 +1,61 @@
+#!/usr/bin/env luajit
+cli_bin = 'kresd -q -c -'
+-- Work around OS X stripping dyld variables
+libdir = os.getenv('DYLD_LIBRARY_PATH')
+if libdir then
+       cli_bin = string.format('DYLD_LIBRARY_PATH="%s" %s', libdir, cli_bin)
+end
+cli_cmd = [[echo '
+option("ALWAYS_CUT", true)
+%s
+return resolve("%s", kres.type.%s, kres.class.%s, 0,
+function (pkt, req)
+       pkt = kres.pkt_t(pkt)
+       req = kres.request_t(req)
+       pcall(function () %s end)
+       quit()
+end)']]
+-- Parse CLI arguments
+local function help()
+       name = 'kresd-query.lua'
+       print(string.format('Usage: %s [-t type] [-c class] [-C config] <name> <script>', name))
+       print('Execute a single-shot query and run a script on the result.')
+       print('There are two variable available: pkt (kres.pkt_t), req (kres.request_t)')
+       print('See modules README to learn about their APIs.')
+       print('')
+       print('Options:')
+       print('\t-h,--help        ... print this help')
+       print('\t-t TYPE          ... query for given type (default: A)')
+       print('\t-c CLASS         ... query in given class (default: IN)')
+       print('\t-C config_str    ... kresd-style config (default: -)')
+       print('Examples:')
+       print('\t'..name..' -t SOA cz "print(pkt:qname())"         ... print response QNAME')
+end
+-- Parse CLI arguments
+if #arg < 2 then help() return 1 end
+local qtype, qclass, qname = 'A', 'IN', nil
+local config, scripts = '', {}
+k = 1 while k <= #arg do
+       local v = arg[k]
+       if v == '-h' or v == '--help' then
+               return help()
+       elseif v == '-C' then
+               k = k + 1
+               config = arg[k]
+       elseif v == '-c' then
+               k = k + 1
+               qclass = arg[k]:upper()
+       elseif v == '-t' then
+               k = k + 1
+               qtype = arg[k]:upper()
+       elseif v:byte() == string.byte('-') then
+               return help()
+       elseif not qname then
+               qname = v
+       else
+               table.insert(scripts, v)
+       end
+       k = k + 1
+end
+cli_cmd = string.format(cli_cmd, config, qname, qtype, qclass, table.concat(scripts, ' '))
+return os.execute(cli_cmd..' | '..cli_bin)