Expired
分类:
2005-12-27 23:28:57
脚本如下:
#!/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;