Random password generation with Perl
- October 16th, 2009
- Write comment
This script will generate new passwords for servers. This will take a list of servers from one file, generate a new password, then output the name of the server along with the password to a seperate file. In the new password file, it also generates code to reset the password for the account. The output will look like:
testserver #]biV}]F!?u
echo -e ‘#]biV}]F!?u\n#]biV}]F!?u’; history -c | passwd –stdin root
Copy and paste the whole echo command into the terminal. This will change the root password and then clear the history file. After all of the passwords have been change, run:
grep -v echo > newpassfile.txt
Now you have an easy to read password file to store for archives.
#!/usr/bin/perl -w
#############################################################
#Version 1.0
#Copyright (c) Jason C. Brown (2009)
#
#Released under the GNU GPLv3 license
#————————————
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see .
#############################################################
system(“clear”);
print “Server File: “;
$SERVERFILE = <>;
print “Password File: “;
$USRPASSFILE = <>;
print “Length of password: “;
$passlen = <>;
open(SERVFILE, “$SERVERFILE”) || die “$!”;
open(PASSFILE, “>$USRPASSFILE”) || die “$!”;
sub passgen()
{
@char = (‘a’..’z',’A’..’Z',’1′..’9′,’!',’@',’#',’
,’%',’^',’&’,'*’,'(‘,’)',’-',’_',’=',’+',’<’,'>’,’[',']‘,’{‘,’}',’:',’;',’?',’|');
$len = $passlen;
$ranpass = “”;
for (0..$len) {
$ranpass .= $char[int rand @char];
}
return $ranpass;
}
$i = 0;
foreach() {
$pass = passgen();
chomp($_) =~ /(.+)/;
print PASSFILE “$_\t$pass\n\techo -e \’$pass\\n$pass\’; history -c | passwd –stdin root\n”;
$i++;
}
close(SERVFILE);
close(PASSFILE);
