因为工作需要,以前写的关于perl处理LDAP的问题,该脚本实现了两个功能:
1. 自动增加帐号和随机密码
2. 把生成后的帐号信息使用邮件方式通知该用户。
- #!/usr/bin/perl
-
-
#This script is used to add the new account and passwd
-
-
use strict;
-
use Mail::Sendmail;
-
use Net::LDAP;
-
-
-
my $user = $ARGV[0];
-
my $password = &randompasswd();
-
-
-
#Generate the random password
-
sub randompasswd {
-
-
my $passwd;
-
my $_rand;
-
my $passwd_length = $_[0];
-
-
if (!$passwd_length){
-
$passwd_length = 8;
-
}
-
-
-
my @chars = split(" ",
-
"a b c d e f g h i j k l m n o
-
p q r s t u v w x y z
-
0 1 2 3 4 5 6 7 8 9"
-
);
-
-
srand;
-
-
-
for ( my $i = 0; $i < $passwd_length; $i++){
-
$_rand = int (rand 36);
-
$passwd .= $chars[$_rand];
-
}
-
-
return $passwd;
-
}
-
-
-
#Connect the LDAP Server
-
print "Connect the LDAP Server ....\n ";
-
-
my $ldap = Net::LDAP->new("192.168.56.101") or die "$@";
-
-
$ldap->bind("uid=test1,ou=People,dc=test,dc=com,dc=cn",
-
password=>"xxx"
-
);
-
-
#Add the new account to the LDAP Server
-
print "Add the $user account..\n";
-
-
my $result = $ldap->add("uid=$user,ou=People,dc=test,dc=com,dc=cn",
-
attr => [ 'cn' => "$user",
-
'sn' => "$user",
-
'uid' => "$user",
-
'mail' => "$user\@test.com.cn",
-
'userpassword' => "$password",
-
'objectclass' =>[ 'top',
-
'person',
-
'organizationalPerson',
-
'inetOrgPerson'
-
]
-
]
-
);
-
-
$result->done;
-
-
$result->code && warn "failed to add entry: ", $result->error ;
-
-
-
-
#Send new mail notice
-
print "Send the mail to notice $user account...\n";
-
-
my %mail = ( To => "$user\@test.com.cn",
-
From => 'test1@testcom.cn',
-
Subject => 'Your username and password',
-
Message => "Hello, $user,your random password is $password",
-
smtp =>'192.168.x.x''
-
);
-
-
-
if (sendmail %mail) { print "Mail Send OK\n" }
-
else { print "Error sending mail: $Mail::Sendmail::error\n" }
阅读(2710) | 评论(2) | 转发(0) |