Доброго времени суток.
Может кому пригодиться этот скрипт.
Создание почтовых учетных записей на Комнигейте по членству в локальной группе AD и отсылает оповещение о создании на почту. reportlist@DOMMm.ru
требует модулей:
LDAP CGP GeneratePassword
В стандартную поставку ActivePerl вроде не входят, приходится до
устанавливать через PPM
use Net::LDAP;
use CLI;
use CGP::CLI;
use Crypt::GeneratePassword qw(word chars);
use Net::SMTP;
#################### Глобальные константы ###################### DC-IP
#################### Глобальные константы ####################
my $CGPAccont;
my @ADAccount;
my $cli; my $Accym; my $addUser;
loadADAccount ();
$CGPAccont = LoadCGPAccont ();
ChekDiferent ();
$Accym = $Accym ."\n---------------------------------------------\nEnd\n";if ($addUser > 0) {SendMail ();}
######################################################################## ####################### загрузка учеток из АД ########################## ########################################################################sub loadADAccount {
my $realName; my $entry; my $displayName; my $atrRef; my $ldap = Net::LDAP->new($LDAPServerAddress,port=>389,timeout=>20) or die "Can't connect to $LDAPServerAddress via LDAP"; $result=$ldap->bind($LDAPAdminDN,password=>$LDAPAdminPassword) or die "Can't bind as admin: ".$result->error;$result->code && return "Can't bind as admin: ".$result->error;
my $mesg = $ldap->search ( # perform a search
base => $LDAPSearchBase, #"cn=$domain", filter => $ADGroop#, #attrs => $name ); $ldap->unbind(); # unbind & disconnect
unless(defined $mesg && !$mesg->code) {
return "LDAP search failed: ".$result->error;
}
if($mesg->all_entries() eq 0) {
return "LDAP: nothing groop ".$ADGroop;
}
foreach $entry ($mesg->all_entries)
{my $ref1=@$entry{'asn'};
my $attrs=@$ref1{'attributes'}; foreach $atrRef (@$attrs) { my $type=@$atrRef{'type'}; my $vals=@$atrRef{'vals'}; $realName=@$vals[0] if($type eq 'sAMAccountName'); $displayName=@$vals[0] if($type eq 'displayName');} #last; # we need only 1 entry my $userData = { RealName => $realName, Password => word(6,8), displayName => $displayName }; push(@ADAccount, $userData);
######################################################################## ################### загрузка учеток из комунигада ###################### ########################################################################sub LoadCGPAccont {
my @special=("public", "dilest","postmaster","pdx","200");
my $accounts;
my @accounts;
my $accountList;
my $i=0;
$cli = new CGP::CLI( { PeerAddr => $CGServerAddress,
PeerPort => 106, login => $CLILogin, password => $CLIPassword } ) || die "Can't login to CGPro: ".$CGP::ERR_STRING."\n"; #Array of undeleted\special accounts $accountList = $cli->ListAccounts($MailDomain); die "\nError " . $cli->getErrMessage . "(".$cli->getErrCode. ") fetching accounts list\n" unless ($accountList);
foreach (keys %$accountList) {
$accounts[$i]=$_; ++$i;};
# Delete special accounts from array of accounts
$i=0;
my $j=0;
foreach (@special) {
foreach (@accounts) { if ($accounts[$i] eq $special[$j]) {splice (@accounts, $i, 1);} ++$i; } ++$j; $i=0;
######################################################################## ################### сравнение массивов и создане пользователя###########
########################################################################sub ChekDiferent {
$Accym = $Accym ."Connect to Comunigate.....\n";
$cli = new CGP::CLI( { PeerAddr => $CGServerAddress, PeerPort => 106, login => $CLILogin, password => $CLIPassword } )
foreach my $item (@ADAccount) {
#$Accym = $Accym
.$item->{'RealName'}."\t\t".$item->{'displayName'}."\n";
if ( defined $CGPAccont->{$item->{'RealName'}}) { $Accym = $Accym .$item->{'RealName'}. "--- present skip user\n"; }else { $Accym = $Accym ."Atemp to create new user - " . $item->{'displayName'} . "\n.........\n"; $cli->SendCommand('CreateAccount '.$item->{'RealName'}. ' {RealName= "'.$item->{'displayName'}.'";}') || die "Can't create account via CLI:".$cli->getErrMessage; $Accym = $Accym ."Atemp to create new user - " .$item->{'displayName'} . " \n\nnsuccessfully greate user-name:".$item->{'RealName'}."\n.........\n Set new password for ".$item->{'RealName'}." as:". $item->{'Password'} ."\n";
$cli->UpdateAccountSettings($item->{'RealName'},{Password => $item->{'Password'}})
|| die "Error: ".$cli->getErrMessage.", quitting"; $Accym = $Accym ."All successfully \n.........\n" ; ++$addUser; }
sub SendMail ()
{
my $smtp = Net::SMTP->new(Host => '192.168.0.8',
Hello => 'DOMMm.ru', Timeout => 30, ); $smtp->mail('reportlist@DOMMm.ru <reportlist@DOMMm.ru>'); $smtp->to('reportlist@DOMMm.ru <reportlist@DOMMm.ru>'); $smtp->data(); $smtp->datasend("To: reportlist\@DOMMm.ru\n"); $smtp->datasend("From: ScriptCMD\@DOMMm.ru\n"); $smtp->datasend("Subject: Adding new user to Comunigate from AD\n");$smtp->datasend("\n\n");
$smtp->datasend ($Accym); $smtp->dataend(); $smtp->quit;
Этот архив был сгенерирован hypermail 2.1.8 : Tue 10 Jun 2008 - 16:14:30 MSD