LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1407|回复: 5

Developing LDAP Applications in Perl with Net::LDAP

[复制链接]
发表于 2004-1-1 18:38:20 | 显示全部楼层 |阅读模式
Install the Convert::Ber Module #安装Convert::Ber模块

Download the Convert::Ber module When you download the Convert::Ber module, you will receive a file called Convert-BER-1.31.tar.gz.
#下载Conver::ber模块,你会收到一个名为conver-BER-1.31.tar.gz的文件。

   gunzip Convert-BER-1.31.tar.gz
   tar -xvf Convert-BER-1.31.tar
   cd Convert-BER-1.31

   perl Makefile.PL
   make
   make test
   make install

Install the Net:DAP::Ber Module#安装Net:DAP::Ber模块。
Download the Net:DAP::Ber module When you download Net:DAP module, you will receive a file called perl-ldap-0.15.tar.gz
#下载Conver::ber模块,你会收到一个名为conver-BER-1.31.tar.gz的文件。

   gunzip perl-ldap-0.15.tar.gz
   tar -xvf perl-ldap-0.15.tar
   cd perl-ldap-0.15

   perl Makefile.PL
   make
   make test
   make install

Your perl development environment is now ready for Net:DAP development. See Net:DAP Programming by Example for the official tutorial.
#你已有一个Net:DAP的perl开发环境。看以下一些个别的简单的例子。

You can also download a newer version of the LDAP module.#你也可以下载更新的LDAP模块。
perl-ldap-0.25 (perl-ldap-0.25.tar.gz) The Makefile.PL says that the IO::Socket::SSL module is required only if you want LDAPS. This module, IO::Socket::SSL, depends on the Net::SSLeay module.#在安装IO::Sokcet::SSL模块前先要安装LDAPS.
Unfortunately, the directory for Net::SSLeay was missing and I couldn't download the file. #不幸的,安装有Net::SSL的目录会提示缺损或不能安装新的LDAPS。
As a result, I chose not to install the newer version 0.25 of Net:DAP.
#象这样的结果,我会选择不安装新的0.25版本。


Searching the LDAP Directory #寻找LDAP目录

The $ldap->search functions is perl' equivalent of the shell command, ldapsearch.

Example #1: Basic Search
This is the perl equivalent of a basic ldapsearch. It returns all nodes that meet the filtering (search) requirements, and displayes all member attribues. It uses the dump() to output results.

#!/usr/bin/perl
#----------------------------------------------------------
# File: ldapquery1.pl
# Desc: This is your basic ldapsearch.
#       base   => The root node.
#       filter => Search for nodes that meet your search criteria:
#                 cn=Bob Pringle      ,or
#                 cn=*
#----------------------------------------------------------

use Net:DAP qw(:all);

# Two syntaxes for initialization.  Choose one.
# $ldap = Net:DAP->new('localhost') or die "$@";
$ldap = new Net::LDAP('localhost') or die "$@";

$ldap->bind( version => 3 );

$mesg = $ldap->search ( base => "dc=example,dc=com",
                        filter => "cn=Bob Pringle",
                      ) or die ("Failed on search.$!");

foreach $entry ($mesg->all_entries)
{
   $entry->dump;
}

$ldap->unbind;


Example #2: Specifying Attributes returned in a Search

#!/usr/bin/perl
#----------------------------------------------------------
# File: ldapquery2.pl
# Desc: This ldapsearch introduces the ability to limit
#       the list of attributes displayed in the dataset.
#       base   => The root node.
#       filter => Search for nodes that meet your search criteria:
#                 cn=Bob Pringle      ,or
#                 cn=*
#       attrs =>  The array of attributes you want to see.
#                 @attrs
#----------------------------------------------------------

use Net::LDAP qw(:all);

# Two syntaxes for initialization.  Choose one.
# $ldap = Net::LDAP->new('localhost') or die "$@";
$ldap = new Net::LDAP('localhost') or die "$@";

$ldap->bind( version => 3 );

@attrs = ['cn', 'sn'];

$mesg = $ldap->search ( base => "dc=example,dc=com",
                        filter => "cn=Bob Pringle",
                        attrs => @attrs
                      ) or die ("Failed on search.$!");

foreach $entry ($mesg->all_entries)
{
   $entry->dump;
}

$ldap->unbind;

Example 3: Access Individual attributes from a Returned Query
The dump() function provided an easy way to display the results of a query, but you could not access the individual nodes or node attributes. The as-struct() function.

#!/usr/bin/perl
#--------------------------------------------------------------
# File: ldapquery3.pl
# Desc: This script uses the as_struct() function from Net::LDAP.
#       It lets us individually access each attribute, ie. cn, sn.
#       Attribute values are stored as an array.
#
#       $base: This is the root node of your tree.
#       $searchString: Search the tree for nodes meeting the
#          search conditions.  Example: sn=*, cn=Michael Yee.
#       @Attrs: Array that specifies which attributes should
#          be displayed for each node in the result set.
#          Example: @Attrs = [ 'cn', 'sn' ];    # Display cn, sn
#--------------------------------------------------------------

use Net::LDAP qw(:all);

# Replace localhost with IP address or DNS entry if available
$ldap = Net::LDAP->new('localhost') or die "$@";

$mesg = $ldap->bind( version => 3 );

my $base = "dc=example,dc=com";
my $searchString = "sn=*";

my @Attrs;
# @Attrs = [ 'cn', 'sn' ];  # anonymous array, Return cn and sn only

my $result = $ldap->search ( base    => "$base",
                             scope   => "sub",
                             filter  => "$searchString",
                             attrs   =>  @Attrs
                           );

my $href = $result->as_struct;

# get an array of the DN's
my @arrayOfDNs  = keys %$href ;     # use DN hashes

# process each dn:
foreach (@arrayOfDNs)
{
   print "dn: ", $_, "\n";          # print the dn:
   my $valref = $$href{$_};

   # get an array of the attribute names passed for this one DN.
   my @arrayOfAttrs = sort keys %$valref; #use Attr hashes
   my $attrName;        

   # Print the attributes and their values
   foreach $attrName (@arrayOfAttrs)
   {
      # skip any binary data
      next if ( $attrName =~ /;binary$/ );

      # get attribute value (pointer) using the attribute name as the hash
      my $attrVal = @$valref{$attrName} ;

      #print "\t $attrName: @$attrVal \n";
      foreach $attElement (@$attrVal)
      {
         print "\t $attrName: $attElement \n";
      }

   }   # End of attribute list

   print "#-------------------------------\n";
}   # End of that dn:


Insert a new LDAP Record #插入一个新的LDAP报告。

The $ldap->add function is the perl equivalent of ldapadd shell command. Test with: ldap://localhost/dc=example,dc=com?*?sub?(sn=Ryan)

#!/usr/bin/perl
#----------------------------------------------------------
# File: ldapinsert.pl
# Desc: This is your basic ldapadd.
#       The $ldap->bind now requires admin information
#----------------------------------------------------------

use Net::LDAP qw(:all);

# Two syntaxes for initialization.  Choose one.
# $ldap = Net::LDAP->new('localhost') or die "$@";
$ldap = new Net::LDAP('localhost') or die "$@";

$ldap->bind( dn       => 'cn=Manager,dc=example,dc=com',
             password => 'secret'
           ) || die $@;

# Create the dn: and attributes that will be added.
my $dn = "cn=Meg Ryan,ou=Sales,dc=example,dc=com";
my @objectClasses = ['top', 'person', 'organizationalPerson', 'inetOrgPerson'];
my @cn = ['Meg Ryan'];
my $sn = "Ryan";

# Add the node to the directory.
$msg = $ldap->add ( $dn, attr => [ 'objectclass' => @objectClasses,
                                   'cn'          => @cn,
                                   'sn'          => $sn
                                 ]
                  ) || warn "Failed to add entry. $!";

print "msg is $msg\n";

$ldap->unbind;


Update a LDAP Record #升级LDAP记录


Example 1: Add an attribute to an existing record #附加一个属性到一个已存在的记录

#!/usr/bin/perl
#----------------------------------------------------------
# File: ldapupdate1.pl
# Desc: Add two attributes (mail and description) with
#       $ldap->modify
# Note: $ldap->bind requires admin information
#----------------------------------------------------------

use Net::LDAP qw(:all);

# Two syntaxes for initialization.  Choose one.
# $ldap = Net::LDAP->new('localhost') or die "$@";
$ldap = new Net::LDAP('localhost') or die "$@";

$ldap->bind( dn       => 'cn=Manager,dc=example,dc=com',
             password => 'secret'
           ) || die $@;

# Prepare updated information. #取得升级数据
my $dn = "cn=Meg Ryan,ou=Salesdc=example,dc=com";
my @mail = [ 'mryan@example.com', 'megryan@example.com' ];
my $description = "Great actress";

$msg = $ldap->modify ( $dn,
                       add => { 'mail' => @mail,
                                'description' => $description }
                     ) || warn "Failed to modify entry. $!";

print "msg is $msg\n";

$ldap->unbind;

Example 2: Replace values of an attribute

#!/usr/bin/perl
#----------------------------------------------------------
# File: ldapupdate2.pl
# Desc: Replace (update) an attribute with $ldap->modify
# Note: $ldap->bind requires admin parameters
#----------------------------------------------------------

use Net::LDAP qw(:all);

# Two syntaxes for initialization.  Choose one.
# $ldap = Net::LDAP->new('localhost') or die "$@";
$ldap = new Net::LDAP('localhost') or die "$@";

$ldap->bind( dn       => 'cn=Manager,dc=example,dc=com',
             password => 'secret'
           ) || die $@;

# Prepare updated information
my $dn = "cn=Meg Ryan,ou=Sales,dc=example,dc=com";
my $description = "Great actress. Deserves an Oscar!";

$msg = $ldap->modify ( $dn,
                       replace => { 'description' => $description }
                     ) || warn "Failed to modify entry. $!";

print "msg is $msg\n";

$ldap->unbind;

Example 3: Delete an attribute

#!/usr/bin/perl
#----------------------------------------------------------
# File: ldapupdate3.pl
# Desc: Delete an attribute with $ldap->modify
# Note: $ldap->bind requires admin privs.
#----------------------------------------------------------

use Net::LDAP qw(:all);

# Two syntaxes for initialization.  Choose one.
# $ldap = Net::LDAP->new('localhost') or die "$@";
$ldap = new Net::LDAP('localhost') or die "$@";

$ldap->bind( dn       => 'cn=Manager,dc=example,dc=com',
             password => 'secret'
           ) || die $@;

# The 'mail' attribute will be deleted from this dn.
my $dn = "cn=Meg Ryan,ou=Sales,dc=example,dc=com";

$msg = $ldap->modify ( $dn,
                       delete => 'mail'
                     ) || warn "Failed to modify entry. $!";

print "msg is $msg\n";

$ldap->unbind;

Delete an LDAP Record 删除一个LDAP记录

This is the perl version of the ldapdelete shell command.

#!/usr/bin/perl
#----------------------------------------------------------
# File: ldapremove.pl
# Desc: Delete a node
#----------------------------------------------------------

use Net::LDAP qw(:all);

# Two syntaxes for initialization.  Choose one.
# $ldap = Net::LDAP->new('localhost') or die "$@";
$ldap = new Net::LDAP('localhost') or die "$@";

$ldap->bind( dn       => 'cn=Manager,dc=example,dc=com',
             password => 'secret'
           ) || die $@;

my $dn = "cn=Meg Ryan,ou=Salesdc=example,dc=com";

$msg = $ldap->delete ( $dn) || warn "Failed to delete entry. $!";

print "msg is $msg\n";

$ldap->unbind;
发表于 2004-1-1 18:41:28 | 显示全部楼层
大姐,翻译一下嘛:)
 楼主| 发表于 2004-1-1 20:34:02 | 显示全部楼层
最初由 tojeff 发表
大姐,翻译一下嘛:)


我才多少岁,被你叫的这么大。。:confused:

不知道你需要哪里作翻译??还有,什么时候答应我呀??
发表于 2004-1-2 12:36:49 | 显示全部楼层
那以后叫妹妹好了啦:)

答应什么哦?:confused: :confused:
 楼主| 发表于 2004-1-2 17:06:57 | 显示全部楼层
就叫我的网名吧吧。。。
做版主。。。人多力量大。。
发表于 2004-1-10 21:40:09 | 显示全部楼层
支持一下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表