Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > 使用Perl操纵LDAP数据库
【标  题】:使用Perl操纵LDAP数据库
【关键字】:Perl,LDAP
【来  源】:http://blog.chinaunix.net/article.php?articleId=63825&blogId=6542

使用Perl操纵LDAP数据库

Your Ad Here 可以通过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]) . "\n";
}

# 查询所有记录的信息
$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\n";

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

# 取消绑定
$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
========================================
安装Linux驱动:【上一篇】
在Debian下给Grub加上背景:【下一篇】
【相关文章】
  • awk分段处理记录并格式化输出(处理LDAP文件和DHCP记录)
  • Perl版猜数字游戏
  • 在指定文件夹中的文件中查找包含指定字符的行(这个小东西不错[Perl])
  • 使用 LDAP 目录服务器[下载]
  • 自动编写ACE程序Makefile文件的Perl程序
  • Windows下Perl开发环境的安装与配置
  • mod_perl编程的简单应用实例介绍
  • 用PERL改写的猜数字小游戏
  • 初学者入门:Perl概述
  • 开始研究一下ldap这个东东
  • 【随机文章】
  • 绿萼听母亲说完
  • 上网与Windows注册表安全
  • 什么是终端
  • 邯郸就开始了电子政务和数字邯郸的建设
  • Armadillo标准加壳的程序的脱壳和引入表修复方案
  • Lucene In Action
  • 在AIX 4.3.3系统上安装OpenSSH
  • 认识设备管理器中的问题符号
  • 思科PIX ASDM的安装与故障排除
  • Matlab 脚本文件和函数文件
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.