文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>使用Perl操纵LDAP数据库

使用Perl操纵LDAP数据库

时间:2005-12-27  来源:coolend

可以通过Perl的LDAP模块(http://search.cpan.org/~gbarr/perl-ldap/)来方便地对LDAP数据库进行相关操作。

下面的脚本简要介绍了使用Net::LDAP等模块来删除、添加、查询PureFTPD所需的用户记录,以及设置用户密码。


脚本如下:

#!/usr/bin/perl -w

use strict;
use Net::LDAP;
use Net::LDAP::Extension::SetPassword;

# 连接LDAP数据库
my $ldap = Net::LDAP->new('localhost') or
die " Cannot connect to LDAP server ! ";

# 绑定
my $mesg = $ldap->bind('cn=root,dc=test,dc=com',
password => 'secret'
);

# 删除记录
my $result = $ldap->delete('uid=muddyboot,ou=users,dc=test,dc=com');
$result->code && warn " Warnning: Failed to delete entry: ", $result->error;

# 添加记录
$result = $ldap->add('uid=muddyboot,ou=users,dc=test,dc=com',
attr => [
'cn' => 'Muddyboot',
'uid' => 'muddyboot',
'uidNumber' => '2000',
'gidNumber' => '100',
'homeDirectory' => '/home/muddyboot',
'userPassword' => '',
'loginShell' => '/bin/bash',
'FTPStatus' => 'enabled',
'FTPQuotaFiles' => '50',
'FTPQuotaMBytes' => '10',
'FTPDownloadBandwidth' => '50',
'FTPUploadBandwidth' => '50',
'FTPDownloadRatio' => '5',
'FTPUploadRatio' => '1',
'objectclass' => [ 'posixAccount',
'PureFTPdUser' ],
]
);
$result->code && warn " Warnning: Failed to add entry: ", $result->error;

# 设置密码
$result = $ldap->set_password( user => 'uid=muddyboot,ou=users,dc=test,dc=com',
newpasswd => 'secret'
);
$result->code && warn " Warnning: Failed to set password: ", $result->error;

# 修改记录
$result = $ldap->modify( 'uid=muddyboot,ou=users,dc=test,dc=com',
replace => {
'FTPQuotaMBytes' => '100',
}
);
$result->code && warn " Warnning: Failed to modify entry: ", $result->error;

# 查询指定记录的单个属性
$result = $ldap->search(
base => "ou=users,dc=test,dc=com",
filter => "(&(uid=muddyboot))"
);
$result->code && warn " Warnning: Failed to search entry: ", $result->error;
my $entry = $result->entry(0);
$result = $entry->get_value('FTPStatus');
print "FTP status of muddyboot: $result";

# 查询指定记录的多个属性
my ($i,$j);
my @attr = (
'FTPStatus', 'FTPQuotaFiles','FTPQuotaMBytes',
'FTPDownloadBandwidth','FTPUploadBandwidth', 'FTPDownloadRatio'
);
print "Information of user muddyboot:";
for ($i=0; $i <(@attr); $i++) {
print " $attr[$i] :" . $entry->get_value($attr[$i]) . " ";
}

# 查询所有记录的信息
$result = $ldap->search(
base => "ou=users,dc=test,dc=com",
filter => "(&(objectClass=PureFTPdUser))"
);
$result->code && warn " Warnning: Failed to search entry: ", $result->error;
## 获取记录的个数
my $count = $result->count();
print "Total records: $count ";

for ($i=0; $i<$count; $i++) {
$entry = $result->entry($i);
print "======================================== ";
print "Information for user: " . $entry->get_value('uid') . " ";
for ($j=0; $j<(@attr); $j++) {
print " $attr[$j] :" . $entry->get_value($attr[$j]) . " ";
}
}
print "======================================== ";

# 取消绑定
$mesg = $ldap->unbind;
运行结果:
FTP status of muddyboot: enabled
Information of user muddyboot:
FTPStatus :enabled
FTPQuotaFiles :50
FTPQuotaMBytes :100
FTPDownloadBandwidth :50
FTPUploadBandwidth :50
FTPDownloadRatio :5
Total records: 3
========================================
Information for user: test
FTPStatus :enabled
FTPQuotaFiles :50
FTPQuotaMBytes :10
FTPDownloadBandwidth :50
FTPUploadBandwidth :50
FTPDownloadRatio :5
========================================
Information for user: coolend
FTPStatus :enabled
FTPQuotaFiles :50
FTPQuotaMBytes :10
FTPDownloadBandwidth :50
FTPUploadBandwidth :50
FTPDownloadRatio :5
========================================
Information for user: muddyboot
FTPStatus :enabled
FTPQuotaFiles :50
FTPQuotaMBytes :100
FTPDownloadBandwidth :50
FTPUploadBandwidth :50
FTPDownloadRatio :5
========================================
相关阅读 更多 +
排行榜 更多 +
ooxe官方版下载

ooxe官方版下载

金融理财 下载
ooxe

ooxe

金融理财 下载
OXE交易app安卓版下载

OXE交易app安卓版下载

金融理财 下载