-
-
Notifications
You must be signed in to change notification settings - Fork 779
Closed
Labels
Description
SUMMARY
I would like the ability to query TXT records and noticed there is no way to specify a query type to the dig action.
STACKSTORM VERSION
st2 3.6.0, on Python 3.6.8
Steps to reproduce the problem
I attempted a few ways to add "TXT" to the query by adding to queryopts or try appending to the string hostname. Upon looking at the code I realized nothing like that would work.
Expected Results
Get a list returned of TXT records
Some sample code to add it
class DigAction(Action):
def run(self, rand, count, nameserver, hostname, queryopts, querytype): # Add querytype parameter
opt_list = []
output = []
cmd_args = ["dig"]
if nameserver:
nameserver = "@" + nameserver
cmd_args.append(nameserver)
if isinstance(queryopts, str) and "," in queryopts:
opt_list = queryopts.split(",")
else:
opt_list.append(queryopts)
cmd_args.extend(["+" + option for option in opt_list])
cmd_args.append(hostname)
cmd_args.append(querytype) # append query type (Default is set to "A" in dig.yaml)
try:
raw_result = subprocess.Popen(
cmd_args, stderr=subprocess.PIPE, stdout=subprocess.PIPE
).communicate()[0]
if sys.version_info >= (3,):
# This function might call getpreferred encoding unless we pass
# do_setlocale=False.
encoding = locale.getpreferredencoding(do_setlocale=False)
result_list_str = raw_result.decode(encoding)
else:
result_list_str = str(raw_result)
if querytype.lower() == "txt": # improve the output formatting result of TXT records
result_list_str = result_list_str.replace('"', '') # strip quotes so we don't see \" wrapped around output
result_list = list(filter(None, result_list_str.split("\n")))
I only spent a few minutes on this code to test making it work for me. It could be improved on to make sure works for other types as well. I added inline comments to show the only lines I added