#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use POSIX; our $LENGTH = 8; our $COUNT = 1; our ($ALPHA, $BASE64, $INSENSITIVE); GetOptions( "alphanumeric" => \$ALPHA, "base64" => \$BASE64, "length|size=i" => \$LENGTH, "count=i" => \$COUNT, "insensitive" => \$INSENSITIVE, "help" => sub { usage(); exit 0; }, ); our @CHARS = ( 'a' .. 'z', 0 .. 9 ); push @CHARS, 'A' .. 'Z' unless ( $INSENSITIVE ); if ( $ALPHA ) { } elsif ( $BASE64 ) { push @CHARS, split //, q(+/); } else { push @CHARS, '_', split //, q{!@#$%^&*()-=+[]{}|;':",./<>?`~} } foreach ( 1..$COUNT ) { my $printf_str = ceil( log10( $COUNT + 1 ) ); printf("%${printf_str}s: ", $_ ) if $COUNT > 1; my $pw; foreach ( 1..$LENGTH ) { $pw .= $CHARS[ rand @CHARS ]; } print $pw,"\n"; } sub usage { print <<"_USAGE_"; usage: rand-pw.pl [ --length|size size ] [ --alphanumeric ] [ --count num ] [ --insensitive ] _USAGE_ }