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

Source: Internet
Author: User
Tags ldap perl script

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 by interact with the 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 = $;

if (!/$string _dl/)

{

#--------------Get the account name and domain name---------------------

                                                   my $str _obj = Win32::ole->getobject ("ldap://". $para) or Die "[email protected]";

                                                                                                    

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 = $;

&getmembersingroup ($subldap, $sub _group_name);

}

}

}

}

My $ldap = net::ldap->new (' global.ds.company.com ') or die "[email protected]";

My $MESG = $ldap->bind (' [email protected] ', password = 'youraccountpassword');

if ($MESG->code)

{

Print $MESG->error, "\ n";

}

&getmembersingroup ($ldap, "GroupName");

How do I get all the members in user group by using LDAP in Perl?

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.