{"id":11367,"date":"2024-11-13T19:25:00","date_gmt":"2024-11-14T00:25:00","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=11367"},"modified":"2025-01-15T16:59:45","modified_gmt":"2025-01-15T21:59:45","slug":"python-getting-active-directory-subnets","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=11367","title":{"rendered":"Python: Getting Active Directory Subnets"},"content":{"rendered":"\n<p>Like my <a href=\"https:\/\/www.rushworth.us\/lisa\/?p=11364\" data-type=\"post\" data-id=\"11364\">script that pulls the AD site information<\/a> &#8211; this lets me see what subnets are defined and which sites are assigned to those subnets. I was able to quickly confirm that the devices that had problems communicating with Active Directory <em>don&#8217;t<\/em> have a site defined. Way back in 2000, we created a &#8220;catch all&#8221; 10.0.0.0\/8 subnet and assigned it to the user authentication site. New networks on a whole different addressing scheme don&#8217;t have a site assignment. It <em>should<\/em> still work, but the application in question has historically had issues with going the &#8220;Ok, list &#8217;em all&#8221; route. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom ldap3 import Server, Connection, ALL, 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_subnets_and_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 subnets\n    search_base = &#039;CN=Subnets,CN=Sites,CN=Configuration,DC=example,DC=com&#039;  # Change this to match your domain&#039;s DN\n    search_filter = &#039;(objectClass=subnet)&#039;  # Filter to find all subnet objects\n    search_attributes = &#x5B;&#039;cn&#039;, &#039;siteObject&#039;]  # Retrieve the common name and site object references\n\n    # Perform the search\n    connection.search(search_base, search_filter, SUBTREE, attributes=search_attributes)\n\n    # Extract and return subnets and their site assignments\n    subnets_sites = &#x5B;]\n    for entry in connection.entries:\n        subnet_name = entry.cn.value\n        site_dn = entry.siteObject.value if entry.siteObject else &quot;No site assigned&quot;\n        subnets_sites.append((subnet_name, site_dn))\n\n    return subnets_sites\n\ndef print_subnets_and_sites(subnets_sites):\n    if subnets_sites:\n        print(&quot;\\nSubnets and their Site Assignments:&quot;)\n        for subnet, site in subnets_sites:\n            print(f&quot;Subnet: {subnet}, Site: {site}&quot;)\n    else:\n        print(&quot;No subnets 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    subnets_sites = get_subnets_and_sites(username, password)\n    print_subnets_and_sites(subnets_sites)\n\nif __name__ == &quot;__main__&quot;:\n    main()\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Like my script that pulls the AD site information &#8211; this lets me see what subnets are defined and which sites are assigned to those subnets. I was able to quickly confirm that the devices that had problems communicating with Active Directory don&#8217;t have a site defined. Way back in 2000, we created a &#8220;catch &hellip;<\/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-11367","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\/11367","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=11367"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11367\/revisions"}],"predecessor-version":[{"id":11368,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11367\/revisions\/11368"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}