@@ -35,39 +35,27 @@ def options(self, context, module_options):
3535 def on_login (self , context , connection ):
3636 resp = connection .search (
3737 searchFilter = "(objectCategory=computer)" ,
38- attributes = ["dNSHostName" , "operatingSystem" , "sAMAccountName" ]
38+ attributes = ["dNSHostName" , "operatingSystem" ]
3939 )
4040 resp_parsed = parse_result_attributes (resp )
4141
4242 answers = []
4343 context .log .debug (f"Total number of records returned: { len (resp_parsed )} " )
4444
45- # detect domain suffix from any valid dnshostname
46- domain = None
47- for entry in resp_parsed :
48- dn = entry .get ("dNSHostName" )
49- if dn and "." in dn :
50- domain = dn .split ("." , 1 )[1 ]
51- break
52-
5345 for item in resp_parsed :
54- dns_host_name = item .get ("dNSHostName" ) or item .get ("sAMAccountName" )
55-
56- # handle machine accounts with only samaccountname
57- if "." not in dns_host_name :
58- dns_host_name = dns_host_name .rstrip ("$" )
59- if domain :
60- dns_host_name = f"{ dns_host_name } .{ domain } "
61-
62- operating_system = item .get ("operatingSystem" ) or "Unknown OS"
46+ dns_host_name = item .get ("dNSHostName" )
47+ operating_system = item .get ("operatingSystem" , "Unknown OS" )
48+ if not dns_host_name :
49+ context .log .debug (f"Skipping computer without dNSHostName: { item .get ('cn' , '<unknown>' )} " )
50+ continue
6351
6452 if self .netbios_only :
65- answer = dns_host_name .split ("." )[0 ]
53+ netbios_name = dns_host_name .split ("." )[0 ]
54+ answer = netbios_name
6655 elif self .fqdn_only :
6756 answer = dns_host_name
6857 else :
6958 answer = f"{ dns_host_name } ({ operating_system } )"
70-
7159 answers .append (answer )
7260
7361 context .log .success ("Found the following computers:" )
0 commit comments