How to get all the members in user group by using LDAP in Perl ?, Ldapperl

Source: Internet
Author: User
Tags ldap perl script

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");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.