I would probably write your script something like the following, avoiding the problem of how many passwords to generate.
use strict;
use warnings;
my $users = "new.users";
open(my $in, '<', $users) or die "Cannot open $users: $!";
my $tmp = $users . '.tmp';
open(my $out, '>', $tmp) or die "Cannot open $tmp: $!";
foreach (<$in>) {
if(/^userPassword: xyz456$/) {
my $pw = generatePassword(10);
s/^userPassword: xyz456/userPassword: $pw/;
}
print $out "$_";
}
close($out);
close($in);
unlink($users) or die "unlink($users): $!";
rename($tmp, $users) or die "rename($tmp, $users): $!";
exit(0);
sub generatePassword {
my $length = shift;
my $password = "";
my $possible = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRST
+UVWXYZ';
while (length($password) < $length) {
$password .= substr($possible, (int(rand(length($possible)))), 1);
}
return $password
}
|