How to get all the members in user group by using LDAP in Perl ?, Ldapperl
About LDAP:
LDAP stands for Lightweight Directory Access Protocol. It is usually used to fetch (and sometimes update) data in a directory of people.
Using Net: LDAP module in Perl can provide a way to interact with this database.
Perl script to get this:
#! /Usr/bin/perl
#Owner: Rebecca
# Creation date: 2014-12-29
# Usage:
#./Script. pl> yourfile. scv
Use strict;
Use Win32;
Use Win32: OLE;
Use Net: LDAP;
Use warnings;
Sub getmembersingroup
{
(My $ subldap, my $ groupname) = @_;
# ********************************** Get distinguished name by using group name ************************************ *****
My $ mesg = $ subldap-> search (
Base =>"Dc = global, dc = ds, dc = company, dc = com",
Filter => "(& (CN =". $ groupname ."))",
);
If ($ mesg-> code)
{
Print $ mesg-> error, "\ n ";
Exit;
}
My @ entries = $ mesg-> entries;
My $ distinguishedName;
Foreach my $ entry (@ entries)
{
$ DistinguishedName = $ entry-> get_value ("distinguishedName ");
}
# ******************** Get members by using the newly got distinguished Name ******** *************************************
$ Mesg = $ subldap-> search (
Base => $ distinguishedName,
Scope => "sub ",
Filter => "(& (objectClass = *))",
);
@ Entries = $ mesg-> entries;
My $ entry;
Foreach $ entry (@ entries)
{
My @ member = $ entry-> get_value ("member ");
Foreach (@ member)
{
My $ line =$ _;
My $ para = $ line;
My $ string_dl = "OU = Distribution Lists ";
$ Line = ~ /DC = (.*?), /;
My $ str_domain = $1;
If (! /$ String_dl /)
{
# -------------- Get the account name and domain name ---------------------
My $ str_obj = Win32: OLE-> GetObject ("LDAP: //". $ para) or die "$ @";
My $ status_able = "disabled ";
If ($ str_obj-> {accountdisabled} eq 0)
{
$ Status_able = "enabled ";
}
$ Str_obj-> {displayName} = ~ S/\, // g; # remove the, in the name
Print "$ str_obj-> {displayName}, $ str_obj-> {sAMAccountName}, $ str_domain, $ status_able \ n ";
}
Else
{
# It is a DL need to get the members inside
$ Line = ~ /CN = (.*?), /;
My $ sub_group_name = $1;
& Getmembersingroup ($ subldap, $ sub_group_name );
}
}
}
}
My $ ldap = Net: LDAP-> new ('Global .ds.company.com') Or die "$ @";
My $ mesg = $ ldap-> bind ('Yourid @ yourdomian.ds.company.com', Password =>"Youraccountpassword");
If ($ mesg-> code)
{
Print $ mesg-> error, "\ n ";
}
& Getmembersingroup ($ ldap ,"GroupName");