Skip to content

Instantly share code, notes, and snippets.

@Maki-Daisuke
Last active August 29, 2015 14:05
Show Gist options
  • Save Maki-Daisuke/59c23945d736a59cc881 to your computer and use it in GitHub Desktop.
Save Maki-Daisuke/59c23945d736a59cc881 to your computer and use it in GitHub Desktop.
捨てパスワードとかランダムな文字列を生成するのに使ってるの
#!/usr/bin/env perl
use strict;
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub HELP_MESSAGE {
my $fh = shift;
print $fh <<HELP;
Usage: $0 [-ADU] [-l <LENGTH>] [-n <NUM>] <CHARS>...
Generates random strings using <CHARS>.
By default, this use alphabets, digits and underscore in addition to <CHARS>.
Options:
-A: Do not use alphabets
-D: Do not use digits
-U: Do not use underscore
-l: Specify the length of generated strings (Default: 12)
-n: Specify the number of generated strings (Default: 5)
HELP
exit;
}
my %opts = (
A => 0,
D => 0,
U => 0,
l => 12,
n => 5,
);
getopts('ADUn:l:', \%opts);
# Check options
$opts{'l'} = 12 unless $opts{'l'} > 0;
$opts{'n'} = 5 unless $opts{'n'} > 0;
# Add used chars with checking duplication.
my %chars;
$chars{$_} = 1 foreach split //, join '', @ARGV;
unless ( $opts{'A'} ) {
$chars{$_} = 1 foreach 'a'..'z', 'A'..'Z';
}
unless ( $opts{'D'} ) {
$chars{$_} = 1 foreach '0'..'9';
}
unless ( $opts{'U'} ) {
$chars{'_'} = 1;
}
my @chars = keys %chars;
# Generate!
foreach ( 1 .. $opts{'n'} ) {
print $chars[int rand @chars] foreach 1..$opts{'l'};
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment