Another password generator, written in perl. The special thing about this script is, that it's possible to specify the characters, that will occur in the password
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Getopt::Long;
6
7 Getopt::Long::Configure ("bundling", "gnu_compat");
8
9 my $version = 0.1;
10 my %options = map {$_, 0} qw(upper lower num special alpha alphanum all stdin version help);
11
12 GetOptions ( "upper|u" => \$options{'upper'},
13 "lower|l" => \$options{'lower'},
14 "num|n" => \$options{'num'},
15 "special|s" => \$options{'special'},
16 "alpha" => \$options{'alpha'},
17 "alphanum" => \$options{'alphanum'},
18 "all" => \$options{'all'},
19 "stdin" => \$options{'stdin'},
20 "help|h" => \$options{'help'},
21 "version|v" => \$options{'version'},
22 );
23
24 if ($options{'help'} == 1){
25 print "perlpwd - Generates a random password from a given set of characters\n" .
26 "using perl's rand() function\n" .
27 "\nUsage:" .
28 "\tperlpwd [options...] [passwordlength]\n" .
29 "If no passwordlength is given, the defaultlength (12 characters) will be used\n\n" .
30 "Options:\n" .
31 " -u, --upper\t\tUse upper case letters\n" .
32 " -l, --lower\t\tUse lower case letters\n" .
33 " -n, --num\t\tUse all digits\n" .
34 " -s, --special\t\tUse special characters\n" .
35 " --alpha\t\tUse upper and lower case characters\n" .
36 " --alphanum\tUse all letters and digits\n" .
37 " --all\t\tUse all characters (default)\n" .
38 " --stdin\t\tRead characters from STDIN\n" .
39 " -h, --help\t\tPrint this help and exit\n" .
40 " -v, --version\t\tDisplay the program version and exit\n" ;
41 exit 0;
42 }
43
44 if ($options{'version'}) {
45 print "perlpwd - version $version\nAuthor: Haui, contact haui45\@web.de\n";
46 exit 0;
47 }
48
49 ########## define the character arrays ###########
50 my @upper = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
51 my @lower = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
52 my @num = qw(0 1 2 3 4 5 6 7 8 9 );
53 no warnings;
54 my @special = qw( . : ; - _ # ' + * ~ ^ ° ! " § $ % & / = ? \ } ] [ { , ( ) < > | );
55 use warnings;
56 my @characters;
57 #################################################
58
59
60 #**evaluate the commandline options****
61 if ($options{'alphanum'}){
62 $options{'num'} = 1;
63 $options{'upper'} = 1;
64 $options{'lower'} = 1;
65 }
66
67 if ($options{'alpha'}){
68 $options{'upper'} = 1;
69 $options{'lower'} = 1;
70 }
71
72 if ($options{'all'}){
73 $options{'num'} = 1;
74 $options{'upper'} = 1;
75 $options{'lower'} = 1;
76 $options{'special'} = 1;
77 }
78
79 if ($options{'num'}){
80 @characters = (@characters, @num);
81 }
82 if ($options{'lower'}){
83 @characters = (@characters, @lower);
84 }
85
86 if ($options{'upper'}){
87 @characters = (@characters, @upper);
88 }
89 if ($options{'special'}){
90 @characters = (@characters, @special);
91 }
92 if ($options{'stdin'}){
93 my $input="";
94 while(my $line = <STDIN>){
95 chomp $line;
96 $input .= $line;
97 }
98 chomp $input;
99 undef @characters;
100 @characters = split //, $input;
101
102 #delete duplicate entries
103 my %hash = map{$_, 1} @characters;
104 undef @characters;
105 @characters = keys %hash;
106 #end
107 }
108
109 # use all characters if nothing else is defined
110 unless (@characters){
111 @characters = (@num, @special, @lower, @upper);
112 }
113
114 #******************************************
115
116 my $tmprand = int(rand(75)) + 25;
117
118 #randomize the array
119 for (my $i = 0; $i <= $tmprand; $i++){
120 @characters = sort{int(rand(2))}(@characters);
121 }
122
123
124 my $password = "";
125 my $passwordlength = 12;
126 if ($ARGV[0]){
127 $passwordlength = int($ARGV[0]);
128 }
129
130 #generate the password
131 for (my $i=0; $i<$passwordlength; $i++){
132 $password .= $characters[int(rand($#characters + 1))];
133 }
134
135 print "$password\n";
A simple shell script, that generates a random password from a given set of characters.
1 #!/bin/bash 2 3 array=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
1 2 3 4 5 6 7 8 9 0 ! - _ § $ % '&' / = ? ';' , . : '|' '<' '>'); 4 5 i=0; # Laufvariable 6 j=${#array[*]}; # Arraylaenge, d.h. Anzahl der Elemente 7 declare -a password; # Zielarry deklarieren - nicht unbedingt noetig 8 9 10 if (( $# != 1 )) # Falsche Anzahl an Aufrufparametern 11 then 12 echo "Fehler: Bitte eine positive Integer-Zahl als Argument übergeben" 13 echo Usage: ./password password_length 14 exit 1 15 fi 16 if [ $1 = "--help" ] # Hilfe... 17 then 18 echo Script zum Erstellen von Passwörtern variabler Länge 19 echo Usage: ./password password_length 20 exit 0 21 fi 22 23 if [ "${1//[^0-9]/}" == "$1" ] # ueberpruefen, ob PAramter eine Zahl ist 24 then 25 echo "Passwort wird erstellt..." 26 echo 27 else 28 echo "Der übergebene Paramteter ist keine positve Zahl!" 29 echo Usage: ./password password_length 30 exit 1 31 fi 32 33 34 while (( $i < $1 )) # solange Laufvariable kleiner als Laengenparamter ist.... 35 do 36 nummer=`od -w1 -t d1 -A n /dev/urandom | head -1 | sed 's/^[ \t]*//' | sed 's/-//g'` # nummer ist eine Pseudozufallszahl 37 while (( $nummer > $j )) # solange die Nummer kleiner als Arraylaenge ist, Nummer neu erstellen 38 do 39 nummer=`od -w1 -t d1 -A n /dev/urandom | head -1 | sed 's/^[ \t]*//' | sed 's/-//g'` 40 done 41 password[$i]=${array[$nummer]}; # andernfalls Arrayelement Nummer "nummer" auswählen && passwordelement i zuweisen 42 #i=`expr $i + 1` 43 (( i++ )) 44 done 45 46 47 echo ${password[*]} | sed 's/ //g'