Skip to content

Add query type to linux.dig action #5772

@AmbiguousYeoman

Description

@AmbiguousYeoman

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions