Skip to content

Instantly share code, notes, and snippets.

@taiyoh
Created May 17, 2012 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taiyoh/2716408 to your computer and use it in GitHub Desktop.
Save taiyoh/2716408 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Apache::Htpasswd;
use Term::ReadKey;
my $create;
my $result = GetOptions("create|c" => \$create);
my $path = shift or die "require file path\n";
my $user = shift or die "require auth user\n";
unlink $path if -f $path && $create;
unless (-f $path) {
open my $h, '>', $path;
close $h;
}
my $htpass = Apache::Htpasswd->new($path);
print "New password: ";
ReadMode 'noecho';
my $pass1 = ReadLine(0);
chomp $pass1; $pass1 =~ s{[\r\n]}{}g;
ReadMode 'restore';
print "\n";
print "Re-type New password: ";
ReadMode 'noecho';
my $pass2 = ReadLine(0);
chomp $pass2; $pass2 =~ s{[\r\n]}{}g;
ReadMode 'restore';
print "\n";
if ($pass1 eq $pass2) {
$htpass->htDelete($user);
$htpass->htpasswd($user, $pass1);
print "Adding password for user ${user}\n";
}
else {
print "htpasswd: password verification error\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment