{"id":11364,"date":"2024-11-11T16:53:19","date_gmt":"2024-11-11T21:53:19","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=11364"},"modified":"2025-01-15T16:55:46","modified_gmt":"2025-01-15T21:55:46","slug":"python-get-active-directory-sites","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=11364","title":{"rendered":"Python: Get Active Directory Sites"},"content":{"rendered":"\n<p>One down side of <em>not<\/em> administering the Active Directory domain anymore is that I <em>don&#8217;t<\/em> have the quick GUI tools that show you how &#8220;stuff&#8221; is set up. Luckily, the sites are all reflected in AD objects that can be read by authenticated users:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom ldap3 import Server, Connection, ALL, SIMPLE, SUBTREE, Tls\nimport ssl\nimport getpass\n\n# Attempt to import USERNAME and PASSWORD from config.py\ntry:\n    from config import USERNAME, PASSWORD\nexcept ImportError:\n    USERNAME, PASSWORD = None, None\n\n# Define constants\nLDAP_SERVER = &#039;ad.example.com&#039;\nLDAP_PORT = 636\n\ndef get_all_sites(username, password):\n    # Set up TLS configuration\n    tls_configuration = Tls(validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLSv1_2)\n\n    # Connect to the LDAP server\n    server = Server(LDAP_SERVER, port=LDAP_PORT, use_ssl=True, tls=tls_configuration, get_info=ALL)\n    connection = Connection(server, user=username, password=password, authentication=&#039;SIMPLE&#039;, auto_bind=True)\n\n    # Define the search base for sites\n    search_base = &#039;CN=Sites,CN=Configuration,DC=example,DC=com&#039;  # Update to match your domain&#039;s DN structure\n    search_filter = &#039;(objectClass=site)&#039;  # Filter to find all site objects\n    search_attributes = &#x5B;&#039;cn&#039;]  # We only need the common name (cn) of the sites\n\n    # Perform the search\n    connection.search(search_base, search_filter, SUBTREE, attributes=search_attributes)\n\n    # Extract and return site names\n    site_names = &#x5B;entry&#x5B;&#039;cn&#039;].value for entry in connection.entries]\n    return site_names\n\ndef print_site_names(site_names):\n    if site_names:\n        print(&quot;\\nAD Sites:&quot;)\n        for site in site_names:\n            print(f&quot;- {site}&quot;)\n    else:\n        print(&quot;No sites found in the domain.&quot;)\n\ndef main():\n    # Prompt for username and password if not available in config.py\n    username = USERNAME if USERNAME else input(&quot;Enter your LDAP username: &quot;)\n    password = PASSWORD if PASSWORD else getpass.getpass(&quot;Enter your LDAP password: &quot;)\n\n    site_names = get_all_sites(username, password)\n    print_site_names(site_names)\n\nif __name__ == &quot;__main__&quot;:\n    main()\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>One down side of not administering the Active Directory domain anymore is that I don&#8217;t have the quick GUI tools that show you how &#8220;stuff&#8221; is set up. Luckily, the sites are all reflected in AD objects that can be read by authenticated users:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1945],"tags":[],"class_list":["post-11364","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11364"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11364\/revisions"}],"predecessor-version":[{"id":11365,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11364\/revisions\/11365"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}