| Category: | Utiltity Scripts |
| Author/Contact Info | coded by munkyeetr. This is my first Perl script, so I am looking for some feedback; what could I do better and more efficien |
| Description: | Command line script that can generate multiple random passwords of any length. User can exclude character-types by using command line options. I believe I have handled foreseeable errors, and that the code is well commented. REQUIRED MODULES: Getopt::Long |
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
# FUNCTION: GetRandNum()
# ---------------------------------------------------------
sub GetRandNum() {
my $lower=33;
my $upper=126;
my $random = int(rand( $upper-$lower+1 )) + $lower;
return $random;
}
# ---------------------------------------------------------
# SUB: DisplayUsage()
# ---------------------------------------------------------
sub DisplayUsage() {
print "\nUsage: genpass [-OPTIONS] LENGTH\n";
print "Generate secure passwords LENGTH characters long.\n\n";
print "\t-s, --symbols\t\tExclude symbol characters.\n";
print "\t-n, --numbers\t\tExclude number characters.\n";
print "\t-u, --uppercase\t\tExclude uppercase characters.\n";
print "\t-l, --lowercase\t\tExclude lowercase characters.\n\n";
print "\t-q(X)\t\t\tCreate X number of passwords.\n\n";
print "\t--help\t\t\tDisplay this usage screen.\n\n";
print "Report bugs, comments, and questions to jbrown_home\@yahoo.
+ca\n\n";
}
# ---------------------------------------------------------
# DECLARE OPTION FLAGS
# ---------------------------------------------------------
my $symbols='0'; # all self-explanatory
my $numbers='0';
my $uppercase='0';
my $lowercase='0';
my $help='0';
my $qty='0';
# PARSE AND SET COMMAND-LINE OPTIONS
# ---------------------------------------------------------
GetOptions( 's|S|symbols' => \$symbols,
'n|N|numbers' => \$numbers,
'u|U|uppercase' => \$uppercase,
'l|L|lowercase' => \$lowercase,
'q|Q:i' => \$qty,
'help' => \$help, );
## START INPUT CHECKING HERE
# ---------------------------------------------------------
my $kill='0'; # flag to stop the script if input is invalid (or --h
+elp is used)
my $errcount=0; # count error messages
my @errmsg; # error message array
# If -q option was used to set a quantity of passwords, make sure it c
+ontains at
# least a value of 1 so that a password can be generated
# ---------------------------------------------------------
if ($qty lt '0' || $qty eq '0') {
$qty='1';
}
# Check for --help option, kill script
# (This is not really an error, but I put it here because of the $kill
+ flag)
# ---------------------------------------------------------
if ($help eq '1') {
$kill='1';
}
# Check that user hasn't excluded all character-types, warn user, kill
+ script
# ---------------------------------------------------------
if (($symbols eq '1') && ($numbers eq '1') && ($uppercase eq '1') && (
+$lowercase eq '1')) {
$errmsg[$errcount]="\n** 0x1: At least 1 character-type must be in
+cluded";
$kill='1';
$errcount++;
}
# Check that user has passed only 1 argument (LENGTH), warn user, kill
+ script
# ---------------------------------------------------------
if ($#ARGV > 0 || $#ARGV < 0) {
$errmsg[$errcount]="\n** 0x2: Incorrect number of arguments passed
+";
$kill='1';
$errcount++;
}
# Check for only numeric input in LENGTH argument, warn user, kill scr
+ipt
# ---------------------------------------------------------
else {
if($ARGV[0] !~ /^[0-9]+$/) {
$errmsg[$errcount]="\n** 0x3: Invalid input. LENGTH argument m
+ust be a numeric value";
$kill='1';
$errcount++;
}
}
# If any of the above tests triggered the $kill flag, display errors a
+nd kill script
# ---------------------------------------------------------
if ($kill eq '1' && $help eq '1') { # If triggered by the --hel
+p option
DisplayUsage();
exit();
}
elsif ($kill eq '1') { # If triggered by an error
print "\n** GENPASS ERROR ----------------------------------------
+-----------------";
print "\n** ".$errcount." Error(s) found"; # show number of error
+s
foreach my $err (@errmsg) { # display error messages
print $err;
}
print "\n**\n** Type genpass --help for command usage\n";
print "** --------------------------------------------------------
+---------------\n\n";
exit(); # exit script
}
# ---------------------------------------------------------
## END INPUT CHECKING HERE
## MAIN SCRIPT STARTS HERE
# ---------------------------------------------------------
my $pwd=""; # holds password
my $length=$ARGV[0]; # length argument passed by user
while ($qty gt '0') { # while we have passwords to generate
$pwd=&GenPass($length); # generate a password
print $pwd; # display the password
$qty--; # we have 1 less to make
}
# ---------------------------------------------------------
## MAIN SCRIPT ENDS HERE
# FUNCTION: GenPass()
# DESCRIPTION: Generates a password of variable length based on option
+s passed on the command line.
# ARGUMENTS: Integer value "Length" assigned to $pwdlen
# RETURNS: String $password
# ---------------------------------------------------------
sub GenPass() {
my $pwdlen=$_[0]; # password length
my $count=0; # a counter
my $password=""; # string to hold the password as it's being genera
+ted
my $isgood=0; # validity flag
my $num=0; # will hold random numbers
# loop until our password is the desired length
while ($count < $pwdlen) {
$isgood=0; # reset the isgood flag
$num=GetRandNum(); # get a random number
# test the random number to see if it is a valid character
# and change the isgood flag if it hasn't been excluded by the use
+r...
if (($num >= 33 && $num <= 47) || ($num >= 58 && $num <= 64) |
+| ($num >= 91 && $num <= 95) || ($num >= 123 && $num <= 126)) {
if ($symbols eq '0') { # symbols
$isgood=1;
}
} elsif (($num >= 48 && $num <= 57)) {
if ($numbers eq '0') { # numbers
$isgood=1;
}
} elsif (($num >= 65 && $num <= 90)) {
if ($uppercase eq '0') { # uppercase
$isgood=1;
}
} elsif (($num >= 97 && $num <= 122)) {
if ($lowercase eq '0') { # lowercase
$isgood=1;
}
}
else {
; #do nothing
}
# test the isgood flag, and if the character is good
if ($isgood == 1) {
$password=$password.chr($num); # append the character
+to the password string
$count++; # increment the count
}
# or else do nothing, and loop again...
}
# return the finished password to main script
return $password."\n";
}
# ---------------------------------------------------------
__END__
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: genpass Password Generator
by graff (Chancellor) on Jun 02, 2007 at 17:39 UTC | |
|
Re: genpass Password Generator
by ww (Archbishop) on Jun 02, 2007 at 12:20 UTC | |
|
Re: genpass Password Generator
by GrandFather (Saint) on Jun 02, 2007 at 23:06 UTC | |
|
Re: genpass Password Generator
by Anonymous Monk on Jun 04, 2007 at 03:02 UTC | |
by munkyeetr (Initiate) on Jun 04, 2007 at 03:06 UTC | |
by blazar (Canon) on Jun 05, 2007 at 08:48 UTC | |
by graff (Chancellor) on Jun 05, 2007 at 03:52 UTC |