Skip to content

Commit 234b41e

Browse files
committed
Simplify logic
1 parent 8b3bc6b commit 234b41e

1 file changed

Lines changed: 15 additions & 25 deletions

File tree

nxc/modules/dump-computers.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,27 @@ def options(self, context, module_options):
3434
self.fqdn_only = True
3535

3636
def on_login(self, context, connection):
37-
search_filter = "(objectCategory=computer)"
38-
context.log.debug(f"Search Filter = {search_filter}")
39-
40-
entries = connection.search(
41-
searchFilter=search_filter,
37+
resp = connection.search(
38+
searchFilter="(objectCategory=computer)",
4239
attributes=["dNSHostName", "operatingSystem"]
4340
)
41+
resp_parsed = parse_result_attributes(resp)
4442

4543
answers = []
46-
context.log.debug(f"Total number of records returned: {len(entries)}")
47-
48-
for item in entries:
49-
if not isinstance(item, SearchResultEntry):
50-
continue
44+
context.log.debug(f"Total number of records returned: {len(resp_parsed)}")
5145

52-
try:
53-
parsed = parse_result_attributes([item])[0]
54-
dns_host_name = parsed.get("dNSHostName", "")
55-
operating_system = parsed.get("operatingSystem", "")
46+
for item in resp_parsed:
47+
dns_host_name = item["dNSHostName"]
48+
operating_system = item.get("operatingSystem", "Unknown OS")
5649

57-
if dns_host_name:
58-
netbios_name = dns_host_name.split(".")[0]
59-
if self.netbios_only:
60-
answer = netbios_name
61-
elif self.fqdn_only:
62-
answer = dns_host_name
63-
else:
64-
answer = f"{dns_host_name} ({operating_system})"
65-
answers.append(answer)
66-
except Exception:
67-
context.log.debug("Failed to parse entry", exc_info=True)
50+
if self.netbios_only:
51+
netbios_name = dns_host_name.split(".")[0]
52+
answer = netbios_name
53+
elif self.fqdn_only:
54+
answer = dns_host_name
55+
else:
56+
answer = f"{dns_host_name} ({operating_system})"
57+
answers.append(answer)
6858

6959
if answers:
7060
context.log.success("Found the following computers:")

0 commit comments

Comments
 (0)