Skip to content

Instantly share code, notes, and snippets.

@alces
Created November 12, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alces/844c2948c31b271e23f6 to your computer and use it in GitHub Desktop.
Save alces/844c2948c31b271e23f6 to your computer and use it in GitHub Desktop.
A script searching for computers in AD by names' wildcard
# get a list of hosts with names matching a wildcard from AD
# on RedHat-based systems python-ldap package is required
import ldap
# get a field from ldap results
res_field = lambda r, n: r[1][n][0]
# send search query to ldap and get a result
ldap_search = lambda ldap_srv, wld_card: ldap_srv.search_s('ou=servers,dc=example,dc=com',
ldap.SCOPE_SUBTREE,
'(&(objectclass=computer)(name=%s))' % wld_card,
['name'])
# return a list of hosts with names matching a wildcard
def lst_hosts(wld_card):
ldap_srv = ldap.initialize('ldap://dc.example.com')
ldap_srv.simple_bind_s('admin@example.com', 'ver7s3ctertw0rd')
return [res_field(res, 'name').lower() for res in ldap_search(ldap_srv, wld_card)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment