Script:
# Written
by Daniel Paessens (HP -
TSG - EUWS)
#
E-mail: daniel.paessens@hp.com
#
#
Filename: Password.pl
# Creation Date: 08/03/2007
# Modification Date: 19/03/2007
#
Version: 0.2
# use strict;
use Text::Template;
use FileHandle; # this module allows you to use
object-oriented
# syntax for file functions
#
# Prepare Working envirnoment (Variables)
#
my $ini_file= "data.ini";
my $fh;
$fh = new FileHandle $ini_file || die "can't open $ini_file";
#go thru the ini file processing one line at a time.
while ($_ = $fh->getline){
chomp;
#skip lines with no letters
unless (/\w/) {next}
#skip comments
next if (/#/);
#split the line into the variable name and its
value
my ($name, $value) = split (/ = /);
$$name = ($value);
}
# @@ -9,8 +31,10 @@
our $minupper;
our $minspecial;
our $distribute;
my $index;
my $password;
my $templatedir =$workdir . "\\Template";
my $finaldir =$workdir . "\\Final";
my $zone =$workdir . "\\zone.txt";
my $overview =$workdir . "\\result.txt";
my $servers =$workdir . "\\serverlist.txt";
my
$job =$workdir
. "\\job.bat";
if ($length eq "" ) { $length = 12 ;};
if ($minnum eq "" ) { $minnum = 2 ;};
if ($minlower eq "" ) { $minlower = 2 ;};
if ($minupper eq "" ) { $minupper = 2 ;};
if ($minspecial eq "" ) { $minspecial = 2 ;};
# @@ -19,10 +43,67 @@
open(OVER,">$overview") || die ("Could not open
$overview");
open(ZONE, $zone) || die ("Could not open $zone");
print OVER "Creation of Passwords\n";
print OVER "---------------------\n";
foreach $line (<ZONE>) {
chomp($line);
my $psw = mkpasswd(
-length => $length,
-minnum => $minnum,
-minlower => $minlower, # minlower is
increased if necessary
-minupper => $minupper,
-minspecial => $minspecial,
-distribute => $distribute,);
my $tempfile = $templatedir .
"\\Change_Password.xml";
my $outputfile = $finaldir .
"\\Change_Password" . $line .".xml";
open(N, ">$outputfile");
my $template =
Text::Template->new(SOURCE => $tempfile);
my %vars = (psw => $psw);
my $result =
$template->fill_in(HASH => \%vars);
if (defined $result) { print N
$result;};
print OVER "Zone
$line\t\t$psw\n";
}
close(ZONE);
close(OVER);
open(SRV, $servers) || die ("Could not open $zone");
open(JOB, ">$job") || die ("Could not open $zone");
@lines = <SRV>;
close(SRV);
print JOB "\@echo off\n";
foreach (@lines) {
@info = split();
$srvname = $info[0];
$zonename = $info[1];
$job = "xcopy /y " . $finaldir .
"\\Change_Password" . $zonename .".xml \\\\" . $srvname .
"\\C\$\\Config\\iLO\\Change_Password.xml\n";
print JOB $job;
$job3 = "xcopy /y " . $workdir .
"\\Change_Password.bat \\\\" . $srvname .
"\\C\$\\Config\\iLO\\Change_Password.bat\n";
print JOB $job3;
$job2 = "psexec \\\\" . $srvname . "
-w C:\\Config\\iLO\\ C:\\Config\\iLO\\Change_Password.bat\n";
print JOB $job2;
}
exit;
sub mkpasswd {
my $password;
my $UPPER =
"ABCDFGHIJKLMNOPQRSTVWXYZ";
my $LOWER =
"abcdefghijklmnopqrstuvwxyz";
my $DIGIT = "0123456789";
my $SPECIAL =
"?,;.:/!?%*$+)?]@?#&";
my $index;
my $random_choice;
my @pass;
my @newpass;
for ($index=0; $index<$minupper;
$index++) {
$password
.= substr($UPPER,int(rand(length($UPPER))),1);
}
for ($index=0; $index<$minlower;
$index++) {
$password
.= substr($LOWER,int(rand(length($LOWER))),1);
}
for ($index=0; $index<$minnum;
$index++) {
$password
.= substr($DIGIT,int(rand(length($DIGIT))),1);
}
for ($index=0; $index<$minspecial;
$index++) {
$password
.= substr($SPECIAL,int(rand(length($SPECIAL))),1);
}
while (length($password) <
$length) {
$random_choice=int(rand(4));
if ($random_choice eq 0) {
$password
.= substr($SPECIAL,int(rand(length($SPECIAL))),1);
}
if ($random_choice eq 1) {
$password
.= substr($DIGIT,int(rand(length($DIGIT))),1);
}
if ($random_choice eq 2) {
$password
.= substr($LOWER,int(rand(length($LOWER))),1);
}
if ($random_choice eq 3)
{
$password
.= substr($UPPER,int(rand(length($UPPER))),1);
}
}
# randomize password letters
@pass = split(//, $password);
while (@pass) {
push(@newpass,
splice(@pass, rand @pass, 1));
}
$password="";
foreach (@newpass) {
$password
.= "$_";
}
return $password;
}