Chinaunix首页 | 论坛 | 博客
  • 博客访问: 596936
  • 博文数量: 40
  • 博客积分: 7274
  • 博客等级: 少将
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-20 15:00
个人简介

Expired

文章分类
文章存档

2011年(1)

2008年(3)

2007年(17)

2006年(10)

2005年(9)

分类:

2005-12-27 23:28:57

可以通过Perl的LDAP模块(~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
========================================
阅读(2316) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~