Chinaunix首页 | 论坛 | 博客
  • 博客访问: 195552
  • 博文数量: 17
  • 博客积分: 2459
  • 博客等级: 大尉
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 21:32
文章分类

全部博文(17)

文章存档

2012年(6)

2011年(16)

2009年(1)

分类: Python/Ruby

2011-04-07 22:39:55

因为工作需要,以前写的关于perl处理LDAP的问题,该脚本实现了两个功能:

1. 自动增加帐号和随机密码
2. 把生成后的帐号信息使用邮件方式通知该用户。



  1. #!/usr/bin/perl

  2. #This script is used to add the new account and passwd

  3. use strict;
  4. use Mail::Sendmail;
  5. use Net::LDAP;


  6. my $user = $ARGV[0];
  7. my $password = &randompasswd();


  8. #Generate the random password
  9. sub randompasswd {

  10.     my $passwd;
  11.     my $_rand;
  12.     my $passwd_length = $_[0];
  13.     
  14.     if (!$passwd_length){
  15.         $passwd_length = 8;
  16.     }


  17.     my @chars = split(" ",
  18.         "a b c d e f g h i j k l m n o
  19.         p q r s t u v w x y z
  20.         0 1 2 3 4 5 6 7 8 9"
  21.     );

  22.     srand;


  23.     for ( my $i = 0; $i < $passwd_length; $i++){
  24.         $_rand = int (rand 36);
  25.         $passwd .= $chars[$_rand];
  26.     }

  27.     return $passwd;
  28. }


  29. #Connect the LDAP Server
  30. print "Connect the LDAP Server ....\n ";

  31. my $ldap = Net::LDAP->new("192.168.56.101") or die "$@";

  32. $ldap->bind("uid=test1,ou=People,dc=test,dc=com,dc=cn",
  33.              password=>"xxx"
  34.             );

  35. #Add the new account to the LDAP Server
  36. print "Add the $user account..\n";

  37. my $result = $ldap->add("uid=$user,ou=People,dc=test,dc=com,dc=cn",
  38.                         attr => [ 'cn' => "$user",
  39.                                   'sn' => "$user",
  40.                                   'uid' => "$user",
  41.                                   'mail' => "$user\@test.com.cn",
  42.                                   'userpassword' => "$password",
  43.                                   'objectclass' =>[ 'top',
  44.                                                      'person',
  45.                                                      'organizationalPerson',
  46.                                                      'inetOrgPerson'
  47.                                                      ]
  48.                            ]
  49. );

  50. $result->done;

  51. $result->code && warn "failed to add entry: ", $result->error ;



  52. #Send new mail notice
  53. print "Send the mail to notice $user account...\n";

  54. my %mail = ( To => "$user\@test.com.cn",
  55.                From => 'test1@testcom.cn',
  56.                Subject => 'Your username and password',
  57.                Message => "Hello, $user,your random password is $password",
  58.                smtp =>'192.168.x.x''
  59. );


  60. if (sendmail %mail) { print "Mail Send OK\n" }
  61. else { print "Error sending mail: $Mail::Sendmail::error\n" }

阅读(2659) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

jiannma2011-04-09 00:02:19

小雅贝贝: 感谢支持,文章不错,加油,希望以后继续写出好文章~.....
嘿嘿 支持 小雅MM

小雅贝贝2011-04-08 09:42:46

感谢支持,文章不错,加油,希望以后继续写出好文章~