#!/usr/bin/perl use warnings; use strict; unless (@ARGV) { print <<"END_HELP"; Clare's Random Password Generator Usage: pwdgen template [number] where template is a string composed of the following characters: \tl\tlower case letter\n\tL\tupper case letter\n\tc\tlower case consonant\n\tC\tupper case consonant \tv\tlower case vowel\n\tV\tupper case vowel\n\tn\tnumber\n\tp\tpunctuation mark\n\ta\tany character Any character in the template which is not one of those above will be placed in the password at the same position. [number] refers to the number of passwords to generate - this defaults to 10. END_HELP exit(0); } my $count; my $template = $ARGV[0]; if ($ARGV[1] && $ARGV[1] =~ /^\d+$/) { $count = $ARGV[1]; } else { $count = 10; } my @numbers = (0..9); my @punctuation = qw(. ? < > : @ / ! " % ^ & * ( ) - + = _); my @loweralpha = ('a'..'z'); my @upperalpha = ('A'..'Z'); my @lowerconsonants = qw(b c d f g h j k l m n p q r s t v w x y z); my @upperconsonants = qw(B C D F G H J K L M N P Q R S T V W X Y Z); my @lowervowels = qw(a e i o u); my @uppervowels = qw(A E I O U); my @all = (@loweralpha, @upperalpha, @numbers, @punctuation); my $symbols = { 'n' => \@numbers, 'p' => \@punctuation, 'l' => \@loweralpha, 'L' => \@upperalpha, 'c' => \@lowerconsonants, 'C' => \@upperconsonants, 'v' => \@lowervowels, 'V' => \@uppervowels, 'a' => \@all, }; for (0..$count) { generate_password(); } sub generate_password { my $password = ''; $password = join '', map { exists $symbols->{$_} ? $symbols->{$_}[rand @{$symbols->{$_}}] : $_ } split //, $template; print "$password\n"; }