Created
November 12, 2015 12:33
-
-
Save alces/844c2948c31b271e23f6 to your computer and use it in GitHub Desktop.
A script searching for computers in AD by names' wildcard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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