Есть 2 переменные с IP-адресами:
start = “10.62.39.1”
end = “10.62.39.12”
Как с помощью Python вывести из Active Directory имена компьютеров и их версию операционной системы в пределах диапазона от 10.62.39.1 до 10.62.39.12
import ipaddress from ms_active_directory import Domain # Define IP range ip_range = ipaddress.ip_range('10.62.39.1', '10.62.39.12') # Connect to your Active Directory domain (replace with your details) domain_name = "YOUR_DOMAIN_NAME" username = "YOUR_USERNAME" password = "YOUR_PASSWORD" domain = Domain(domain_name, username, password) # Enumerate computers in the IP range computers = [] for ip in ip_range: try: # Use LDAP filter to search by IP address computer = domain.computers.find_one(f"({ip.exploded})") if computer: computers.append({"name": computer.name, "os": computer.operatingsystem}) except Exception as e: print(f"Error getting info for {ip}: {e}") if computers: print("Found computers:") for computer in computers: print(f" Name: {computer['name']}") print(f" Operating System: {computer['os']}") else: print("No computers found in the IP range.") # Close the domain connection domain.close()
import ipaddress from ms_active_directory import ADDomain domain = ADDomain('062.OAO.RU') session = domain.create_session_as_user('********@062.OAO.RU', '*********') start = '10.62.39.1' end = '10.62.39.5' start_ip = ipaddress.ip_address(start) end_ip = ipaddress.ip_address(end) ip_range = [ip for ip in range(int(start_ip), int(end_ip) + 1)] computers = [] for ip in ip_range: try: computer = session.find_computer_by_name(f"({ip.exploded})") if computer: computers.append({"name": computer.name, "os": computer.distinguished_name}) except Exception as e: print(f"Error getting info for {ip}: {e}") if computers: print("Found computers:") for pc in computers: print(f" Name: {pc['name']}") print(f" Distinguished name: {pc['distinguished_name']}") else: print("No computers found in the IP range.")
Error getting info for 171845377: 'int' object has no attribute 'exploded' Error getting info for 171845378: 'int' object has no attribute 'exploded' Error getting info for 171845379: 'int' object has no attribute 'exploded' Error getting info for 171845380: 'int' object has no attribute 'exploded' Error getting info for 171845381: 'int' object has no attribute 'exploded'
import socket def get_domain_name(ip_address): try: hostname = socket.gethostbyaddr(ip_address)[0] return hostname except socket.herror: return "No domain name found" ip_address = "10.0.0.123" domain_name = get_domain_name(ip_address) print(f"The domain name for {ip_address} is {domain_name}")
atm87
Появляется ошибка:Error getting info for 171845377: 'int' object has no attribute 'exploded'
atm87ip_range = [ip for ip in range(int(start_ip), int(end_ip) + 1)]
>>> import ipaddress >>> >>> ipstart = '10.62.39.1' >>> ipend = '10.62.39.12' >>> >>> iplist = [] >>> ipcur = ipaddress.ip_address(ipstart) >>> while ipcur <= ipaddress.ip_address(ipend): ... iplist.append(str(ipcur)) ... ipcur += 1 ... >>> iplist ['10.62.39.1', '10.62.39.2', '10.62.39.3', '10.62.39.4', '10.62.39.5', '10.62.39.6', '10.62.39.7', '10.62.39.8', '10.62.39.9', '10.62.39.10', '10.62.39.11', '10.62.39.12'] >>>
ZerGip_range = ipaddress.ip_range('10.62.39.1', '10.62.39.12')
>>> import ipaddress >>> >>> ip_range = ipaddress.ip_range('10.62.39.1', '10.62.39.12') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'ipaddress' has no attribute 'ip_range' >>>