MCPcopy Create free account
hub / github.com/StackStorm/st2 / run

Method run

contrib/linux/actions/dig.py:28–86  ·  view source on GitHub ↗
(self, rand, count, nameserver, hostname, queryopts, querytype)

Source from the content-addressed store, hash-verified

26
27class DigAction(Action):
28 def run(self, rand, count, nameserver, hostname, queryopts, querytype):
29 opt_list = []
30 output = []
31
32 cmd_args = ["dig"]
33 if nameserver:
34 nameserver = "@" + nameserver
35 cmd_args.append(nameserver)
36
37 if isinstance(queryopts, str) and "," in queryopts:
38 opt_list = queryopts.split(",")
39 else:
40 opt_list.append(queryopts)
41
42 cmd_args.extend(["+" + option for option in opt_list])
43
44 cmd_args.append(hostname)
45 cmd_args.append(querytype)
46
47 try:
48 raw_result = subprocess.Popen(
49 cmd_args, stderr=subprocess.PIPE, stdout=subprocess.PIPE
50 ).communicate()[0]
51
52 if sys.version_info >= (3,):
53 # This function might call getpreferred encoding unless we pass
54 # do_setlocale=False.
55 encoding = locale.getpreferredencoding(do_setlocale=False)
56 result_list_str = raw_result.decode(encoding)
57 else:
58 result_list_str = str(raw_result)
59
60 # Better format the output when the type is TXT
61 if querytype.lower() == "txt":
62 result_list_str = result_list_str.replace('"', "")
63
64 result_list = list(filter(None, result_list_str.split("\n")))
65
66 # NOTE: Python3 supports the FileNotFoundError, the errono.ENOENT is for py2 compat
67 # for Python3:
68 # except FileNotFoundError as e:
69 except OSError as e:
70 if e.errno == errno.ENOENT:
71 return (
72 False,
73 "Can't find dig installed in the path (usually /usr/bin/dig). If "
74 "dig isn't installed, you can install it with 'sudo yum install "
75 "bind-utils' or 'sudo apt install dnsutils'",
76 )
77 else:
78 raise e
79
80 if int(count) > len(result_list) or count <= 0:
81 count = len(result_list)
82
83 output = result_list[0:count]
84 if rand is True:
85 random.shuffle(output)

Callers 5

get_linux_distributionFunction · 0.45
test_runMethod · 0.45

Calls

no outgoing calls

Tested by 4

test_runMethod · 0.36