#!/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__

In reply to genpass Password Generator by munkyeetr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.