Last active
October 9, 2015 09:37
-
-
Save jpallari/3481666 to your computer and use it in GitHub Desktop.
Credential getter for CFG formatted password files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
from ConfigParser import ConfigParser | |
from subprocess import Popen, PIPE, STDOUT | |
# Place your own settings here | |
filepath = (os.environ['HOME'], '.mypwfile.gpg') | |
pipecmd = ('/usr/bin/pbcopy',) | |
decryptcmd = ('gpg2', '-q', '-d', 'FILE') | |
def pwget(lookupstr): | |
filename = os.path.join(*filepath) | |
command = replace_pattern(decryptcmd, 'FILE', filename) | |
output = command_output(command) | |
parser = parser_for_stream(output) | |
section = find_matching_section(parser, lookupstr) | |
if section: | |
(name, username, password) = section | |
print "Found match:", name | |
print "Piping to command:", ' '.join(pipecmd) | |
pipe_credentials(username, password) | |
else: | |
print "No matches found" | |
def replace_pattern(sequence, pattern, replacement): | |
return (x if x != pattern else replacement | |
for x in sequence) | |
def command_output(command): | |
return Popen(command, stdout=PIPE, stderr=STDOUT, close_fds=True).stdout | |
def parser_for_stream(stream): | |
cfgparser = ConfigParser() | |
cfgparser.readfp(stream) | |
return cfgparser | |
def find_matching_section(parser, lookup_string): | |
section = find_matching_section_name(parser, lookup_string) | |
if section: | |
items = parser.items(section) | |
(username, password) = get_credentials(items) | |
return (section, username, password) | |
else: | |
return None | |
def find_matching_section_name(parser, lookup_string): | |
g = (x for x in parser.sections() | |
if startswith_caseinsensitive(x, lookup_string)) | |
try: | |
return g.next() | |
except: | |
return None | |
def startswith_caseinsensitive(a, b): | |
return a.lower().startswith(b.lower()) | |
def get_credentials(items): | |
d = dict(items) | |
username = d.get('username', '').strip("'\"") | |
password = d.get('password', '').strip("'\"") | |
return (username, password) | |
def pipe_credentials(username, password): | |
send_to_pipe('user name', username) | |
send_to_pipe('password', password) | |
def send_to_pipe(key, value): | |
if value: | |
wait_for_enter(key) | |
pipe(value) | |
else: | |
print "No %s found" % key | |
def wait_for_enter(key): | |
print "Press enter to pipe", key | |
sys.stdin.read(1) | |
def pipe(value): | |
p = Popen(pipecmd, stdin=PIPE) | |
p.communicate(value) | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
arg = ' '.join(sys.argv[1:]) | |
pwget(arg) | |
else: | |
print """usage: %s ENTRY""" % os.path.basename(sys.argv[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment