加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 23:49:12 所属栏目:大数据 来源:网络整理
导读: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 sc

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读