Skip to content

Instantly share code, notes, and snippets.

@bradclawsie
Created July 11, 2011 05:50
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 bradclawsie/1075363 to your computer and use it in GitHub Desktop.
Save bradclawsie/1075363 to your computer and use it in GitHub Desktop.
getpw.pl
#!/usr/bin/env perl
use v5.14;
use warnings;
use strict;
# this is my password file. yours may be different
my $pwfile = $ENV{'HOME'} . '/git/docs/pw/pw.txt.asc';
die $pwfile . ' not readable?' unless (-r $pwfile);
# search term required
my $term = $ARGV[0] || die 'use: getpw.pl term';
# make sure both gpg2 and xclip are available
my $gpg2 = `which gpg2`;
my $xclip = `which xclip`;
chomp $gpg2;
chomp $xclip;
die 'gpg2 required!' unless ($gpg2);
die 'xclip required!' unless ($xclip);
# try to find the term in the file, exit if not found
my $extract_line_cmd = $gpg2 . ' --decrypt ' . $pwfile . '|grep "' . $term . '"';
my $extract_line = `$extract_line_cmd`;
chomp $extract_line;
die $term . ' not found!' unless ($extract_line);
# my auth line format is domain userid password...we only want the password
my @auth_fields = split(' ',$extract_line);
my $pw = $auth_fields[2];
my $xclip_cmd = 'echo "' . $pw . '" | ' . $xclip . ' -selection clipboard';
system($xclip_cmd);
# i don't want the password lingering around in the clipboard
say 'in 30 seconds your clipboard will be erased';
$| = 1;
for (my $i = 30;$i >= 1;$i--) {
print "$i..";
sleep(1);
}
print "\n";
say 'erasing clipboard';
system('echo "" | ' . $xclip . ' -selection clipboard');
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment