Query all computer objects from Active Directory
(self)
| 58 | self.connection = None |
| 59 | |
| 60 | def query_computers(self): |
| 61 | """Query all computer objects from Active Directory""" |
| 62 | if not self.connection: |
| 63 | raise Exception("Not connected to AD. Call connect() first.") |
| 64 | |
| 65 | try: |
| 66 | # Convert domain to DN format (e.g., company.local -> DC=company,DC=local) |
| 67 | domain_dn = ','.join([f"DC={part}" for part in self.domain.split('.')]) |
| 68 | |
| 69 | # Search for computer objects |
| 70 | search_base = domain_dn |
| 71 | search_filter = '(objectClass=computer)' |
| 72 | |
| 73 | self.connection.search(search_base, search_filter, attributes=LDAP_COMPUTER_ATTRIBUTES) |
| 74 | |
| 75 | computers = [] |
| 76 | for entry in self.connection.entries: |
| 77 | computer_info = { |
| 78 | 'sid': str(entry.objectSid) if entry.objectSid else None, |
| 79 | 'computer_name': str(entry.cn) if entry.cn else None, |
| 80 | 'dns_hostname': str(entry.dNSHostName) if entry.dNSHostName else None, |
| 81 | 'os': str(entry.operatingSystem) if entry.operatingSystem else None |
| 82 | } |
| 83 | computers.append(computer_info) |
| 84 | |
| 85 | logger.info(f"Found {len(computers)} computer objects") |
| 86 | return computers |
| 87 | |
| 88 | except LDAPException as e: |
| 89 | logger.error(f"LDAP query failed: {e}") |
| 90 | return [] |
| 91 | except Exception as e: |
| 92 | logger.error(f"Query error: {e}") |
| 93 | return [] |
| 94 | |
| 95 | def query_subnets(self): |
| 96 | """Query AD Sites and Services for configured subnets""" |