#!/usr/bin/perl use strict; use warnings; use Getopt::Long; Getopt::Long::Configure ("bundling"); ## PARSE AND SET COMMAND-LINE OPTIONS ## ----------------------------------------------------- my %flags=('symbols', 0, 'numbers', 0, 'uppercase', 0, 'lowercase', 0, + 'confusable', 0, 'help', 0, 'qty', 1); GetOptions( 's|S|symbols' => \$flags{symbols}, 'n|N|numbers' => \$flags{numbers}, 'u|U|uppercase' => \$flags{uppercase}, 'l|L|lowercase' => \$flags{lowercase}, 'c|C|confusable' => \$flags{confusable}, 'q|Q:i' => \$flags{qty}, 'help' => \$flags{help}, ); # Set password characters, excluding those flagged on the command-line my $pwdchars = join( '', map {chr} ( 0x21 .. 0x7e )); $pwdchars =~ s/\d+// if ( $flags{numbers} ); $pwdchars =~ s/[A-Z]+// if ( $flags{uppercase} ); $pwdchars =~ s/[a-z]+// if ( $flags{lowercase} ); $pwdchars =~ s/[_\W]+//g if ( $flags{symbols} ); $pwdchars =~ tr/1Il0O//d if ( $flags{confusable} ); # If user triggered the --help option flag, display and exit if ($flags{help}) { &DisplayUsage(); exit(); } ## START VALIDATE INPUT ## ----------------------------------------------------- my $kill=0; # flag to stop the script if input is invalid (or - +-help is used) my @errmsg; # error message descriptions # 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 ($flags{qty} == 0 || $flags{qty} < 0) { $flags{qty}=1; } # Check that user hasn't excluded all character-types, warn user, kill + script if ( length($pwdchars) == 0) { push @errmsg, "** 0x1: At least 1 character-type must be included" +; $kill=1; } # Check that user has passed only 1 argument (LENGTH) other than optio +ns flags, warn user, kill script if ($#ARGV > 0 || $#ARGV < 0) { push @errmsg, "** 0x2: Incorrect number of arguments passed"; $kill=1; } # Check for only numeric input in LENGTH argument, warn user, kill scr +ipt if ($ARGV[0] !~ /^[0-9]+$/) { push @errmsg, "** 0x3: Invalid input. LENGTH argument must be +a numeric value"; $kill=1; } # If any of the above validation tests triggered the $kill flag... if ($kill == 1) { print "\n** GENPASS ERROR ---------------------------------------- +-----------------"; print "\n** ".@errmsg." Error(s) found"; # display number of +errors foreach my $err (@errmsg) { # display error messages print "\n".$err; } print "\n**\n** Type genpass --help for command usage\n"; print "** -------------------------------------------------------- +---------------\n\n"; exit(); # exit script } ## END VALIDATE INPUT ## START MAIN SCRIPT ## ----------------------------------------------------- # From 1 to qty for ( 1..$flags{qty} ) { print &GenPass( $ARGV[0] )."\n"; } exit(); ## END MAIN SCRIPT ## FUNCTION DEFINITIONS ## ----------------------------------------------------- sub GenPass() { my ($pwdlen) = @_; my $limit = length( $pwdchars ); my $pwd = ''; for ( 0..$pwdlen-1 ) { $pwd .= substr( $pwdchars, rand( $limit ), 1 ); } return $pwd; } # use Here-Documents to display usage text sub DisplayUsage { print <<" USAGE"; Usage: genpass [-OPTIONS] LENGTH Generate secure passwords LENGTH characters long. -s, --symbols\t\tExclude symbols. -n, --numbers\t\tExclude numbers. -u, --uppercase\t\tExclude uppercase letters. -l, --lowercase\t\tExclude lowercase letters. -c, --confusable\tExclude confusable characters like: l,I,1,0,O -q(X)\t\t\tCreate X number of passwords. --help\t\t\tDisplay this usage screen. Report bugs, comments, and questions to jbrown_home\@yahoo.ca USAGE } __END__
In reply to Re: genpass Password Generator
by Anonymous Monk
in thread genpass Password Generator
by munkyeetr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |