#!/usr/bin/env perl use strict; use warnings; ############################################################################## # # File : passgen.pl # Version : 0.10 # Author : H Bakkum (hakkum) # Email : bakkum.h (at) gmail (dot) com # History : 02-feb-2011 (hakkum) added options and created documentation # 27-jan-2011 (hakkum) program/script created # ############################################################################## =head1 NAME passgen - generate random alphanumeric password =head1 SYNOPSIS B [-LNSp] [-l I] =head1 DESCRIPTION B generates a random password of the specified I and according to the rules specified by the command line options. If no password length is given, a default length of 14 will be generated. If no options are provided, letters, numbers, and symbols will be used to create password. =head1 OPTIONS =over 4 =item B<-L> Prevent letters, both uppercase and lowercase, from being used during the password generation. =item B<-N> Prevents numbers, 0 through 9, from being used during the password generation. =item B<-S> Prevents the following symbols from being used during the password generation: ` ~ ! @ # $ % ^ & * ( ) _ + - = \ , . / < > ? ; ' : " [ ] { } =item B<-p> Prevents "Password: " from being sent to STDOUT. This allows for password to be used in a pipe. =item B<-l> I Specifies the length of the generated password. =item B<-h> Print a summary of options and exit. =back =head1 AUTHOR H Bakkum, =cut ############################################################################## use Getopt::Std; my %cli_options; getopts("LNSl:ph", \%cli_options); USAGE: { if ($cli_options{h}) { print< EOF_USAGE exit 0; } } MAIN: { print "Password: " if not $cli_options{p}; print random_password() . "\n"; } #----------------------------------------------------------------------------- # &random_password() #----------------------------------------------------------------------------- sub random_password { # define variables my @chars_pool; my $pass_len; my $password; my $random_number; # characters in password @chars_pool = character_pool(); # default password length $pass_len = 14; # if command line argument, use as password length if ($cli_options{l}) { $pass_len = $cli_options{l} if $cli_options{l} =~ /^\d+$/; $pass_len = 14 if $pass_len <= 0; $pass_len = 100 if $pass_len >= 100; } # generate password for (1..$pass_len) { $random_number = int rand @chars_pool; $password .= $chars_pool[$random_number]; } $password; } #----------------------------------------------------------------------------- # &character_pool() #----------------------------------------------------------------------------- sub character_pool { # define variables my @strings; my @digits; my @symbols; my @chars_pool; @strings = ('a'..'z', 'A'..'Z'); @digits = (0..9); @symbols = ( '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '\\', ',', '.', '/', '<', '>', '?', ';', '\'', ':', '"', '[', ']', '{', '}', ); push @chars_pool, @strings if not $cli_options{L}; push @chars_pool, @digits if not $cli_options{N}; push @chars_pool, @symbols if not $cli_options{S}; @chars_pool ? @chars_pool : "-"; }